如何使用 ggplot2 更改刻面比例?

问题描述

我正在尝试更改某些面板的比例以获得更好的视图。在 facet_grid 中,scales 已经是 "free",但是,我需要为与第一个范围相对应的行使用不同的比例。有人知道怎么做吗?

library (ggplot2)

Source <- c(rep("Water",12),rep("Oil",12))
Range <- rep((c(rep("First",4),rep("Second",8))),2)
Xaxis <- c(0,1,2,5,10,20,30,40,40)
Yaxis <- c(0,40)

DF <- data.frame(Source,Range,Xaxis,Yaxis)

ggplot(data = DF,aes(x = Xaxis,y = Yaxis)) +
  geom_smooth(method = "lm") +
  geom_point() +
  facet_grid(Range~Source,scales = "free")

enter image description here

我希望前排看起来像这样:

enter image description here

解决方法

另一种方法是使用 facet_wrap() 而不是 facet_grid(),例如

library(tidyverse)

Source <- c(rep("Water",12),rep("Oil",12))
Range <- rep((c(rep("First",4),rep("Second",8))),2)
Xaxis <- c(0,1,2,5,10,20,30,40,40)
Yaxis <- c(0,40)

DF <- data.frame(Source,Range,Xaxis,Yaxis)

ggplot(data = DF,aes(x = Xaxis,y = Yaxis)) +
  geom_smooth(method = "lm") +
  geom_point() +
  facet_wrap(Range~Source,scales = "free")

example_1.png