简体   繁体   中英

y-axis scale limits not working when using ggplot() in R

I have two ggplot() graphs p1 and p2 that I'm displaying next to each other. For that reason, I would like both to have the same ylim() . Here's the script I'm using for 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")

And here's the script I'm using for 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")

However, as seen on the image below, y axis don't match. Not only that, but even when specifying scale_y_continuous(breaks = scales::pretty_breaks(n = 5)) I don't get the same number of tick in both y axis: 在此处输入图像描述

Hope at least somebody can set me on the right track, in case this is fixable. If it helps, I used the following data for 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))

and for 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))

You can fix this by instead of ylim() using coord_cartesian like this: +coord_cartesian(ylim = c(0.05, 0.4), expand = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM