[英]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.