facet_wrap ylim

Published by onesixx on

https://chrischizinski.github.io/rstats/using_geom_blank/

https://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2

set.seed(666)
foo.dat<-rbind(data.frame(group="A", x = runif(50), y=rnorm(50,mean=5)),
               data.frame(group="B", x = runif(50), y=rnorm(50,mean=5,sd=3)+20),
               data.frame(group="C", x = runif(50), y=rnorm(50,mean=5,sd=5)+30))

## Plot it in ggplot
ggplot() + 
  geom_point(data=foo.dat,aes(x=x,y=y,colour=group),size=4) + 
  facet_wrap(~group,scales="free_y") + 
  ## using scales="free_y" allows the y-axis to vary while keeping x-axis constant among plots
  theme_bw()


ggplot() + 
  geom_point(data = foo.dat, aes(x = x, y = y, colour = group), size = 4) + 
  facet_wrap(~group, scales = "free_y") + 
  coord_cartesian(ylim = c(0, 50)) + 
  theme_bw()

ggplot() + 
  geom_point(data = foo.dat, aes(x = x, y = y, colour = group), size = 4) + 
  facet_wrap(~group, scales = "free_y") + 
  expand_limits(y = 0) + scale_y_continuous(expand = c(0, 0)) +
  theme_bw()


blank_data <- data.frame(group = c("A", "A", "B", "B", "C", "C"), y = c(2, 8, 10, 40, 20, 50))
ggplot() + 
  geom_point(data = foo.dat, aes(x = x, y = y, colour = group), size = 4) + 
  geom_blank(data = blank_data, aes(x = 0, y = y)) + 
  facet_wrap(~group, scales = "free_y") + 
  theme_bw()

 

val_limit <-lapply(searching_sensor, function(x) { range(draw_data[variable==x]$val)})
names(val_limit) <- searching_sensor


draw_data <- dtrace[dtrace$variable %in% searching_sensor, ]


blank_data <- data.frame(group = c("A", "A", "B", "B", "C", "C"), y = c(2, 8, 10, 40, 20, 50))
ggplot() + 
  geom_point(data = foo.dat, aes(x = x, y = y, colour = group), size = 4) + 
  geom_blank(data = blank_data, aes(x = 0, y = y)) + 
  facet_wrap(~group, scales = "free_y") + 
  theme_bw()


val_limit <-lapply(searching_sensor, function(x) { range(dtrace[variable==x]$val)}) %>% unlist

blank_data <- data.frame(
 group = rep(searching_sensor, each=2),
 y = lapply(searching_sensor, function(x) { range(dtrace[variable==x]$val)}) %>% unlist)

 

show.legend=FALSE

 

 

Categories: R ggplot2

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x