从平均值的计算值中分组geom_boxplot

时间:2022-03-02 13:06:00

I created some grouped boxplots, basically for each dimension on the x axis I am showing various groups. Because my dataset is quite large, I had to precalculate the values for the boxes as ggplot did not have enough memory (I used ddply and did it in pieces).

我创建了一些分组的箱形图,基本上对于x轴上的每个维度,我显示了各种组。因为我的数据集非常大,所以我不得不预先计算框的值,因为ggplot没有足够的内存(我使用ddply并将其分成几部分)。

I believe this is beter than just bar charts of the averages as it shows some of the variability.

我相信这不仅仅是平均值的条形图,因为它显示了一些变化。

I want 2 modifications, one was to not show the whisker lines, and I have done that by setting ymin=lower and ymax=upper.

我想要2个修改,一个是不显示胡须线,我通过设置ymin = lower和ymax = upper来完成。

I also wanted to add the means as well, but they show all in the center of each X category, and of course I want them each aligned with its box.

我也想添加手段,但它们显示在每个X类别的中心,当然我希望它们每个都与它的盒子对齐。

to make it easier on anyone helping, I recreated the same chart using mtcars - I tried position = "dodge" and "identity" with no change

为了方便任何人帮助,我使用mtcars重新创建了相同的图表 - 我尝试了position =“dodge”和“identity”,没有任何变化

Anyone knows how to do this? I searched and did not find a way. I am also attaching a picture of my latest chart. Code is below

谁知道怎么做?我搜索并没有找到方法。我还附上了我最新图表的图片。代码如下

data(mtcars)
data <- as.data.frame(mtcars)
data$cyl <- factor(data$cyl)
data$gear <- factor(data$gear)

summ <- ddply(data, .(cyl, gear),summarize, lower=quantile(mpg,probs=0.25,na.rm=T), middle=quantile(mpg,probs=.5,na.rm=T),upper=quantile(mpg,probs=.75,na.rm=T),avg=mean(mpg,na.rm=T))

p2 <- ggplot(summ, aes(x = cyl, lower = lower, middle = middle, upper = upper,fill=gear,ymin=lower,ymax=upper))+geom_boxplot(stat = "identity")
p2 <- p2 + geom_point(aes(x = cyl, y=avg, color=gear),color="red",position="dodge")
p2 

从平均值的计算值中分组geom_boxplot

1 个解决方案

#1


2  

The problem is that the width of the points is not the same as the width of the box plots. In that case you need to tell position_dodge what width do use. ?position_dodge gives a simple example of this using points and error bars, but the principle is the same for points and box plots. In your example, replacing position="dodge" with position=position_dodge(width=0.9) will dodge the points by the same amount as the box plots.

问题是点的宽度与箱形图的宽度不同。在这种情况下,您需要告诉position_dodge使用什么宽度。 ?position_dodge使用点和误差条给出了一个简单的例子,但点和箱形图的原理是相同的。在您的示例中,使用position = position_dodge(width = 0.9)替换position =“dodge”将使点数与箱形图相同。

#1


2  

The problem is that the width of the points is not the same as the width of the box plots. In that case you need to tell position_dodge what width do use. ?position_dodge gives a simple example of this using points and error bars, but the principle is the same for points and box plots. In your example, replacing position="dodge" with position=position_dodge(width=0.9) will dodge the points by the same amount as the box plots.

问题是点的宽度与箱形图的宽度不同。在这种情况下,您需要告诉position_dodge使用什么宽度。 ?position_dodge使用点和误差条给出了一个简单的例子,但点和箱形图的原理是相同的。在您的示例中,使用position = position_dodge(width = 0.9)替换position =“dodge”将使点数与箱形图相同。