在第一轴上使用Free_y比例,并在第二轴上固定+ Facet_grid+ GGPLOT2
是否有任何方法可以在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) -
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
次级轴在GGPLOT2中实现,是主要轴的变换的装饰,因此我不知道一种优雅的方法,因为它需要辅助轴公式知道每个方面的不同缩放因子。
这是一种骇人听闻的方法,我将每个次级系列缩放到其各自的主要系列中,然后为次级系列添加一些手动注释。另一种方法可能是为每个方面分开制作图,例如在这里
拼布
将它们组合起来。给定一些虚假数据,其中这些小方面的范围在主要系列中具有不同的范围,但次级系列的范围相同:
< img src =“ https://i.sstatic.net/dw7kq.png” alt =“在此处输入图像说明”>
...我们可以将每个次要系列缩放到其主要系列中,并为其添加自定义注释该中学系列:
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:
...we could scale each secondary series to its primary series, and add custom annotations for that secondary series: