[英]How to plot with ggplot2 a geom_bar graph with color fillings from a data.frame
I would like to get a bar graph in which the bar height corresponds to a total value, and the bar itself is divided into colors according to partial values. 我想获得一个条形图,其中条形高度对应于一个总值,条形本身根据部分值分为颜色。
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")
What I get does not respect the distribution of the partial values... 我得到的不尊重部分值的分布...
Change total to value in the ggplot: 将总计更改为ggplot中的值:
ggplot(df, aes(x = name, y = value, fill = key)) + geom_bar(stat = "identity")
instead of: 代替:
ggplot(df, aes(x = name, y = total, fill = key)) + geom_bar(stat = "identity")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.