[英]Bar Plot alignment issue in R
给定的数据框由不同的行组成,其中一些样本包含 3 列
df1 <- data.frame( income = c(>20K, <=20K, >20K, <=20K),
country name = c(Cuba, Cuba, USA, USA),
count = c(10, 12, 21, 27)
我想要 plot 条形图,国家名称由收入填充。 没有得到正确的结果
这是我的代码
ggplot(df2, aes(x = region)) +
geom_bar(aes(fill = income), position = "fill")
请帮忙!!
这里有几个问题:
df1
,但ggplot
调用将df2
作为数据参数。df1
中的字符串没有用引号引起来df1
缺少右括号。df1
还有一个非法的列名country name
,它没有用引号括起来。region
的列,但这就是您在 ggplot 调用中使用的列。geom_bar
默认情况下计算数据框中的条目,而不是绘制您想要的实际计数,因此您应该为 y 轴指定count
并将其geom_col
position = "fill"
在这里做什么。无论如何,如果您解决了所有这些问题,它似乎可以正常工作。
df1 <- data.frame( income = c(">20K", "<=20K", ">20K", "<=20K"),
region = c("Cuba", "Cuba", "USA", "USA"),
count = c(10, 12, 21, 27))
ggplot(df1, aes(x = region, y = count)) +
geom_col(aes(fill = income))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.