[英]How to get from ggcombine to a single plot with two y-axes?
我正在寻找一种方法来结合这两个情节。 使用 ggarrange 我已经能够将它们放在一个图形中,但现在我想将它们与两个 Y 轴结合起来。 不幸的是,我现在一无所获。 R 对我来说仍然是全新的领域。 互联网上有一些解释,但不幸的是,我在将它们应用于我的特定数据集时遇到了问题。 我很诚实。 万一有人感兴趣:上图象征着月相。 1 是满月,0 是新月。 带有 geom_col() 的下图表示被相机陷阱拍摄的动物。 目标是将活动确定为月相的 function。 我用于这两个图的代码是:
ggarrange(
ggplot(d1, aes(x = Datum, y = mondphase)) +
geom_smooth(span = .2, se = FALSE) +
ylab("Mondphase") + xlab("") +
theme_bw()
,
ggplot(d1, aes(x = Datum, y = Number.of.Animals)) +
geom_col(show.legend = FALSE) +
ylab("Anzahl Auslösungen") +
theme_bw()
,
ncol = 1, align = "v", heights = 1:2
)
这是我的数据的一个例子。 我在代码中使用了“d1”,因为“d1”是特定的相机。
ID Camera.Trap.Name Latitude Longitude Photo.Type Photo.Date Photo.time Class Order Family Genus Species
1 2492 Frankfurt 12345 54321 Animal 2020-05-15 22:31:03 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
2 2493 Frankfurt 12345 54321 Animal 2020-05-15 22:34:50 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
3 2494 Frankfurt 12345 54321 Animal 2020-05-15 22:34:51 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
4 2495 Frankfurt 12345 54321 Animal 2020-05-15 22:34:52 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
5 2496 Frankfurt 12345 54321 Animal 2020-05-15 22:38:50 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
6 2497 Frankfurt 12345 54321 Animal 2020-05-15 22:38:51 MAMMALIA CETARTIODACTYLA CERVIDAE Capreolus capreolus
Number.of.Animals Camera.Start.Date Camera.End.Date Flash Datum up down day_length
1 1 2020-05-14 2020-06-24 25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54 15.45635
2 1 2020-05-14 2020-06-24 25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54 15.45635
3 1 2020-05-14 2020-06-24 25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54 15.45635
4 1 2020-05-14 2020-06-24 25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54 15.45635
5 1 2020-05-14 2020-06-24 25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54 15.45635
6 1 2020-05-14 2020-06-24 25 2020-05-15 2020-05-15 05:38:31 2020-05-15 21:05:54 15.45635
mondphase X
1 0.3737363 NA
2 0.3737363 NA
3 0.3737363 NA
4 0.3737363 NA
5 0.3737363 NA
6 0.3737363 NA
对于任何建议,我都会非常高兴。! 非常感谢。
我认为您可能正在寻找辅助 y 轴。 在 ggplot 中,它们使用起来有点痛苦,因为它不鼓励人们使用它们。 关键的两个步骤是:
sec_axis
中指定逆变换。 为了使两个变量之间的数据范围重叠,我们可以使用缩放 package 中的rescale
function。
library(ggplot2)
library(scales)
# Make secondary axis
secondary <- with(economics, sec_axis(
~ rescale(., from = range(psavert), to = range(unemploy)), # <- inverse transform
name = "Unemployment"
))
# Make plot
ggplot(economics, aes(date)) +
geom_col(aes(y = psavert)) +
# Apply transformation
geom_line(aes(y = rescale(unemploy, to = range(psavert))), # <- transform
colour = "blue") +
# Supply secondary axis
scale_y_continuous(
name = "Personal Savings Rate",
sec.axis = secondary
)
由代表 package (v1.0.0) 于 2021 年 3 月 4 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.