首页 > 解决方案 > 如何使用 ggplot2 更改刻面比例?

问题描述

我正在尝试更改某些面板的比例以获得更好的视图。facet_grid但是,在scalesare already"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,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)
Yaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,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")

在此处输入图像描述

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

在此处输入图像描述

标签: rggplot2scaleaxisfacet

解决方案


另一种方法是使用,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,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)
Yaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,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")

示例_1.png


推荐阅读