문제

Here is an example of the scenario:

df<-data.frame(t=1:8, V=c(1:4,seq(40,46, 2)), C1=rep(c('A','B'), 4), C2=rep(c('C','D'), c(4,4)))

This produces the following data frame:

  t  V C1 C2
1 1  1  A  C
2 2  2  B  C
3 3  3  A  C
4 4  4  B  C
5 5 40  A  D
6 6 42  B  D
7 7 44  A  D
8 8 46  B  D

I want to plot V vs t and split facets by C1 and C2. I want the max(V)-min(V) to be the same for each facet, but max(V) and min(V) would be different, depending on the data range for each row of plots. Here is where I am so far:

ggplot(data=df) + geom_point(aes(x=t, y=V)) + facet_grid(C2~C1)

default version

max(V)-min(V) are of the same size, plenty of y-axis is wasted and trends are obscured by the extra range in each panel. The other option is:

ggplot(data=df) + geom_point(aes(x=t, y=V)) + facet_grid(C2~C1, scales='free_y')

free_y version

in which case, max(V)-min(V) = {6 for the 1st row of plots, and 3 for the 2nd row of plots}.

There is also no way to specify limits for each row, like we are able to do in lattice by passing a list with ranges to each panel to the ylim parameter.

도움이 되었습니까?

해결책

You can add space="free_y" to facet_grid() to ensure that space between values are the same for each row.

ggplot(data=df) + geom_point(aes(x=t, y=V)) + 
        facet_grid(C2~C1, scales='free_y',space="free_y")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top