r - 强制 y 轴从 0 开始,插入“break”,并使用 ggplot2 有一个大的 y 轴
问题描述
我仍在努力最终确定一个可复制的数字以供出版。审阅者希望看到下图的 y 轴从 0 开始并包括换行符“//”。y 轴不仅需要非常大(想想,1500 单位),而且还需要非常紧密地放大(想想,300 单位)。这使得审阅者希望我们添加一个换行符来表示我们的轴从 0 开始,但继续。
我想要的示例(注意 y 轴;这是在类似图中的 powerpoint 中手动完成的):
我的代码:
ggplot(data = quad2,
aes(x, predicted, group = group)) +
geom_point(aes(shape = group), size = 6) +
scale_shape_manual(values=c(19, 1)) +
geom_line(size = 2,
aes(linetype = group),
color = "black") +
scale_linetype_manual(values = c("solid", "dashed")) +
geom_linerange(size = 1,
aes(ymin = predicted - conf.low,
ymax = predicted + conf.high),
color = "black",
alpha = .8) +
geom_segment(aes(xend = x,
yend = ifelse(group == "Control", conf.high, conf.low)),
arrow = arrow(angle = 90), color = "red")+
labs(x = "Time",
y = expression(bold("QUAD Volume (cm"^"3"*")")),
linetype = "",
shape = "") + #Legend title
scale_y_continuous(limits =c(1500, 2000))
可重现的数据:
dput(quad2)
structure(list(x = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L), .Label = c("PRE", "MID1", "MID2", "MID3", "POST"), class = "factor"),
predicted = c(1666.97185871754, 1660.27445165342, 1743.2831065274,
1678.48945165342, 1788.50605542978, 1637.40907049806, 1807.55826371403,
1639.78265640012, 1865.8766220711, 1652.91070173056), std.error = c(88.8033117577884,
91.257045996107, 92.9973963841595, 95.3834973421298, 95.0283457128716,
97.3739053806999, 95.6466346849776, 97.9142418717957, 93.3512943191676,
95.5735155125126), conf.low = c(0, 91.257045996107, 0, 95.3834973421298,
0, 97.3739053806999, 0, 97.9142418717957, 0, 95.5735155125126
), conf.high = c(88.8033117577884, 0, 92.9973963841595, 0,
95.0283457128716, 0, 95.6466346849776, 0, 93.3512943191676,
0), group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("Intervention", "Control"), class = "factor")), class = "data.frame", row.names = c(NA,
-10L))
解决方案
绘制不连续轴是有原因的,这个原因是你应该尽可能避免这样做。虽然我不同意你的评论者,但如果你真的想要一个 y 轴中断,你可以对底层的网格图形感到沮丧和肮脏。
首先制作你的情节。我唯一添加的是 y 轴格式和轴线主题。我们只需将底部刻度标记为“0”。
plt <- ggplot(data = quad2,
aes(x, predicted, group = group)) +
geom_point(aes(shape = group), size = 6) +
scale_shape_manual(values=c(19, 1)) +
geom_line(size = 2,
aes(linetype = group),
color = "black") +
scale_linetype_manual(values = c("solid", "dashed")) +
geom_linerange(size = 1,
aes(ymin = predicted - conf.low,
ymax = predicted + conf.high),
color = "black",
alpha = .8) +
geom_segment(aes(xend = x,
yend = ifelse(group == "Control", conf.high, conf.low)),
arrow = arrow(angle = 90), color = "red")+
labs(x = "Time",
y = expression(bold("QUAD Volume (cm"^"3"*")")),
linetype = "",
shape = "") + #Legend title
scale_y_continuous(limits =c(1400, 2000),
breaks = seq(1400, 2000, by = 200),
labels = c(0, seq(1600, 2000, by = 200)),
expand = c(0,0,0.05,0)) +
theme(axis.line = element_line())
然后,我们将它做成一个 gtable 并抓取 y 轴线:
gt <- ggplotGrob(plt)
is_yaxis <- which(gt$layout$name == "axis-l")
yaxis <- gt$grobs[[is_yaxis]]
# You should grab the polyline child
yline <- yaxis$children[[1]]
现在我们可以根据需要编辑该行:
yline$x <- unit(rep(1, 4), "npc")
yline$y <- unit(c(0, 0.1, 1, 0.15), "npc")
yline$id <- c(1, 1, 2, 2)
yline$arrow <- arrow(angle = 90)
将其放回 gtable 对象并绘制它:
yaxis$children[[1]] <- yline
gt$grobs[[is_yaxis]] <- yaxis
# grid plotting syntax
grid.newpage(); grid.draw(gt)
您可以在您认为合适的行编辑步骤中做出风格选择。
推荐阅读
- javascript - 使用 Bootstrap 4 在下拉菜单中更改图像值
- regex - 将字符串转换为正则表达式模式
- json - 从数组和对象中提取字段的值
- html - 如何使用 javascript 和递归查询嵌套级别 DOM
- javascript - 如何修复'在猫头鹰轮播中的图像之间切换内容移动到顶部'
- javascript - 窗口加载未显示正确的其他消息
- angular - 新的 Angular 项目无法正确渲染
- excel - 使用 Excel VBA 从两个列表中构建一个列表
- wordpress - 匹配 htaccess 重定向
- swift - 如何在 SwiftUI 中的 TabView 的 tabItem 上显示徽章编号?