在箱线图的一部分添加geom_smooth。

huangapple go评论72阅读模式
英文:

Add geom_smooth to part of of boxplot

问题

I would like to add geom_smooth of each treatment with data only from week 0 to week 8 timepoints while keeping the "before treatment" boxplots in pre and post timepoints.

我想要在图上添加每种处理的geom_smooth,只使用从第0周到第8周的数据时间点,同时保持"治疗前"的箱线图在pre和post时间点。

I have the following dataset:

我有以下的数据集:

timepoint <- c(rep("pre", 5), rep("post", 5), rep(paste0("week", 0:8), each = 4))
timepoint <- c(rep(timepoint, 4))
treatment <- c(rep("before treatment", 10), rep(c("A", "B", "C","D"), 9))
treatment <- c(rep(treatment, 4))
setseed(1)
measurements <- c(runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
                  runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
                  runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4),runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
                  runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
                  runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4),runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
                  runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
                  runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4),runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
                  runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
                  runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4))
mydata <- data.frame(timepoint,treatment,measurements)
mydata$timepoint <- factor(mydata$timepoint, levels=c("pre","post","week0","week1","week2","week3","week4","week5","week6","week7","week8"))

and the plot

图表如下:

ggplot(mydata, aes(timepoint, measurements)) +
  geom_boxplot(aes(color=treatment, fill=treatment))

在箱线图的一部分添加geom_smooth。

在箱线图的一部分添加geom_smooth。

I tried to add geom_smooth to this plot, but I suspect this did not work because the x-axis was not numeric. I also tried to create another column of mydata$numeric.timepoint with "pre" and "post" as -2, -1 and others as numbers without "week", but I would like to keep the x-axis breaks as in the figure.

我尝试在这个图上添加geom_smooth,但我怀疑这没有起作用,因为x轴不是数字。我还尝试创建另一列mydata$numeric.timepoint,其中"pre"和"post"为-2,-1,其他没有"week"的数字,但我想保持x轴的刻度线与图中的一样。

英文:

I would like to add geom_smooth of each treatment with data only from week 0 to week 8 timepoints while keeping the "before treatment" boxplots in pre and post timepoints.
I have the following dataset:

timepoint &lt;- c(rep(&quot;pre&quot;, 5), rep(&quot;post&quot;, 5), rep(paste0(&quot;week&quot;, 0:8), each = 4))
timepoint&lt;-c(rep(timepoint,4))
treatment &lt;- c(rep(&quot;before treatment&quot;, 10),rep(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;,&quot;D&quot;), 9))
treatment&lt;-c(rep(treatment,4))
setseed(1)
measurements &lt;- c(runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4),runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4),runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4),runif(5, 1.3,1.5),runif(5, 0.2, 0.4),runif(5, 0.3, 0.5),
runif(5, 0.4, 0.8),runif(5, 0.5, 0.7),runif(5, 0.7, 1),
runif(5, 0.9, 1.2),runif(5, 1, 1.3),runif(6, 1.2, 1.4))
mydata&lt;-data.frame(timepoint,treatment,measurements)
mydata$timepoint&lt;-factor(mydata$timepoint, levels=c(&quot;pre&quot;,&quot;post&quot;,&quot;week0&quot;,&quot;week1&quot;,&quot;week2&quot;,&quot;week3&quot;,&quot;week4&quot;,&quot;week5&quot;,&quot;week6&quot;,&quot;week7&quot;,&quot;week8&quot;))

and the plot

ggplot(mydata, aes(timepoint, measurements))+
geom_boxplot(aes(color=treatment, fill=treatment))

在箱线图的一部分添加geom_smooth。

I tried to add geom_smooth to this plot, but I suspect this did not work because the x-axis was not numeric. I also tried to create another column of mydata$numeric.timepoint with "pre" and "post" as -2, -1 and others as numbers without "week", but I would like to keep the x-axis breaks as in the figure.

答案1

得分: 1

你可以这样实现你建议的布局:

library(dplyr)
library(ggplot2)

mydata %>%
  ggplot(aes(timepoint, measurements)) +
  geom_boxplot(aes(fill = treatment)) +
  geom_smooth(data = . %>%
                filter(!timepoint %in% c('pre', 'post')),
              mapping = aes(group = treatment, color = treatment), 
              se = FALSE, show.legend = FALSE
  )

为了达到图表的目的(可能是获取变化的想法,并比较不同处理的趋势),你可以尝试以下方式:

library(dplyr)
library(ggplot2)
library(forcats) ## 用于方便的因子重排序等功能

mydata %>%
  ## 重新排序时间点因子,以便'post'在最后:
  mutate(timepoint = fct_relevel(timepoint, c('pre', paste0('week', 0:8), 'post'))) %>%
  ggplot(aes(timepoint, measurements)) +
  ## 显示数据点,略微抖动并着色
  geom_jitter(aes(col = treatment), width = .1, height = 0, size = 1, alpha = .5,
              show.legend = FALSE
  ) +
  ## 显示一个平滑曲线 - 但只有CI带 - 每个处理
  geom_smooth(data = . %>%
                filter(!timepoint %in% c('pre', 'post')),
              aes(timepoint, measurements, group = treatment, fill = treatment),
              col = NA
  ) +
  ## 显示总趋势 - 但没有CI带 - 以便比较各种处理:
  geom_smooth(data = . %>%
                filter(!timepoint %in% c('pre', 'post')),
              aes(timepoint, measurements, group = 'all'),
              col = 'white', linewidth = .5
  )

附注:'pre'和'post'的值是否应该反过来标记?

在箱线图的一部分添加geom_smooth。

英文:

You can achieve your suggested layout like this:

library(dplyr)
library(ggplot2)
mydata %&gt;%
ggplot(aes(timepoint, measurements)) +
geom_boxplot(aes(fill = treatment)) +
geom_smooth(data = . %&gt;% filter(!timepoint %in% c(&#39;pre&#39;, &#39;post&#39;)),
mapping = aes(group = treatment, color = treatment), 
se = FALSE, show.legend = FALSE
)

which is quite overloaded:
在箱线图的一部分添加geom_smooth。

To serve the chart's purpose (presumably: get an idea of variation, and compare trends among treatments), you could try such:

library(dplyr)
library(ggplot2)
library(forcats) ## for convenient factor releveling etc.
mydata %&gt;%
## reorder factor timepoint so that &#39;post&#39; comes last:
mutate(timepoint = fct_relevel(timepoint, c(&#39;pre&#39;, paste0(&#39;week&#39;, 0:8), &#39;post&#39;))) %&gt;%
ggplot(aes(timepoint, measurements)) +
## show datapoints, slightly jittered and coloured
geom_jitter(aes(col = treatment), width = .1, height = 0, size = 1, alpha = .5,
show.legend = FALSE
) +
## show one smoother - but only CI band - per treatment
geom_smooth(data = . %&gt;% filter(!timepoint %in% c(&#39;pre&#39;, &#39;post&#39;)),
aes(timepoint, measurements, group = treatment, fill = treatment),
col = NA
) +
## show overall trend - but without CI band - to compare treatments against:
geom_smooth(data = . %&gt;% filter(!timepoint %in% c(&#39;pre&#39;, &#39;post&#39;)),
aes(timepoint, measurements, group = &#39;all&#39;),
col = &#39;white&#39;, linewidth = .5
)

sidenote: should 'pre' and 'post' values be labelled the other way round?

在箱线图的一部分添加geom_smooth。

答案2

得分: 0

<strike>如果您想在一个面板上绘制所有内容,这可能不容易实现。您需要计算平滑线,然后通过它们绘制线条(可能使用样条进行平滑曲线)。</strike> <sub>[用户I_O证明错误]</sub>

也许您想考虑facets。作为一个额外的好处,这也可以简化您的图表。

由于您的x变量是离散的,您需要指定一个组的美观性为1(或:group = treatment)

ggplot(mydata, aes(timepoint, measurements))+
  geom_boxplot(aes(color=treatment, fill=treatment)) +
  geom_smooth(aes(group = 1)) +
  facet_grid(~treatment)

### 由于您的数据,会有很多警告

在箱线图的一部分添加geom_smooth。<!-- -->

英文:

<strike>It won't be easily possible if you want to plot everything on one panel. You would need to calculate the smoothing lines and then plot the lines through them (probably using splines for smoother curves).</strike> <sub>[proven wrong by user I_O]</sub>

Maybe you want to consider facets. As a bonus, this is also de-cluttering your graph.

And due to the discrete nature of your x variable, you would need to specify a group aesthetic of 1 (or: group = treatment)

ggplot(mydata, aes(timepoint, measurements))+
  geom_boxplot(aes(color=treatment, fill=treatment)) +
  geom_smooth(aes(group = 1)) +
  facet_grid(~treatment)

### comes with lots of warnings due to your data

在箱线图的一部分添加geom_smooth。<!-- -->

huangapple
  • 本文由 发表于 2023年5月17日 21:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定