ggplot 在期望闪避图时生成堆积条
ggplot producing stacked bar when expecting a dodge chart
我有一个数据集,其中显示了产品、会计期间和交易 table。
app$FISCAL_PERIOD <-(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)
当我 运行 这个时,我得到了我想要的图表,一个 "dodged" 条形图:
ggplot(data=app, aes(x=FISCAL_PERIOD, y=sum_trans, fill=Product)) + geom_bar(stat="identity", position=position_dodge(), colour="black")
但是当我 运行 这时,我得到了一个堆叠条形图,这不是我需要的:
ggplot(data=app, aes(x=Product, y=sum_trans, fill=FISCAL_PERIOD)) + geom_bar(stat="identity", position=position_dodge(), colour="black")
这可能是因为 ggplot 不理解 FISCAL_PERIOD 变量作为因子变量。这应该可以解决问题:
ggplot(data=app, aes(x=Product, y=sum_trans, fill=as.factor(FISCAL_PERIOD))) +
geom_bar(stat="identity", position="dodge", colour="black")
我有一个数据集,其中显示了产品、会计期间和交易 table。
app$FISCAL_PERIOD <-(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)
当我 运行 这个时,我得到了我想要的图表,一个 "dodged" 条形图:
ggplot(data=app, aes(x=FISCAL_PERIOD, y=sum_trans, fill=Product)) + geom_bar(stat="identity", position=position_dodge(), colour="black")
但是当我 运行 这时,我得到了一个堆叠条形图,这不是我需要的:
ggplot(data=app, aes(x=Product, y=sum_trans, fill=FISCAL_PERIOD)) + geom_bar(stat="identity", position=position_dodge(), colour="black")
这可能是因为 ggplot 不理解 FISCAL_PERIOD 变量作为因子变量。这应该可以解决问题:
ggplot(data=app, aes(x=Product, y=sum_trans, fill=as.factor(FISCAL_PERIOD))) +
geom_bar(stat="identity", position="dodge", colour="black")