如何在 ggplot 中指定 y 轴不从 0 开始?

3

如何在我的图表中打破y轴不从0开始?我猜用ggplot无法实现。还有其他解决方案吗?

我想要这个效果:enter image description here

我的ggplot:

ggplot(allEvents %>% dplyr::filter(FixationID>1,FixationID<15), aes(FixationID, Fixation)) +
  geom_line(stat="summary", fun.y=mean, position="identity") + theme_bw() + stat_summary(fun.data=mean_se)+   
  labs (x = "Ordinal fixation number", y = "Fixation duration (s)") +  
  scale_y_continuous (name="Fixation duration (s) ", limits=c(0.1, 0.4)) + 
  scale_x_continuous (name="Ordinal fixation number", breaks=c(5,10,15,20)) + 
  geom_smooth(method=lm)

我的数据摘要:

structure(list(Fixation = c(0.383, 0.185, NA, 0.312, NA, 0.328, 
NA, 0.259, NA, 0.335), FixationID = c(1, 2, NA, 3, NA, 4, NA, 
5, NA, 6)), .Names = c("Fixation", "FixationID"), row.names = c(NA, 
10L), class = "data.frame")

2
之前有一个非常类似的问题在这里:https://dev59.com/eLroa4cB1Zd3GeqPgDkI。简而言之,ggplot2不支持不连续的轴(断点),但是如果你努力尝试,你可以制作它们。 - teunbrand
1个回答

0

正如@teunbrand的评论所提到的,ggplot2不允许轴线中断,因此要实现这一点确实很棘手。

或者,您可以在base r绘图中绘制图形,并使用plotrix包中的axis.break函数。

(注:我在示例中使用了dplyr中的between函数来选择您感兴趣的数据子集。您的数据集在我的示例中称为df)。

library(dplyr) 
library(plotrix)

with(subset(df, between(FixationID,2,15)),
     plot(x = FixationID, y = Fixation, type = "b", ylim = c(0.1,0.4), pch = 16))
with(subset(df, between(FixationID,2,15)),
     abline(lm(Fixation~FixationID),col = "blue"))
axis.break(axis = 2, breakpos = 0.1, style="slash")

enter image description here

这回答了你的问题吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接