关于 r:ggplot 在期望闪避图时产生堆积条 | 珊瑚贝

ggplot producing stacked bar when expecting a dodge chart


我有一个数据集,在一张表中显示产品、会计期间和交易。

1
2
3
4
5
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)

当我运行它时,我得到了我希望的图表,一个”躲避”的条形图:

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 变量理解为因子变量。这应该可以解决问题:

1
2
ggplot(data=app, aes(x=Product, y=sum_trans, fill=as.factor(FISCAL_PERIOD))) +
geom_bar(stat=”identity”, position=”dodge”, colour=”black”)

来源:https://www.codenong.com/38418691/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8679.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?