ggplot:根据月份调整轴 space
ggplot: adjust axis space according to months
已更新:
如何修改以下 ggplot 以获得以下内容:
X 轴应按月调整,即基线到 6 个月应该更短,6 个月到 24 个月应该更长,具体取决于时间点之间的月数。 (下面的解决方案)
在 24 个月到 48 个月之间的 X 轴上包括一个中断(比如在 36 个月时)。
通过减少基线前和 48 个月后的白色 space 使图变大。 (下面的解决方案)
感谢您的帮助!
fat <- structure(list(study_group = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L), .Label = c("Intervention group", "Control group"
), class = "factor"), mean1 = c(37.02, 32.95, 34.18, 36.38, 37.1,
37.27, 37.64, 38.22), se1 = c(0.22, 0.3, 0.35, 0.38, 0.23, 0.35,
0.28, 0.32), timepoint1 = c(0, 6, 24, 48, 0, 6, 24, 48)), row.names = c(3L,
7L, 11L, 15L, 19L, 23L, 27L, 31L), class = "data.frame")
ggplot(fat, aes(timepoint1, mean1, colour = study_group)) +
geom_point(size=2) +
geom_line(size=1.2) +
geom_errorbar(aes(ymin=mean1-se1, ymax=mean1+se1), width=1, size=1) +
scale_x_continuous(
breaks = c(0, 6, 24, 36, 48),
labels = c("Baseline", "6 months", "24 months", "36 months", "48 months"),
expand = c(0.05, 0))
最好的办法可能是将月数编码为 numeric
向量而不是 factor/character。然后您可以设置中断并重新标记 x 轴。两侧的空白由比例尺的 expand
参数控制。下面的虚拟数据示例:
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
df <- data.frame(
x = rep(c(0, 6, 24, 48), 2), # <- encode as numeric
ymin = c(1,2,3,4,1,3,5,7),
y = c(2,3,4,5,2,4,6,8),
ymax = c(3,4,5,6,5,7,8,9),
group = rep(c("A", "B"), each = 4)
)
ggplot(df, aes(x, y, colour = group)) +
geom_line() +
geom_errorbar(aes(ymin = ymin, ymax = ymax)) +
# Details for the x-axis
scale_x_continuous(
breaks = c(0, 6, 24, 36, 48),
labels = c("Baseline", "6 months", "24 months", "36 months", "48 months"),
expand = c(0.02, 0)
)
作为在 SO 上提问的建议,请尝试包含一些相关(虚拟)数据并省略与手头问题无关的代码(例如主题设置、y 尺度、装饰等)。这使得更容易进入问题的核心,而不会被额外的代码分散注意力或不必生成虚拟数据。
已更新:
如何修改以下 ggplot 以获得以下内容:
X 轴应按月调整,即基线到 6 个月应该更短,6 个月到 24 个月应该更长,具体取决于时间点之间的月数。 (下面的解决方案)
在 24 个月到 48 个月之间的 X 轴上包括一个中断(比如在 36 个月时)。
通过减少基线前和 48 个月后的白色 space 使图变大。 (下面的解决方案)
感谢您的帮助!
fat <- structure(list(study_group = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L), .Label = c("Intervention group", "Control group"
), class = "factor"), mean1 = c(37.02, 32.95, 34.18, 36.38, 37.1,
37.27, 37.64, 38.22), se1 = c(0.22, 0.3, 0.35, 0.38, 0.23, 0.35,
0.28, 0.32), timepoint1 = c(0, 6, 24, 48, 0, 6, 24, 48)), row.names = c(3L,
7L, 11L, 15L, 19L, 23L, 27L, 31L), class = "data.frame")
ggplot(fat, aes(timepoint1, mean1, colour = study_group)) +
geom_point(size=2) +
geom_line(size=1.2) +
geom_errorbar(aes(ymin=mean1-se1, ymax=mean1+se1), width=1, size=1) +
scale_x_continuous(
breaks = c(0, 6, 24, 36, 48),
labels = c("Baseline", "6 months", "24 months", "36 months", "48 months"),
expand = c(0.05, 0))
最好的办法可能是将月数编码为 numeric
向量而不是 factor/character。然后您可以设置中断并重新标记 x 轴。两侧的空白由比例尺的 expand
参数控制。下面的虚拟数据示例:
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
df <- data.frame(
x = rep(c(0, 6, 24, 48), 2), # <- encode as numeric
ymin = c(1,2,3,4,1,3,5,7),
y = c(2,3,4,5,2,4,6,8),
ymax = c(3,4,5,6,5,7,8,9),
group = rep(c("A", "B"), each = 4)
)
ggplot(df, aes(x, y, colour = group)) +
geom_line() +
geom_errorbar(aes(ymin = ymin, ymax = ymax)) +
# Details for the x-axis
scale_x_continuous(
breaks = c(0, 6, 24, 36, 48),
labels = c("Baseline", "6 months", "24 months", "36 months", "48 months"),
expand = c(0.02, 0)
)
作为在 SO 上提问的建议,请尝试包含一些相关(虚拟)数据并省略与手头问题无关的代码(例如主题设置、y 尺度、装饰等)。这使得更容易进入问题的核心,而不会被额外的代码分散注意力或不必生成虚拟数据。