简体   繁体   English

在第一个轴上使用 free_y 比例并在第二个 + facet_grid + ggplot2 上固定

[英]Use free_y scale on first axis and fixed on second + facet_grid + ggplot2

Is there any method to set scale = 'free_y' on the left hand (first) axis in ggplot2 and use a fixed axis on the right hand (second) axis?有什么方法可以在 ggplot2 的左手(第一)轴上设置scale = 'free_y'并在右手(第二)轴上使用固定轴?

I have a dataset where I need to use free scales for one variable and fixed for another but represent both on the same plot.我有一个数据集,我需要对一个变量使用自由比例,对另一个变量使用固定,但在同一个图上表示两者。 To do so I'm trying to add a second, fixed, y-axis to my data.为此,我尝试向我的数据添加第二个固定的 y 轴。 The problem is I cannot find any method to set a fixed scale for the 2nd axis and have that reflected in the facet grid.问题是我找不到任何方法来为第二轴设置固定比例并将其反映在刻面网格中。

This is the code I have so far to create the graph -这是我迄今为止创建图表的代码 -

#plot weekly seizure date 
p <- ggplot(dfspw_all, aes(x=WkYr, y=Seizures, group = 1)) +  geom_line() + 
  xlab("Week Under Observation") + ggtitle("Average Seizures per Week - To Date") + 
  geom_line(data = dfsl_all, aes(x =WkYr, y = Sleep), color = 'green') +
  scale_y_continuous(
    # Features of the first axis
    name = "Seizures",
    # Add a second axis and specify its features
    sec.axis = sec_axis(~.[0:20], name="Sleep")
  )

p + facet_grid(vars(Name), scales = "free_y") +
  theme(axis.ticks.x=element_blank(),axis.text.x = element_blank())

This is what it is producing (some details omitted from code for simplicity) -这就是它所产生的(为简单起见,代码中省略了一些细节) -

在此处输入图像描述

What I need is for the scale on the left to remain "free" and the scale on the right to range from 0-24.我需要的是左侧的刻度保持“自由”,右侧的刻度范围为 0-24。

Secondary axes are implemented in ggplot2 as a decoration that is a transformation of the primary axis, so I don't know an elegant way to do this, since it would require the secondary axis formula to be aware of different scaling factors for each facet.辅助轴在 ggplot2 中实现为装饰,它是主轴的转换,所以我不知道这样做的优雅方法,因为它需要辅助轴公式了解每个方面的不同缩放因子。

Here's a hacky approach where I scale each secondary series to its respective primary series, and then add some manual annotations for the secondary series.这是一种 hacky 方法,我将每个次要系列扩展到其各自的主要系列,然后为次要系列添加一些手动注释。 Another way might be to make the plots separately for each facet like here and use patchwork to combine them.另一种方法可能是像这里一样为每个方面分别制作图,并使用patchwork来组合它们。

Given some fake data where the facets have different ranges for the primary series but the same range for the secondary series:给定一些假数据,其中主要系列的方面具有不同的范围,但次要系列的范围相同:

library(tidyverse)
fake <- tibble(facet = rep(1:3, each = 10),
               x = rep(1:10, times = 3),
               y_prim = (1+sin(x))*facet/2,
               y_sec = (1 + sin(x*3))/2)

ggplot(fake, aes(x, y_prim)) +
  geom_line() + 
  geom_line(aes(y= y_sec), color = "green") +
  facet_wrap(~facet, ncol = 1)

在此处输入图像描述

...we could scale each secondary series to its primary series, and add custom annotations for that secondary series: ...我们可以将每个次要系列扩展到其主要系列,并为该次要系列添加自定义注释:

fake2 <- fake %>%
  group_by(facet) %>%
  mutate(y_sec_scaled = y_sec/max(y_sec) * (max(y_prim))) %>%
  ungroup()

fake2_labels <- fake %>% 
  group_by(facet) %>%
  summarize(max_prim = max(y_prim), baseline = 0, x_val = 10.5)

ggplot(fake2, aes(x, y_prim)) +
  geom_line() + 
  geom_line(aes(y= y_sec_scaled), color = "green") +
  facet_wrap(~facet, ncol = 1, scales = "free_y") +
  geom_text(data = fake2_labels, aes(x = x_val, y = max_prim, label = "100%"),
            hjust = 0, color = "green") +
  geom_text(data = fake2_labels, aes(x = x_val, y = baseline, label = "0%"),
            hjust = 0, color = "green") +
  coord_cartesian(xlim = c(0, 10), clip = "off") +
  theme(plot.margin = unit(c(1,3,1,1), "lines"))

在此处输入图像描述

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

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