ggplot2: geom_bar 填充颜色;如何更改为不同的数据分组
ggplot2: geom_bar fill colour; how to change to different grouping of data
我有以下显示会计期间、产品和交易总和的数据集:
app$FISCAL_PERIOD <-c(201604,201604,201604,201605,201605,201605,201606,201606,201606,201607,201607,201607,201608,201608,201608,201609,201609,201610,201610,201611,201611)
app$Product <- c("Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 2","Product 3","Product 2","Product 3","Product 2","Product 3")
app$sum_trans<-c(78,23410,1946,84,29532,417,16,30364,129,305,32386,584,424,20873,274,20,20929,470,19261,10,6131)
我在 ggplot2 中将其绘制为闪避条形图。 Ggplot 会自动为每个填充分配颜色,以便每个会计期间都有不同的颜色。
ggplot(data = app, aes(x = Product, y = sum_trans, fill = as.factor(FISCAL_PERIOD))) +
geom_bar(stat="identity", position="dodge", colour="black")
我需要的是显示每个产品具有相同的颜色和不影响图表的填充变量。
IE。我希望所有产品 1 会计期间数据为一种颜色,所有产品 3 会计期间数据为另一种颜色。
您可以将填充变量设置为 product
,因为它定义了您想要的颜色,并指定一个额外的组变量为 FISCAL_PERIOD
,这样每个条仍将按年份分段:
ggplot(data = app, aes(x = Product, y = sum_trans, fill = Product, group = FISCAL_PERIOD)) +
geom_bar(stat="identity", position="dodge", colour="black")
我有以下显示会计期间、产品和交易总和的数据集:
app$FISCAL_PERIOD <-c(201604,201604,201604,201605,201605,201605,201606,201606,201606,201607,201607,201607,201608,201608,201608,201609,201609,201610,201610,201611,201611)
app$Product <- c("Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 2","Product 3","Product 2","Product 3","Product 2","Product 3")
app$sum_trans<-c(78,23410,1946,84,29532,417,16,30364,129,305,32386,584,424,20873,274,20,20929,470,19261,10,6131)
我在 ggplot2 中将其绘制为闪避条形图。 Ggplot 会自动为每个填充分配颜色,以便每个会计期间都有不同的颜色。
ggplot(data = app, aes(x = Product, y = sum_trans, fill = as.factor(FISCAL_PERIOD))) +
geom_bar(stat="identity", position="dodge", colour="black")
我需要的是显示每个产品具有相同的颜色和不影响图表的填充变量。 IE。我希望所有产品 1 会计期间数据为一种颜色,所有产品 3 会计期间数据为另一种颜色。
您可以将填充变量设置为 product
,因为它定义了您想要的颜色,并指定一个额外的组变量为 FISCAL_PERIOD
,这样每个条仍将按年份分段:
ggplot(data = app, aes(x = Product, y = sum_trans, fill = Product, group = FISCAL_PERIOD)) +
geom_bar(stat="identity", position="dodge", colour="black")