我知道Change color median line ggplot geom_boxplot()年内就有类似问题的解决方案了.

然而,它在我的情况下不起作用,我也在使用facet_wrap.

ggplot(data, aes(x = year, y = value)) +
  geom_boxplot() +
  facet_wrap(~month, ncol = 12)

这给出了以下可视化:

enter image description here

我需要把中间线变成蓝色.

我试过了(如前面提到的答案:

p <- ggplot(data, aes(x = year, y = value)) +
  geom_boxplot() +
  facet_wrap(~month, ncol = 12)
dat <- ggplot_build(p)$data[[1]]
p + geom_segment(
  data=dat, aes(x=xmin, xend=xmax,
                y=middle, yend=middle), colour="blue")

这就是输出(它显然没有将线条分布在各个方面):

enter image description here

我使用的数据:

data <- structure(list(date = structure(c(2009, 2009.08333333333, 2009.16666666667, 
2009.25, 2009.33333333333, 2009.41666666667, 2009.5, 2009.58333333333, 
2009.66666666667, 2009.75, 2009.83333333333, 2009.91666666667, 
2010, 2010.08333333333, 2010.16666666667, 2010.25, 2010.33333333333, 
2010.41666666667, 2010.5, 2010.58333333333, 2010.66666666667, 
2010.75, 2010.83333333333, 2010.91666666667, 2011, 2011.08333333333, 
2011.16666666667, 2011.25, 2011.33333333333, 2011.41666666667, 
2011.5, 2011.58333333333, 2011.66666666667, 2011.75, 2011.83333333333, 
2011.91666666667, 2012, 2012.08333333333, 2012.16666666667, 2012.25, 
2012.33333333333, 2012.41666666667, 2012.5, 2012.58333333333, 
2012.66666666667, 2012.75, 2012.83333333333, 2012.91666666667, 
2013, 2013.08333333333, 2013.16666666667, 2013.25, 2013.33333333333, 
2013.41666666667, 2013.5, 2013.58333333333, 2013.66666666667, 
2013.75, 2013.83333333333, 2013.91666666667, 2014, 2014.08333333333, 
2014.16666666667, 2014.25, 2014.33333333333, 2014.41666666667, 
2014.5, 2014.58333333333, 2014.66666666667, 2014.75, 2014.83333333333, 
2014.91666666667, 2015, 2015.08333333333, 2015.16666666667, 2015.25, 
2015.33333333333, 2015.41666666667, 2015.5, 2015.58333333333, 
2015.66666666667, 2015.75, 2015.83333333333, 2015.91666666667, 
2016, 2016.08333333333, 2016.16666666667, 2016.25, 2016.33333333333, 
2016.41666666667, 2016.5, 2016.58333333333, 2016.66666666667, 
2016.75, 2016.83333333333, 2016.91666666667, 2017, 2017.08333333333, 
2017.16666666667, 2017.25, 2017.33333333333, 2017.41666666667, 
2017.5, 2017.58333333333, 2017.66666666667, 2017.75, 2017.83333333333, 
2017.91666666667, 2018, 2018.08333333333, 2018.16666666667, 2018.25, 
2018.33333333333, 2018.41666666667, 2018.5, 2018.58333333333, 
2018.66666666667, 2018.75, 2018.83333333333, 2018.91666666667, 
2019, 2019.08333333333, 2019.16666666667, 2019.25, 2019.33333333333, 
2019.41666666667, 2019.5, 2019.58333333333, 2019.66666666667, 
2019.75, 2019.83333333333, 2019.91666666667, 2020, 2020.08333333333, 
2020.16666666667, 2020.25, 2020.33333333333, 2020.41666666667, 
2020.5, 2020.58333333333, 2020.66666666667, 2020.75, 2020.83333333333, 
2020.91666666667, 2021, 2021.08333333333, 2021.16666666667, 2021.25, 
2021.33333333333, 2021.41666666667, 2021.5, 2021.58333333333, 
2021.66666666667, 2021.75, 2021.83333333333, 2021.91666666667, 
2022, 2022.08333333333, 2022.16666666667, 2022.25, 2022.33333333333, 
2022.41666666667, 2022.5, 2022.58333333333, 2022.66666666667, 
2022.75, 2022.83333333333, 2022.91666666667, 2023, 2023.08333333333, 
2023.16666666667, 2023.25, 2023.33333333333, 2023.41666666667, 
2023.5, 2023.58333333333, 2023.66666666667, 2023.75, 2023.83333333333, 
2023.91666666667, 2024), class = "yearmon"), value = c(1491, 
1221, 2519, 1981, 610, 107, 504, 562, 583, 942, 400, 123, 52, 
0, 95, 102, 131, 358, 719, 805, 652, 557, 681, 298, 359, 6010, 
17062, 10137, 10680, 5351, 3103, 7308, 2262, 511, 707, 771, 479, 
235, 875, 359, 1422, 1904, 1630, 2421, 1272, 1816, 2015, 723, 
217, 232, 1177, 1922, 1114, 3563, 6964, 7743, 9757, 8566, 1362, 
2681, 2171, 3335, 5550, 15679, 14597, 22778, 24107, 25031, 26125, 
15264, 9295, 6732, 3615, 4354, 2283, 16061, 21229, 22886, 23205, 
22612, 15929, 8916, 3219, 9637, 5273, 3828, 9675, 9149, 19957, 
22344, 23498, 21294, 16979, 27390, 13554, 8435, 4470, 8882, 10853, 
12870, 22997, 23461, 11460, 3914, 6266, 5923, 5583, 2283, 4150, 
1065, 977, 3083, 3794, 3244, 1811, 1822, 852, 1040, 1091, 556, 
199, 60, 414, 271, 1143, 1785, 1276, 1883, 2793, 2185, 1411, 
583, 1774, 1649, 375, 723, 1740, 2228, 7232, 5442, 4262, 3374, 
5348, 1526, 1039, 4059, 2385, 1479, 5703, 5855, 8727, 10123, 
6877, 7220, 9589, 4668, 3035, 2439, 1358, 3930, 8751, 8148, 13803, 
16896, 13710, 13523, 9177, 10791, 4963, 9466, 13267, 14597, 8154, 
15213, 23456, 25756, 19209, 10321, 8335, 9977, 2293), year = c(2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 
2022L, 2022L, 2022L, 2022L, 2022L, 2023L, 2023L, 2023L, 2023L, 
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2024L
), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L)), row.names = c(NA, -181L), class = "data.frame")

WORKING CODE

多亏了zephryl's answer,我才能达到我想要的.

使用刻面,这是最终的情节(有一些改进):

p <- ggplot(data, aes(y = value)) +
  geom_boxplot() +
  facet_wrap(~factor(month.name[month], levels = month.name), ncol = 12) +
  scale_x_continuous(labels = NULL, breaks = NULL) +
  labs(x = NULL, y = "ibc",
       title = paste(min(data$year), max(data$year), sep = "-"))

dat <- ggplot_build(p)$data[[1]]
dat$month <- sort(unique(data$month))[dat$PANEL]

p + geom_segment(
  data=dat, 
  aes(x=xmin, xend=xmax,
      y=middle, yend=middle),
  colour="blue",
  linewidth = 1
)

这提供了:

enter image description here

然而,正如zephryl所注意到的,我并不真正需要方面,所以我的问题归结为可以用the aforementioned solution解决的问题.这是没有方面的代码:

p <- ggplot(data, aes(x = factor(month.name[month], levels = month.name), y = value)) +
  geom_boxplot() +
  labs(x = NULL, y = "ibc",
       title = paste(min(data$year), max(data$year), sep = "-"))

dat <- ggplot_build(p)$data[[1]]
p + geom_segment(
  data=dat, 
  aes(x=xmin, xend=xmax,
      y=middle, yend=middle),
  colour="blue",
  linewidth = 1
)

这是我最后的 Select :

enter image description here

推荐答案

ggbuild()在一个名为PANEL的列中返回面编号.您可以通过索引到原始分面变量的唯一值来添加新的分面列.

library(ggplot2)

p <- ggplot(data, aes(x = year, y = value)) +
  geom_boxplot() +
  facet_wrap(~month, ncol = 12)

dat <- ggplot_build(p)$data[[1]]
dat$month <- sort(unique(data$month))[dat$PANEL]

p + geom_segment(
  data=dat, 
  aes(x=xmin, xend=xmax,
      y=middle, yend=middle),
  colour="blue",
  linewidth = 1
)

注:

  • 在这种特殊情况下,因为你的原始刻面变量已经由连续数字组成,所以你可以只做dat$month <- dat$PANEL.但这是一个有点特殊的情况.
  • year作为x轴实际上没有意义,至少对于这个示例数据是这样的,因为您每年和每月只有一个值.出于同样的原因,这里并不真正需要分面--您可以将month映射到group:
data$month <- factor(data$month)
p <- ggplot(
    data, 
    aes(x = month, y = value, group = month)
  ) +
  geom_boxplot() 

dat <- ggplot_build(p)$data[[1]]

p + geom_segment(
  data=dat, 
  aes(x=xmin, xend=xmax,
      y=middle, yend=middle,
      group=group),
  colour="blue",
  linewidth = 1
)

R相关问答推荐

卸载安装了BRM的模型发出的警告

使用rlang s arg_match判断函数输入列表

更改Heatmap Annotation对象的名称

从嵌套列表中智能提取线性模型系数

在R中无法读入具有Readxl和lApply的数据集

条形图和在Ploly中悬停的问题

矩阵的堆叠条形图,条形图上有数字作为标签

如何通过ggplot2添加短轴和删除长轴?

将二进制数据库转换为频率表

当我们有多个反斜杠和/特殊字符时使用Gsubing

将项粘贴到向量中,并将它们分组为x的倍数,用空格分隔

如何使用同比折线图中的个别日

我是否可以使用多个变异项来构建顺序列(标记多个问题)

`-`是否也用于数据帧,有时使用引用调用?

是否可以将线性模型的p值添加到tbl_summary中

为R中的16组参数生成10000个样本的有效方法是什么?

向R中的数据帧添加一列,该列统计另一列中每个唯一值的二进制观测值的数量

随机将数据帧中特定列上的某些行设置为NA

如何在GALT包的函数&geom_x样条线中调整线宽

通过分析特定列中的字符串在数据框中创建新的行和列