Hello everyone I have a question regarding a legend, I do not understand how I get to a legend. I also want the colors to be the same as the legend. Does anyone have an idea? Thank you
output$gesamtkonsum <-renderPlot({
dataset <- subset(consum, geschlecht == input$geschlecht & altersgruppe == input$altersgruppe)
columns <- NULL
droge <- input$drogen
if (droge == "Tabak"){
columns <- c("`Tabak`", "`ehemalige Raucher (seit mind. 2 Jahren)`", "`ehemalige Raucher (seit weniger als 2 Jahren)`", "`Nichtraucher (nie)`")
} else if (droge == "Alkohol"){
columns <- c("`Alkohol`", "`1 mal pro Tag Alkoholkonsum`","`seltener Alkoholkonsum`","`1-2 mal wöchentlich Alkoholkonsum`","`nie Alkoholkonsum`")
} else if (droge == "Schlafmittel"){
columns <- c("`Schlafmittel`", "`täglich Schlafmittel`","`mehrmals pro Woche Schlafmittel`","`etwa einmal pro Woche Schlafmittel`")
} else if (droge == "Beruhigungsmittel") {
columns <- c("`Beruhigungsmittel`", "`täglich Beruhigungsmittel`", "`mehrmals pro Woche Beruhigungsmittel`", "`etwa einmal pro Woche Beruhigungsmittel`")
} else if ( droge == "Cannabis") {
columns <- c("`Cannabis`", "`schon einmal genommen Cannabis`", "`in den letzten 30 Tagen Cannabis`")
} else if ( droge == "harte Drogen"){
columns <- c("`harte Drogen`", "`schon einmal genommen harte Drogen`", "`in den letzten 12 Monaten Harte Drogen`")
}
plot <- ggplot()
for (i in columns){
plot <- plot + geom_line(data = dataset, aes_string(x = "jahr", y = i , group = 1),
colour = randomColor(count = 1, hue = c(" ", "random", "red", "orange", "yellow","green", "blue", "purple", "pink", "monochrome"), luminosity = c(" ", "random", "light", "bright", "dark")))
}
print(plot)
})
}
here were my structure:structure(list(jahr = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 5L,
5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1992",
"1997", "2002", "2007", "2012", "2017"), class = "factor"), geschlecht = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L), .Label = c("Frauen", "Männer"), class = "factor"),
altersgruppe = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("15-39",
"40-69", "70+"), class = "factor"), Tabak = c(29.5, 53.7,
68, 38.9, 29.4, 14.9, 30.7, 24.8, 9, 42.2, 30.1, 11.6, 29,
24.4, 6.4, 38.8, 31.1, 13, 32, 26, 7.6, 41.2, 35.5, 17.1,
37.3, 25.9, 8, 45.3, 37.1, 21.2, 31.1, 22, 6.2, 40.5, 36.4,
20.1), Alkohol = c(74.4, 79.3, 73.8, 85.3, 88.2, 88.4, 77.4,
80.6, 72.1, 86.9, 90, 87.9, 76.5, 80.4, 69.5, 87.1, 90.1,
90.2, 68.3, 74.8, 58.6, 84.3, 87.4, 85.6, 75.7, 77.9, 61.2,
86.6, 91.6, 86.6, 78.6, 80, 66.4, 88.1, 93.7, 89.9), Schlafmittel = c(1.5,
6, 15.7, 1.1, 3.8, 6.5, 1.8, 6.7, 18.9, 0.9, 3.7, 9.9, 1.2,
7.1, 21.4, 0.2, 3.9, 11.4, 0.7, 4.9, 17.9, 0.3, 2.6, 9, 1.5,
6.7, 24, 0, 3.1, 14.9, 1.3, 6.7, 27.7, 0.3, 3.9, 10.9), Beruhigungsmittel = c(2,
4.6, 8.6, 1.5, 3.1, 3.8, 2.5, 5.7, 9.5, 1.7, 3.9, 4.5, 2.2,
6.2, 10.6, 1.9, 3.8, 7, 2.1, 5.9, 8.4, 1, 3.3, 6.8, 2.2,
6.7, 11.6, 1.5, 3.2, 5.8, 2.5, 6.3, 11.6, 2, 3.2, 3.9), Cannabis = c(36.2,
19.9, 0, 52.9, 30.9, 0, 32.2, 16.6, 0, 51.7, 27.6, 0, 26.8,
14.1, 0, 47.9, 21, 0, 24.6, 8.4, 0, 43.5, 15.8, 0, 22.8,
5.1, 0, 40.4, 12.4, 0, 13.2, 0, 0, 27, 0, 0), `harte Drogen` = c(7.3,
4.9, 0, 16, 8.5, 0, 7.1, 2.8, 0, 12.1, 6.1, 0, 4.9, 2.7,
0, 10.2, 5, 0, 3.5, 1.4, 0, 6.8, 3, 0, 3.9, 1.4, 0, 8, 3.1,
0, 3.4, 0, 0, 7.3, 0, 0), `Nichtraucher (nie)` = c(59, 53.7,
68, 50.6, 42.1, 37.4, 57.4, 52.2, 72, 47.7, 40.7, 38.6, 59.5,
51.9, 75.4, 50.9, 38.8, 35.4, 56.6, 53.3, 77.1, 48.6, 32.4,
39.6, 50.7, 55.1, 77.5, 45.1, 31.2, 33, 54, 59.7, 82.9, 47,
29.9, 28.8), `ehemalige Raucher (seit mind. 2 Jahren)` = c(7.2,
21.3, 22.5, 6.5, 25.8, 46.7, 9.1, 21.8, 18.5, 7.6, 27.7,
49.1, 8.8, 22.3, 17.2, 7.8, 27.8, 50, 8.1, 19.2, 14.8, 6.7,
29.8, 42.3, 9.6, 17.6, 13.9, 7.3, 28.8, 44.5, 11.5, 16.8,
10.2, 9.2, 21, 48.2), `ehemalige Raucher (seit weniger als 2 Jahren)` = c(4.3,
1.5, 0.7, 4, 2.7, 1, 2.8, 1.2, 0.4, 2.5, 1.5, 0.7, 2.7, 1.4,
1.1, 2.4, 2.3, 1.6, 3.4, 1.5, 0.4, 3.5, 2.3, 0.9, 2.5, 1.4,
0, 2.3, 2.9, 0, 3.4, 1.5, 0, 3.4, 2.8, 2.9), `2 mal oder mehr pro Tag Alkoholkonsum` = c(0.4,
0.6, 3.3, 1, 3.3, 10, 0, 1.4, 3.2, 0.6, 5.1, 9.7, 0, 1.5,
3.2, 1.2, 5.5, 11.6, 0.3, 1.9, 2.7, 2.1, 7.9, 13.1, 0.4,
2.4, 3.3, 2.6, 8.4, 10.7, 1.4, 4.1, 5.1, 6.9, 15.3, 16.5),
`1 mal pro Tag Alkoholkonsum` = c(1.3, 6.8, 15.3, 3.1, 13.2,
29.7, 1.3, 8.9, 16.3, 4.8, 15.6, 31.6, 1.7, 10.7, 14.8, 4.2,
20.3, 34.2, 2.6, 11.6, 15.4, 6.7, 20.8, 34.9, 3, 11.5, 14.3,
8.4, 25.4, 33.5, 4.4, 10.9, 14.2, 11.3, 23.9, 29), `mehrmals pro Woche Alkoholkonsum` = c(7.7,
13.3, 9.5, 16.5, 21.7, 15, 6.3, 10.7, 5.8, 13.4, 17.8, 10.5,
4.6, 8.1, 4.4, 11.7, 14.4, 11, 4.6, 8.2, 3.9, 12.4, 16.2,
9.9, 5.7, 7.9, 4.3, 14.9, 15.1, 11.3, 6.7, 7.3, 3.6, 16.3,
14.5, 7.8), `1-2 mal wöchentlich Alkoholkonsum` = c(34.6,
32.7, 21.8, 41, 33.6, 20.7, 32, 31.4, 16.8, 44.6, 33, 18.7,
32.8, 30.5, 16.6, 46.7, 33.7, 19.7, 30.1, 28.2, 13, 42.8,
27.7, 14.2, 29, 25.1, 10.2, 39.3, 26.5, 13.3, 29.9, 25.4,
11, 35.1, 24.8, 16.8), `seltener Alkoholkonsum` = c(30.9,
25.9, 23.8, 23.6, 16.6, 13, 37.9, 28.1, 30, 23.5, 18.4, 17.4,
37.4, 29.6, 30.5, 23.3, 16.2, 13.8, 30.7, 24.9, 23.5, 20.3,
14.6, 13.5, 37.6, 31.1, 29.1, 21.4, 16.2, 17.7, 36.2, 32.3,
3.6, 18.5, 15.1, 19.7), `nie Alkoholkonsum` = c(25.2, 20.7,
26.2, 14.7, 11.8, 11.6, 22.4, 19.4, 27.9, 13.1, 10, 12.1,
23.4, 19.6, 30.5, 12.9, 9.9, 9.8, 31.7, 25.2, 41.4, 15.7,
12.6, 14.4, 24.3, 22.1, 38.8, 13.4, 8.4, 13.4, 21.4, 20,
33.5, 11.9, 6.3, 10.1), `täglich Schlafmittel` = c(0.4,
3.4, 11.1, 0.6, 2.4, 4.2, 0.9, 3.8, 12.8, 0.4, 1.6, 4.9,
0.6, 3.2, 14.2, 0.2, 2, 7.4, 0.4, 2.7, 11.3, 0, 1.2, 6.8,
0.6, 3.4, 16.4, 0, 1.5, 9.4, 0.4, 3.6, 19.2, 0.3, 2, 7),
`mehrmals pro Woche Schlafmittel` = c(0.7, 1.3, 2.2, 0, 0.7,
1.3, 0.5, 1.7, 3.5, 0.3, 1.4, 2.2, 0.3, 1.8, 3.7, 0, 0.9,
2.3, 0, 1.2, 3.7, 0, 0.7, 2.2, 0.4, 1.9, 4.6, 0, 1, 2.4,
0.4, 1.5, 4.3, 0, 1.2, 2), `etwa einmal pro Woche Schlafmittel` = c(0.4,
1.4, 2.4, 0.5, 0.7, 1, 0.4, 1.1, 2.6, 0.2, 0.8, 2.8, 0.3,
2, 3.4, 0.7, 1, 1.7, 0.3, 1.1, 3, 0.3, 0.6, 2.4, 0.5, 1.4,
3, 0, 0.6, 3.1, 0.5, 1.6, 4.2, 0, 0.7, 1.9), `täglich Beruhigungsmittel` = c(0.8,
3.1, 6.1, 1.1, 2, 2.7, 1.1, 3.8, 6.7, 1, 2.7, 2.2, 1.1, 3.8,
6.3, 1.1, 2.5, 3.4, 1.3, 3.9, 4.9, 0.6, 2.4, 4.2, 1.2, 4.2,
7.3, 1.2, 1.9, 1.8, 1.4, 4.4, 7.5, 1.1, 2.1, 3.9), `mehrmals pro Woche Beruhigungsmittel` = c(0.6,
0.8, 1.2, 0, 0.6, 1.1, 0.6, 1, 1.6, 0.4, 0.9, 1.3, 0.3, 1.1,
1.8, 0.3, 0.7, 1.7, 0.4, 1, 1.7, 0.4, 0.4, 1.4, 0.5, 1.6,
1.9, 0.3, 0.6, 1.2, 0.7, 1.1, 2.1, 0.4, 0.7, 0), `etwa einmal pro Woche Beruhigungsmittel` = c(0.6,
0.8, 1.3, 0.4, 0.5, 0, 0.8, 0.9, 1.2, 0.3, 0.3, 1, 0.8, 1.3,
2.5, 0.5, 0.6, 1.9, 0.4, 1, 1.8, 0, 0.5, 1.2, 0.5, 0.9, 2.4,
0, 0.7, 2.8, 0.4, 0.8, 2, 0.5, 0.4, 0), `in den letzten 30 Tagen Cannabis` = c(3.4,
1.2, 0, 9.1, 2.6, 0, 2.6, 0.5, 0, 7.5, 1.3, 0, 2, 0.4, 0,
7.3, 1.5, 0, 0.9, 0.3, 0, 8.2, 1.3, 0, 2.7, 0, 0, 6.6, 0.5,
0, 1.6, 0, 0, 4.3, 0, 0), `schon einmal genommen Cannabis` = c(32.7,
18.8, 0, 43.8, 28.3, 0, 29.6, 16.1, 0, 44.1, 26.3, 0, 24.8,
13.6, 0, 40.6, 19.5, 0, 21.7, 8.1, 0, 35.3, 14.5, 0, 20.1,
5.1, 0, 33.8, 11.8, 0, 11.5, 0, 0, 22.7, 0, 0), `in den letzten 12 Monaten Harte Drogen` = c(1.2,
0.4, 0, 3.7, 0.8, 0, 1.5, 0, 0, 2.6, 0.4, 0, 0.3, 0, 0, 1.6,
0.5, 0, 0.5, 0, 0, 1.2, 0, 0, 0.5, 0, 0, 1.9, 0, 0, 0.7,
0, 0, 1.3, 0, 0), `schon einmal genommen harte Drogen` = c(6.1,
4.5, 0, 12.3, 7.7, 0, 5.7, 2.8, 0, 9.5, 5.7, 0, 4.6, 2.7,
0, 8.6, 4.5, 0, 3, 1.4, 0, 5.6, 3, 0, 3.4, 1.4, 0, 6.1, 3.1,
0, 2.7, 0, 0, 6, 0, 0), `gesamter Zeitraum` = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA)), row.names = c(NA, -36L), class = "data.frame")
maybe i expressed myself wrong, the legend should show which line is which, i.e. if i choose Tabak the legend should show me the color of the respective category
for example: Tabak = blue
in the legend