在第一轴上使用Free_y比例,并在第二轴上固定+ Facet_grid+ GGPLOT2

发布于 2025-01-28 10:51:38 字数 1063 浏览 2 评论 0原文

是否有任何方法可以在ggplot2中的左手(第一)轴上设置scale ='free_y'并在右手(第二)轴上使用固定轴?

我有一个数据集,我需要在一个变量中使用自由量表,并修复了另一个变量,但两者都表示同一图。为此,我正在尝试在我的数据中添加第二个,固定的Y轴。问题是我找不到任何方法来为第二轴设置固定比例,并在facet网格中反映出固定比例。

这是我到目前为止创建图形的代码 -

#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())

这是它正在产生的(为简单起见,从代码中省略了一些详细信息) -

“在此处输入图像描述”

我需要的是左侧的规模,以保留”自由”和范围为0-24的规模。

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?

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. 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) -

enter image description here

What I need is for the scale on the left to remain "free" and the scale on the right to range from 0-24.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2025-02-04 10:51:38

次级轴在GGPLOT2中实现,是主要轴的变换的装饰,因此我不知道一种优雅的方法,因为它需要辅助轴公式知道每个方面的不同缩放因子。

这是一种骇人听闻的方法,我将每个次级系列缩放到其各自的主要系列中,然后为次级系列添加一些手动注释。另一种方法可能是为每个方面分开制作图,例如在这里 拼布将它们组合起来。

给定一些虚假数据,其中这些小方面的范围在主要系列中具有不同的范围,但次级系列的范围相同:

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)

< img src =“ https://i.sstatic.net/dw7kq.png” alt =“在此处输入图像说明”>

...我们可以将每个次要系列缩放到其主要系列中,并为其添加自定义注释该中学系列:

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"))

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.

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. Another way might be to make the plots separately for each facet like here and use patchwork to combine them.

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)

enter image description here

...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"))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文