使用ggplot在facet上创建零件固定,无零件的轴限制?

时间:2012-10-04 16:39:02

标签: r ggplot2

我想使用ggplot2创建一个多面图,其中y轴的最小限制将固定(比如0),最大限制将由facet中的数据确定(就像{ {1}}。我希望以下类似的东西可行,但没有这样的运气:

scales="free_y"

对我而言,这会引发错误(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) )并不令人感到意外,但我无法想到采用这种问题的策略。

1 个答案:

答案 0 :(得分:8)

在您的情况下,以下任何一种都可以使用:

p + expand_limits(y=0)

p + aes(ymin=0)