gpt4 book ai didi

r - 连接意味着在箱线图上用一条线

转载 作者:行者123 更新时间:2023-12-02 15:33:08 27 4
gpt4 key购买 nike

我有一个显示多个框的箱线图。我想用一条线将每个框的平均值连接在一起。箱线图默认不显示均值,中间线仅表示中位数。我试过了

ggplot(data, aes(x=xData, y=yData, group=g)) 
+ geom_boxplot()
+ stat_summary(fun.y=mean, geom="line")

这不起作用。

有趣的是,正在做

stat_summary(fun.y=mean, geom="point") 

绘制每个框中的中点。为什么“line”不起作用?

像这样但是使用ggplot2, https://aliquote.org/pub/RMB/c4_sols/RMB_c4_sols.html#Fig.%203

enter image description here

最佳答案

这就是你要找的吗?

library(ggplot2)

x <- factor(rep(1:10, 100))
y <- rnorm(1000)
df <- data.frame(x=x, y=y)

ggplot(df, aes(x=x, y=y)) +
geom_boxplot() +
stat_summary(fun=mean, geom="line", aes(group=1)) +
stat_summary(fun=mean, geom="point")

更新:

关于设置 group=1 的一些说明:我想我在 Hadley Wickham 的书“ggplot2: Elegant Graphics for Data Analysis ”中找到了解释。他在第 51 页写道:

Different groups on different layers.

Sometimes we want to plot summariesbased on different levels ofaggregation. Different layers mighthave different group aesthetics, sothat some display individual leveldata while others display summaries oflarger groups.

Building on the previous example,suppose we want to add a single smoothline to the plot just created, basedon the ages and heights of all theboys. If we use the same grouping forthe smooth that we used for the line,we get the first plot in Figure 4.4.

p + geom_smooth(aes(group = Subject),method="lm", se = F)

This is not what we wanted; we haveinadvertently added a smoothed linefor each boy. This new layer needs adifferent group aesthetic, group = 1,so that the new line will be based onall the data, as shown in the secondplot in the figure. The modified layerlooks like this:

p + geom_smooth(aes(group = 1),method="lm", size = 2, se = F)

[...] Using aes(group = 1) in thesmooth layer fits a single line ofbest fit across all boys."

关于r - 连接意味着在箱线图上用一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3989987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com