我在绘制条形图时遇到此错误,并且无法摆脱它,我已经尝试了 qplot 和 ggplot 但仍然出现相同的错误。
以下是我的代码:
library(dplyr)
library(ggplot2)
#Investigate data further to build a machine learning model
data_country = data %>%
group_by(country) %>%
summarise(conversion_rate = mean(converted))
#Ist method
qplot(country, conversion_rate, data = data_country,geom = "bar", stat ="identity", fill = country)
#2nd method
ggplot(data_country)+aes(x=country,y = conversion_rate)+geom_bar()
Error:
stat_count() must not be used with a y aesthetic
data_country 中的数据:
country conversion_rate
<fctr> <dbl>
1 China 0.001331558
2 Germany 0.062428188
3 UK 0.052612025
4 US 0.037800687
错误出现在条形图中,而不是出现在虚线图中。
首先,你的代码有点不对劲。aes()
是一个参数ggplot()
,你不使用ggplot(...)
+ aes(...) + layers
二、从帮助文件来看?geom_bar
:
默认情况下,geom_bar 使用 stat="count" 这使得
条形与每组中病例数的比例(或者如果权重
提供审美,权重之和)。如果你想要高度
表示数据中的值的条形,使用 stat="identity" 和
将变量映射到 y 美学。
您想要第二种情况,其中条形的高度等于conversion_rate
所以你想要的是...
data_country <- data.frame(country = c("China", "Germany", "UK", "US"),
conversion_rate = c(0.001331558,0.062428188, 0.052612025, 0.037800687))
ggplot(data_country, aes(x=country,y = conversion_rate)) +geom_bar(stat = "identity")
Result:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)