Hallo flo_,
willkommen im Forum.
flo_ hat geschrieben: ↑Mi Apr 13, 2022 2:59 pm Bestimmt mit ggplot(), aber wie sehen da die Codes genau aus?
Ist die Wahl von ggplot2 eine Vorgabe, ist es Dein Wunsch oder ist es nur eine voreilige Annahme, dass das bestimmt immer am besten ist?
Lass das hier mal bei Dir laufen und schau Dir eine base Grafik an, die man sicher noch weiter verbessern kann.
Code: Alles auswählen
set.seed(23)
regen <- pmax(cumsum(rnorm(500)), 0)
SPI <- cumsum(rnorm(500)/3)
par(mar = c(5, 4, 4, 4))
plot(regen, type = "h", col ="blue", ylim = c(0, 20), ylab = "Regen")
abline(h = c(7.5, 10, 12.5), lty = 2, col = "grey")
lines(.3 * SPI + 10, col = "red")
axis(4, col = "red", col.axis = "red",
at = c(5, 7.5, 10, 12.5, 15),
labels = c(-2, -1, 0, 1, 2))
mtext("SPI", side = 4, col = "red", line = 3)
Muss es automatisch ggplot2 sein?
Wenn ja, hier wäre ein (noch unfertiger) Schnellschuss in ggplot2:
Code: Alles auswählen
library(ggplot2)
ggplot(data.frame(time = 1:500, regen = regen, spi = SPI)) +
geom_col(aes(x = time, y = regen), color = "blue", alpha = .3) +
geom_line(aes(x = time, y = spi+10), color = "red") +
scale_y_continuous(sec.axis = sec_axis( trans =~(./4)-2.5, name = "SPI"),
limits = c(0, 20))
Geht das in die richtige Richtung? Kannst Du damit arbeiten?
LG,
Bernhard