【问题标题】:Error: stat_count() can only have an x or y aesthetic [duplicate]错误:stat_count() 只能具有 x 或 y 美学[重复]
【发布时间】:2020-07-18 22:29:26
【问题描述】:

我收到错误:stat_count() 只能具有 x 或 y 美学。尝试使用 Excel 工作表中的数据进行绘图时

library(readxl)
library(dplyr)
library(ggplot2)
dataset= read_excel("D:/Downloads/Covid19.xlsx")
dataset2= read_excel("D:/Downloads/Covid19.xlsx", sheet = "Sheet2")
dataset3= dataset[,c(4,5)]
ggplot(dataset2, aes(x=Region, y= male))+geom_bar()

我的 excel 文件中的数据如下所示 Dataset

Excel

【问题讨论】:

  • 或者geom_col
  • @sahwahn 成功了
  • 如果我想在图表中同时添加男性和女性值怎么办? @sahwahn
  • 您需要将数据重新格式化为长格式 - 这应该解释它r-graph-gallery.com/48-grouped-barplot-with-ggplot2.html
  • @sahwahn 是否有适合初学者的简单方法?这对我来说都是保镖

标签: r


【解决方案1】:

您需要包含stat=identity,这基本上是告诉ggplot2您将为条形图提供y值,而不是计算每个x值的总行数,这是默认的stat=count

ggplot(dataset2, aes(x=Region, y= male)) + geom_bar(stat='identity')

【讨论】:

    猜你喜欢
    • 2017-02-02
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多