DS4PS / ays-r-coding-sum-2022

Introductory data science course in R, taught at the Andrew Young School GSU.
http://ds4ps.org/ays-r-coding-sum-2022/
2 stars 2 forks source link

Lab 4 - Adding Legend (ggplot) #5

Open mugekosar opened 2 years ago

mugekosar commented 2 years ago

Hi @lecy,

I'm trying to add a legend for regions. I have tried following functions into aes() function but I'm not able to add a legend. group= color= fill=

Should I add legends in a different way? Can you please help me in understanding which part I'm doing wrong?

      geom_line(data = dat.one.region,
                size = 1,
                aes(x = Year,
                    y = Average, group=Region),
                alpha = 0.6,
                color = "yellow") 

Thanks!

mugekosar commented 2 years ago

Hi @lecy and @jamisoncrawford,

I tried to add legends with geom_segment() and annotate() functions and it worked!

jamisoncrawford commented 2 years ago

Glad you got this working! That's certainly one viable approach.

The conventional way to ensure ggplot() automatically adds a legend is to include color = or fill = in the aes() function inside your geom_*() layer. You can suppress it with show_legend = FALSE (IIRC) and you can relabel the legend in the labs() function, using plot + labs(color = "tomato"), for example.

mugekosar commented 2 years ago

Thank you for your explanation! @jamisoncrawford