R 中 ggplot2 的设置比例中断问题

问题描述 投票:0回答:1

我想为两组设计中的纵向数据绘制线图。 由于组平均值范围为 30-43,我希望我的图从 28 开始。 尽管如此,我希望 y 轴的刻度从 0 开始并插入一个断点。 我从

scale_y_break()
找到了
ggbreak
函数,但遗憾的是输出看起来很奇怪,因为 y 轴刻度直到 28 才会显示,之间添加了很多间距,并且可以看到图形的副本。

这是一个可重现的示例:

plotdata <- data.frame(
  Group = c("Intervention", "Intervention", "Intervention", "Control", "Control", "Control"),
  Mean = c(30, 35, 43, 31, 33, 34),
  Std_Error = c(0.66779, 0.91275, 1.11335, 0.73653, 0.82869, 0.91173),
  Timepoint = c("Baseline", "6 Weeks", "12 Weeks", "Baseline", "6 Weeks", "12 Weeks")
)

plotdata$Timepoint <- factor(plotdata$Timepoint, levels = c("Baseline", "6 Weeks", "12 Weeks"))

plot <- ggplot(plotdata, aes(x = Timepoint, y = Mean, color = Group, group = Group)) +
  geom_errorbar(aes(ymin = Mean - Std_Error, ymax = Mean + Std_Error), width = 0.1, size = 0.8,  color = "grey60") + 
  geom_line(linewidth = 1.8) +  
  geom_point(size = 2.5) +  
  labs(x = "Timepoint", y = "Outcome", title = "Trajectory") +
  scale_color_manual(values = c("Intervention" = "#D81A61", "Control" = "#1E88E5")) + 
  theme_bw() +  
  theme(
    axis.title.x = element_text(margin = margin(t = 15), face = "italic"),  
    axis.title.y = element_text(margin = margin(r = 15), face = "italic"), 
    axis.text.x = element_text(margin = margin(t = 5)),  
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank(), 
    panel.border = element_blank(), 
    plot.title = element_text(size = 14, face = "bold")
  )

plot
plot +  scale_y_break(
  breaks = c(0, 28)
)  

结果如下所示: 没有音阶中断: enter image description here 随着音阶中断: enter image description here

有人可以帮我解决这个问题吗? 提前非常感谢!

r ggplot2 plot graph visualization
1个回答
0
投票

我永远不会这样做,但如果你必须这样做:

设置你的scale_y以包含0, 45范围,并明确设置分隔符以包含0,如下所示

scale_y_continuous(limits = c(0, 45), breaks  = c(0, seq(25, 45, 5))) +

然后使用

plot +  scale_y_break(
  breaks = c(1, 28),
  scales = "fixed"
)    
© www.soinside.com 2019 - 2024. All rights reserved.