r - 如何从 ggcombine 到具有两个 y 轴的单个图?
问题描述
我正在寻找一种方法来结合这两个情节。使用 ggarrange 我已经能够将它们放在一个图形中,但现在我想将它们与两个 Y 轴结合起来。不幸的是,我现在一无所获。R 对我来说仍然是全新的领域。互联网上有一些解释,但不幸的是,我在将它们应用于我的特定数据集时遇到了问题。我很诚实。万一有人感兴趣:上图象征着月相。1 是满月,0 是新月。带有 geom_col() 的下图表示被相机陷阱拍摄的动物。目标是将活动确定为月相的函数。我用于这两个图的代码是:
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
。
使数据范围在两个变量之间重叠,我们可以使用rescale
scales 包中的函数。
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
)
由reprex 包于 2021-03-04 创建(v1.0.0)
推荐阅读
- android - 如何在 Xamarin 表单中检查 network_provider 和 gps_provider?
- android - Android ArCore. How I can make a rotation for some object along a line
- java - 使用工厂方法/抽象工厂模式
- python - 尝试替换数据框列中的特殊字符
- python-3.x - 底图 npstere 投影返回没有数据的空白地图
- amazon-web-services - 识别:空结果
- java - Watson Speech-to-text 授权问题
- c# - 查找计算机上的总物理内存、.NET core 2.1、c#、Ubuntu
- python - 一旦所有页面都被循环并且值附加到我的列表中,我如何将抓取的值存储到数据框中?
- php - $_SESSION 无法使用 ajax 动态调用