Legende in ggplot

Wie erstelle ich Grafiken, was ist zu beachten?

Moderatoren: EDi, jogo

Antworten
electrifix
Beiträge: 2
Registriert: Mo Jul 10, 2023 3:31 pm

Legende in ggplot

Beitrag von electrifix »

Hallo, ich habe noch meine Probleme mit den Legenden in ggplot,
hab schon mehrfach und mit eingien beispielen versucht, aber irgendwie bekomme ich sie nicht in meine Plots.

Kann vllt jemand kurz erläutern an welcher Stelle ich die Bezeichner definiere etc, oder einfach im Code einfügen? Liegt das Problem vllt daran das ich 2 geoms aus 2 versch. df nutze?

Hier ein konkreter Code von mir, wieder im Ursprungszustand.

Code: Alles auswählen


ggplot() +
  theme_fivethirtyeight()+
  theme(axis.title.y= element_text())+
  
  geom_point(data=spotmarket, aes(x = date, y = price, ), color = "steelblue4", size=1) +
  geom_line(data = average_price, aes(x = date, y = sma7d, ), color="orange", linewidth=1) +
  
  labs(title = "Stündlicher Strompreis",x=NULL,  y = "Cent / kWh") +
  
  scale_y_continuous(labels = c(-15,0,15,30,45,60,75,90), breaks = c(-15,0,15,30,45,60,75,90))+
  scale_x_date(date_labels = "%Y", 
               breaks = as.Date(c("2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01", "2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01", "2022-01-01", "2023-01-01")))

Athomas
Beiträge: 769
Registriert: Mo Feb 26, 2018 8:19 pm

Re: Legende in ggplot

Beitrag von Athomas »

Hallo, ich habe noch meine Probleme mit den Legenden in ggplot,
hab schon mehrfach und mit eingien beispielen versucht, aber irgendwie bekomme ich sie nicht in meine Plots.
Aha - schade!
Liegt das Problem vllt daran das ich 2 geoms aus 2 versch. df nutze?
Auf diese konkrete Frage kann ich eine konkrete Antwort geben, die ich auch anhand von mir erzeugten konkreten Spieldaten illustrieren kann:

Code: Alles auswählen

library(ggplot2)

Tage.1 <- 19000 + 1:15
DF.1 <- data.frame(x=as.Date(Tage.1), y=0.321*(Tage.1-19000) + rnorm(15))

Tage.2 <- 19000 + 33:44
DF.2 <- data.frame(x=as.Date(Tage.2), y=-0.231*(Tage.2-19032) + rnorm(12))

ggplot() +
  theme_bw() +
  geom_point(data=DF.1, aes(x,y), colour="red") +
  geom_line(data=DF.2, aes(x,y), colour="blue")
Daran sollte es also nicht liegen!
Antworten