繁体   English   中英

如何使用ggplot2绘制带有来自data.frame的颜色填充的geom_bar图

[英]How to plot with ggplot2 a geom_bar graph with color fillings from a data.frame

我想获得一个条形图,其中条形高度对应于一个总值,条形本身根据部分值分为颜色。

df <- data.frame(name = c("a", "b", "c"), 
                 total = c(10, 20, 30), 
                 left = c(5, 10, 5), 
                 middle = c(4, 5, 15), 
                 right = c(1, 5, 10))

df <- gather(df, key, value, 3:5)

ggplot(df, aes(x = name, y = total, fill = key)) + geom_bar(stat = "identity")

我得到的不尊重部分值的分布...

将总计更改为ggplot中的值:

ggplot(df, aes(x = name, y = value, fill = key)) + geom_bar(stat = "identity")

代替:

ggplot(df, aes(x = name, y = total, fill = key)) + geom_bar(stat = "identity")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM