Missing Legend in ggplot

Unfortunately, no legend is shown in my ggplot graphic. What am I doing wrong or how do I solve my problem.

Here is the code of a small example:

library(tidyverse)
xWerte = c(1,2,3,4,5)
yWerte=c(10,12,14,12,16)
Daten = data.frame(xWerte, yWerte)
ggplot(Daten, aes(x=xWerte))+
geom_line(aes(y=yWerte), size=1,linetype=1,color="darkgreen") +
labs(y="Achsenbeschriftung für y-Achse",x="Achsenbeschriftung für x-Achse",title="Bildchen zum Üben") +
theme(plot.title=element_text(hjust=0.5))

Thanks for helping! Franziska

The use of legends will probably make more sense to you if you check the following:

28 Graphics for communication | R for Data Science (had.co.nz)

Graphs (cookbook-r.com)

Hi Franziska,
Welcome to the forum. I believe that your plot has no legend because it does not need it. You only need a legend if you are trying to distinguish between two or more things on the plot.

library(tidyverse)

dat2 <-   tibble(aa = 1:20, bb = 20:1)
dat2 <-   tibble(aa = 1:20, bb = 20:1, zz =rep(c("A", "B"), each = 10))

# no legend 
ggplot(dat1, aes(aa, bb)) + geom_line(colour="darkgreen")

#legend because I am looking at the line by the zz variable.
ggplot(dat2, aes(aa, bb)) + geom_line(aes(colour = zz))

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.