不得与条形图中的y美学错误一起使用

不得与条形图中的y美学错误一起使用

本文介绍了R ggplot2:stat_count()不得与条形图中的y美学错误一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘制条形图时遇到此错误,但我无法摆脱它,我尝试了qplot和ggplot,但仍然是相同的错误.

I am getting this error while plotting a bar graph and I am not able to get rid of it, I have tried both qplot and ggplot but still the same error.

以下是我的代码:

 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中的数据:

Data in data_country:

    country conversion_rate
    <fctr>           <dbl>
  1   China     0.001331558
  2 Germany     0.062428188
  3      UK     0.052612025
  4      US     0.037800687

该错误出现在条形图中,而不是虚线图中.

The error is coming in bar chart and not in the dotted chart.

推荐答案

首先,您的代码有点错误. aes()ggplot()中的一个参数,您不使用ggplot(...) + aes(...) + layers

First off, your code is a bit off. aes() is an argument in ggplot(), you don't use ggplot(...) + aes(...) + layers

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

您需要第二种情况,条的高度等于conversion_rate所以您想要的是...

You want the second case, where the height of the bar is equal to the conversion_rate So what you want is...

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")

结果:

这篇关于R ggplot2:stat_count()不得与条形图中的y美学错误一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:41