R 错误:stat_count()只能具有x或y值

R 错误:stat_count()只能具有x或y值,r,R,我得到一个错误: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(d

我得到一个错误: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文件中的数据如下所示


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

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

或者可能是
geom_col
@sahwahn它起作用了如果我想在图中同时添加男性和女性值呢@sahwahn您需要将数据重新格式化为长格式-这应该可以解释@sahwahn有没有一种简单易行的方法适合初学者?对我来说,一切都很顺利