gpt4 book ai didi

R ggplot2 : stat_count() must not be used with a y aesthetic error in Bar graph

转载 作者:行者123 更新时间:2023-12-03 05:15:25 25 4
gpt4 key购买 nike

我在绘制条形图时遇到此错误,并且无法摆脱它,我已经尝试了 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()

错误:

  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(. ..) + 图层

第二,来自帮助文件?geom_bar:

By default, geom_bar uses stat="count" which makes the height of the bar proportion to the number of cases in each group (or if the weight aethetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use stat="identity" and map a variable to the y aesthetic.

您想要第二种情况,其中条形的高度等于 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")

结果:

enter image description here

关于R ggplot2 : stat_count() must not be used with a y aesthetic error in Bar graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39679057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com