2012-10-04 9 views
6

Mi piacerebbe creare un grafico sfaccettato usando ggplot2 in cui verrà fissato il limite minimo dell'asse y (diciamo a 0) e il limite massimo sarà determinato dai dati nel facet (come lo è quando scales="free_y". Speravo che qualcosa di simile al seguente potrebbe funzionare, ma senza fortuna del genere:Creare limiti dell'asse parte-fissa, senza parti su facet con ggplot?

library(plyr) 
library(ggplot2) 

#Create the underlying data 
l <- gl(2, 10, 20, labels=letters[1:2]) 
x <- rep(1:10, 2) 
y <- c(runif(10), runif(10)*100) 
df <- data.frame(l=l, x=x, y=y) 

#Create a separate data frame to define axis limits 
dfLim <- ddply(df, .(l), function(y) max(y$y)) 
names(dfLim)[2] <- "yMax" 
dfLim$yMin <- 0 

#Create a plot that works, but has totally free scales 
p <- ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap(~l, scales="free_y") 
#Add y limits defined by the limits dataframe 
p + ylim(dfLim$yMin, dfLim$yMax) 

non è troppo sorprendente per me che questo genera un errore (length(lims) == 2 is not TRUE), ma non riesco a pensare a una strategia per iniziare a questo problema

risposta

7

Nel tuo caso, uno dei seguenti funzionerà:

p + expand_limits(y=0) 

p + aes(ymin=0) 
Problemi correlati