在R中使用ggplot()时,y轴比例限制不起作用

时间:2019-09-30 16:51:20

标签: r ggplot2

我有两个ggplot()图形p1p2彼此相邻显示。因此,我希望两者都具有相同的ylim()。这是我用于p1的脚本:

p1<-ggplot(data = dfnew, aes(x = Time, y = Proportion, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0, 
             position = myjit)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1, position = myjit) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1,
                position = myjit) + scale_shape_manual(values=c("41361´"=19,"41366´"=15)) +
  scale_color_manual(values = c("Day" = "black", 
                                "Night" = "black")) + 
  labs(shape="Collar ID") + 
  ylim(0.05, 0.4) + theme(legend.position = "none")+ 
  scale_y_continuous(breaks = scales::pretty_breaks(n = 5))+
  ggtitle("Feeding")

这是我用于p2的脚本:

p2<-ggplot(data = dfnew, aes(x = Area, y = Proportion,colour=Area, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0, 
             position = myjit)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1, position = myjit) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1,
                position = myjit) + scale_shape_manual(values=c("41361´"=19,"41365´"=17)) + scale_size_manual(values=c(2,2)) +
  scale_color_manual(values = c("SNP" = "brown", 
                                "LGCA" = "darkgoldenrod2")) + labs(shape="Collar ID") + 
  ylim(0.05, 0.4) +
  theme(legend.text=element_text(size=18))+
  theme(legend.title = element_text(size=18)) + 
  scale_y_continuous(breaks = scales::pretty_breaks(n = 5))+
  ggtitle(" ") + theme(legend.position = "none")

但是,如下图所示,y轴不匹配。不仅如此,即使指定scale_y_continuous(breaks = scales::pretty_breaks(n = 5)),我在两个y轴上都没有得到相同的滴答数: enter image description here

希望至少有人可以让我走上正确的轨道,以防万一。如果有帮助,我将以下数据用于p1

> dput(dfnew)
structure(list(Proportion = c(0.242, 0.216, 0.29, 0.256), Lower = c(0.173, 
0.152, 0.214, 0.186), Upper = c(0.329, 0.296, 0.381, 0.342), 
    Time = c("Day", "Night", "Day", "Night"), Collar = c("41361´", 
    "41361´", "41366´", "41366´"), ymin = c(0.069, 0.064, 0.076, 
    0.07), ymax = c(0.571, 0.512, 0.671, 0.598), linegroup = c("Day 41361´", 
    "Night 41361´", "Day 41366´", "Night 41366´")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -4L))

p2

> dput(dfnew)
structure(list(Proportion = c(0.181, 0.289, 0.099, 0.224), Lower = c(0.148, 
0.242, 0.096, 0.217), Upper = c(0.219, 0.341, 0.104, 0.232), 
    Area = c("LGCA", "SNP", "LGCA", "SNP"), Collar = c("41361´", 
    "41361´", "41365´", "41365´"), ymin = c(0.033, 0.047, 0.003, 
    0.00700000000000001), ymax = c(0.4, 0.63, 0.203, 0.456), 
    linegroup = c("LGCA 41361´", "SNP 41361´", "LGCA 41365´", 
    "SNP 41365´")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-4L))

1 个答案:

答案 0 :(得分:0)

您可以这样解决此问题,而不是像这样ylim()使用coord_cartesian来解决:+coord_cartesian(ylim = c(0.05, 0.4), expand = TRUE)