bitte helft mir. Ich möchte eine simple ggplot2-GRafik bauen, welche allerdings dynamische Werte einsetzt. Und ich bekomme es einfach nicht hin. Bitte schaut euch mal meine Beispielsyntax an. Ich definiere also title1 und title2 sowie city1 und city2. Diese darin gespeicherten Werte möchte ich anschließend dynamisch in die ggplot-Syntax einsetzen, sodass die Legende entsprechend die Inhalte wiedergibt und auch die Farbgebung auf diesen Inhalten basiert. Ich habe etliche Wege versucht, aber ich scheitere. Habt ihr eine Idee? Vielen Dank (und guten Rutsch )!
Code: Alles auswählen
set.seed(123)
data <- expand.grid(city = c("A", "B"), year = 2010:2023)
data$wert <- runif(nrow(data), min = 10, max = 100)
title1 <- "this is title1"
title2 <- "this is title2"
city1 <- "A"
city2 <- "B"
# Plot mit Legenden-Titeln
ggplot(data, aes(x = year, y = wert, fill = city)) +
geom_area(position="identity",alpha=0.5) +
labs(title = "Your Main Title", x = "Year", y = "Wert", fill = "City") +
scale_fill_manual(name = "City", values = c(city1 = "blue", city2 = "red"),
labels = c(city1 = title1, city2 = title2))