can't change the legende

Hello

I have plotted some data and now I would like to edit the legend. It should no longer have color as title but " Kategorie:" and the content of the legend should also not be 1,2,3... but "weiche Begriffe", "Extremismus", "Demagogie", "Verschwörungen", "Beleidigungen" and "Sexismus". I have already tried the command "scale_fill_discrete", but it just doesn't work. Does anyone have a solution?

ggplot(tabelle, aes(x = Monat)) +
geom_line(aes(y = weiche_Begriffe, color = "1"), linetype = 1, size =2) +
geom_line(aes(y = Extremismus, color = "2"), linetype = 1, size =2) +
geom_line(aes(y = Demagogie, color = "3"), linetype = 1, size =2)+
geom_line(aes(y = Verschwörung, color = "4"), linetype = 1, size =2)+
geom_line(aes(y = Beleidigung, color = "5"), linetype = 1, size =2)+
geom_line(aes(y = Sexismus, color = "6"), linetype = 1, size =2)+
labs(y = "Häufigkeit", title = "Häufigkeiten pro Monat")+
scale_x_continuous(breaks = seq(1,12, by = 1))

The easiest option is to reshape your data first:
Pivot data from wide to long — pivot_longer • tidyr (tidyverse.org)

Then you only require one geom_line() in your code and you'll get a legend automatically.

Thank you!

I found another way to solve:

ggplot(tabelle, aes(x = Monat)) +
geom_line(aes(y = weiche_Begriffe, color = "1"), linetype = 1, size =2) +
geom_line(aes(y = Extremismus, color = "2"), linetype = 1, size =2) +
geom_line(aes(y = Demagogie, color = "3"), linetype = 1, size =2)+
geom_line(aes(y = Verschwörung, color = "4"), linetype = 1, size =2)+
geom_line(aes(y = Beleidigung, color = "5"), linetype = 1, size =2)+
geom_line(aes(y = Sexismus, color = "6"), linetype = 1, size =2)+
labs(y = "Häufigkeit", title = "Häufigkeiten pro Monat")+
scale_x_continuous(breaks = seq(1,12, by = 1)) ->p

p + scale_colour_discrete(name="Kategorien", breaks=c("1","2","3","4","5","6"),
labels= c("weiche Begriffe","Extremismus","Demagogie",
"Verschwörung","Beleidigung","Sexismus"))

That would be the less straightforward way, but if it works for you, that's ok.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.