ggplot2堆叠条形图-对象y没有找到

时间:2022-08-24 14:48:44

I'm trying to make a very simple stacked bar chart in ggplot2, but for some reason it doesn't work and I get the error message: "Error in pmin(y, 0) : object 'y' not found". Where's the problem?

我试图在ggplot2中创建一个非常简单的堆叠条形图,但是由于某些原因它不能工作,我得到了错误消息:“pmin(y, 0)中的错误:对象'y' not found”。问题在哪里?

factorvar <- c(1,1,1,2,2,2,3,3,3)
factorvar <- factor(factorvar, labels=c("Type", "Size", "Outcome"))
freq <- c(3,1,4,1,2,2,4,1,1)
fillvar <- c(1,1,1,2,2,2,3,3,3)
fillvar <- factor(fillvar)

df.harvest <- data.frame(fillvar,freq,factorvar)

harvest <- ggplot(df.harvest, aes(x=factorvar, y=freq, fill=fillvar)) + geom_bar()
harvest

3 个解决方案

#1


3  

This is pretty old but I didn't see any good answer for it:

这是相当古老的,但我没有看到任何好的答案:

You should always specify the arguments in geom_bar if you are providing the y axis.

如果要提供y轴,则应该始终在geom_bar中指定参数。

You are already providing frequency as y axis and don't want geom_bar to calculate it for you, so you must specify geom_bar(stat = "identity")

您已经提供了y轴的频率,并且不希望它为您计算它,所以您必须指定“风水”(stat =“identity”)

Also, here your fillvar is essentially the same as factorvar and there is no point in using one as axis and one as fill color.

同样,在这里,fillvar和factorvar本质上是一样的,使用一个作为轴,一个作为填充颜色是没有意义的。

However if they were different, you had to also specify geom_bar(stat = "identity", position="dodge") or (stat = "identity", position="stack").

但是,如果它们不同,您还必须指定geom_bar(stat =" identity", position="dodge")或(stat =" identity", position="stack")。

By default, the stat was switching to "bin" in your case and it was giving the error. Also by default, the position uses stack.

默认情况下,在你的情况下,统计数据会切换到“bin”,它会给出错误。默认情况下,该位置使用堆栈。

#2


2  

I'm not really sure if I understand what you want to count and what you want to stack, but using some of your data and this code

我不太确定我是否理解你想计数什么和你想堆栈什么,但是使用你的一些数据和这个代码

qplot(factor(freq), data=df.harvest, geom="bar", fill=factorvar) 

I get this,

我得到了这个,

ggplot2堆叠条形图-对象y没有找到

Is that what you are looking?

这就是你想要的吗?

#3


1  

I'm not sure what the error message means, but if you use fillvar = c(1,2,3,1,2,3,1,2,3), (in place of your fillvar variable) you will get a stacked bar chart.

我不确定错误消息的含义是什么,但是如果您使用fillvar = c(1、2、3、1、2、3、1、2、3),那么您将得到一个堆叠的条形图。

#1


3  

This is pretty old but I didn't see any good answer for it:

这是相当古老的,但我没有看到任何好的答案:

You should always specify the arguments in geom_bar if you are providing the y axis.

如果要提供y轴,则应该始终在geom_bar中指定参数。

You are already providing frequency as y axis and don't want geom_bar to calculate it for you, so you must specify geom_bar(stat = "identity")

您已经提供了y轴的频率,并且不希望它为您计算它,所以您必须指定“风水”(stat =“identity”)

Also, here your fillvar is essentially the same as factorvar and there is no point in using one as axis and one as fill color.

同样,在这里,fillvar和factorvar本质上是一样的,使用一个作为轴,一个作为填充颜色是没有意义的。

However if they were different, you had to also specify geom_bar(stat = "identity", position="dodge") or (stat = "identity", position="stack").

但是,如果它们不同,您还必须指定geom_bar(stat =" identity", position="dodge")或(stat =" identity", position="stack")。

By default, the stat was switching to "bin" in your case and it was giving the error. Also by default, the position uses stack.

默认情况下,在你的情况下,统计数据会切换到“bin”,它会给出错误。默认情况下,该位置使用堆栈。

#2


2  

I'm not really sure if I understand what you want to count and what you want to stack, but using some of your data and this code

我不太确定我是否理解你想计数什么和你想堆栈什么,但是使用你的一些数据和这个代码

qplot(factor(freq), data=df.harvest, geom="bar", fill=factorvar) 

I get this,

我得到了这个,

ggplot2堆叠条形图-对象y没有找到

Is that what you are looking?

这就是你想要的吗?

#3


1  

I'm not sure what the error message means, but if you use fillvar = c(1,2,3,1,2,3,1,2,3), (in place of your fillvar variable) you will get a stacked bar chart.

我不确定错误消息的含义是什么,但是如果您使用fillvar = c(1、2、3、1、2、3、1、2、3),那么您将得到一个堆叠的条形图。