简体   繁体   English

使用刻面和“自由”刻度调整 ggplot2 中的 y 轴限制

[英]Adjusting y axis limits in ggplot2 with facet and "free" scales

Here is the plot I can make:这是我可以制作的 plot:

data <- data.frame(Patient = rep(seq(1, 5, 1), 2),
                   Treatment = c(rep("Pre", 5), rep("Post", 5)),
                   Gene.1 = c(rnorm(5, 10, 5), rnorm(5, 50, 5)),
                   Gene.2 = c(rnorm(5,10,5), rnorm(5, 10, 5)))

data %>%
  gather(Gene, Levels, -Patient, -Treatment) %>%
  mutate(Treatment = factor(Treatment, levels = c("Pre", "Post"))) %>%
  mutate(Patient = as.factor(Patient)) %>%
  ggplot(aes(x = Treatment, y = Levels, color = Patient, group = Patient)) +
  geom_point() +
  geom_line() +
  facet_wrap(. ~ Gene, scales = "free") +
  theme_bw() +
  theme(panel.grid = element_blank())

示例图

The "free" scales function is wonderful, however, I would like to make these two specifications/adjustments: “免费”秤 function 很棒,但是,我想做这两个规格/调整:

  1. Y axis begins at 0 Y 轴从 0 开始

  2. Increase the upper y limit by ~10% so that I have some space to add some annotations later on (p-values, etc.) in photoshop.将 y 上限增加约 10%,以便我有空间稍后在 Photoshop 中添加一些注释(p 值等)。

An alternative strategy might be to make the individual plots and assemble them together, though, this gets a little tedious with many facet elements.另一种策略可能是制作单独的情节并将它们组合在一起,但是,这会因为许多方面元素而变得有点乏味。

First, reproducibility with random data needs a seed.首先,随机数据的再现性需要一个种子。 I started using set.seed(42) , but that generated negative values which caused completely unrelated warnings.我开始使用set.seed(42) ,但这会产生负值,导致完全不相关的警告。 Being a little lazy, I changed the seed to set.seed(2021) , finding all positives.有点懒惰,我将种子更改为set.seed(2021) ,发现所有积极因素。

For #1, we can add limits= , where the help for ?scale_y_continuous says that对于 #1,我们可以添加limits= ,其中?scale_y_continuous的帮助说明

  limits: One of:

            • 'NULL' to use the default scale range

            • A numeric vector of length two providing limits of the
              scale. Use 'NA' to refer to the existing minimum or
              maximum

            • A function that accepts the existing (automatic) limits
              and returns new limits Note that setting limits on
              positional scales will *remove* data outside of the
              limits. If the purpose is to zoom, use the limit argument
              in the coordinate system (see 'coord_cartesian()').

so we'll use c(0, NA) .所以我们将使用c(0, NA)

For Q2, we'll add expand= , documented in the same place.对于第二季度,我们将添加expand= ,记录在同一个地方。

data %>%
  gather(Gene, Levels, -Patient, -Treatment) %>%
  mutate(Treatment = factor(Treatment, levels = c("Pre", "Post"))) %>%
  mutate(Patient = as.factor(Patient)) %>%
  ggplot(aes(x = Treatment, y = Levels, color = Patient, group = Patient)) +
  geom_point() +
  geom_line() +
  facet_wrap(. ~ Gene, scales = "free") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_y_continuous(limits = c(0, NA), expand = expansion(mult = c(0, 0.1)))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM