![](/img/trans.png)
[英]ggplot2 - How to boxplot two variables with different scales on same plot using a primary and secondary y-axis?
[英]How to make y-axis scales same size for each facet in ggplot2?
以下是该场景的示例:
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)))
这将生成以下数据框:
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
我想绘制V vs t并用C1和C2分割刻面。 我希望每个方面的max(V)-min(V)相同,但max(V)和min(V)会有所不同,具体取决于每行图的数据范围。 这是我到目前为止的地方:
ggplot(data=df) + geom_point(aes(x=t, y=V)) + facet_grid(C2~C1)
max(V)-min(V)具有相同的尺寸,浪费了大量的y轴,并且每个面板中的额外范围使趋势变得模糊。 另一种选择是:
ggplot(data=df) + geom_point(aes(x=t, y=V)) + facet_grid(C2~C1, scales='free_y')
在这种情况下,max(V)-min(V)=第1行图表为{6,第2行图表为3}。
也没有办法为每一行指定限制,就像我们能够通过将带有范围的列表传递给ylim参数一样在格中进行。
您可以向facet_grid()
添加space="free_y"
,以确保每行的值之间的空间相同。
ggplot(data=df) + geom_point(aes(x=t, y=V)) +
facet_grid(C2~C1, scales='free_y',space="free_y")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.