Hey people. How can I add a legend to my ggplot?

Hello. I'm trying to use ggplot to add a legend in one graph.
First, It was with the legend, but he wasn't with the colors I wanted, so I changed the colors, but then the legend just disappeared. I can't put it back, can someone help me?

I used:

ggplot(freqHMatM, aes(fill=freqHMatM$Frequencia, y=Frequencia, x=Genero, label = round(freqHMatM$Frequencia, 4))) + 
  geom_col(position="fill", 
           color = "white", show.legend = TRUE, fill = c("mistyrose3", "mistyrose2", "mistyrose1", "mistyrose4", "mistyrose3", "mistyrose2", "mistyrose1")) +
  labs(x = NULL, y = "FrequĂȘncia", title = "HMatM") +
  theme_classic(base_size = 18) +
  scale_y_continuous(labels=percent) +
  geom_text(position = position_fill(vjust = 0.5))

And this graph appeared:


(I've never done it, hope that this image is visible)

And, then, I used this one:

ggplot(freqHMatM, aes(fill=freqHMatM$Frequencia, y=Frequencia, x=Genero, label = round(freqHMatM$Frequencia, 4))) + 
  geom_col(position="fill", 
           color = "white", show.legend = TRUE, fill = c("mistyrose3", "mistyrose2", "mistyrose1", "mistyrose4", "mistyrose3", "mistyrose2", "mistyrose1")) +
  labs(x = NULL, y = "FrequĂȘncia", title = "HMatM") +
  theme_classic(base_size = 18) +
  scale_y_continuous(labels=percent) +
  geom_text(position = position_fill(vjust = 0.5))

And this graph, without legends, appeared:
(Look at the first comment, please, I will put this image there, because new users can put only 1 media)

Well, for the ones who understand it, I clearly don't understand how to use most of these functions. Can anyone just help me to add a legend? And change it's name, if possible?

Just because I think it will probably be important:
HMatM is the answers of a Likert scale, ranging from 1 to 5.
A segregate my data into "man" and "woman", to see if there's any difference in the answers.
"FrequĂȘncia", in my code, is the same as frequency, where I put the frequency of man and woman who answered my questions (it was 57 women and 59 men).
"Genero" is the same as "gender", wich were "man" and "woman".

My "freqHMatM" data looks like this:
(Look at the second comment, please, I will put this image there)

Hope anyone can help me.
Thanks in advance.

image

Can you please turn this into a proper REPRoducible EXample (reprex) illustrating your issue? A reprex makes it much easier for others to understand your issue and figure out how to help.

1 Like

I'm sorry... I'm afraid I don't know you to create a reprex :frowning:

Click on the link I gave you to learn how

1 Like

Hope someone can help. Thanks for the tip, @andresrcs

#I will put here the libraries I believe I'm using
library(scales)
library(tidyverse)
library(ggplot2)


#This are my data
freqHMatM <- data.frame(
             stringsAsFactors = FALSE, NA,
             HMatM = c(3L,4L,5L,2L,3L,4L,5L),
             Gender = c("Homem", "Homem","Homem", "Mulher","Mulher", "Mulher","Mulher"),
             n = c(5L,14L, 40L,2L,4L,11L,40L),
             Frequency = c(8.47, 23.73, 67.80, 3.51,7.02, 19.30, 70.17)
              )


# This return a blue graph with legends, but I needed to change it's colors. I created the next one, then.
ggplot(freqHMatM, aes(fill=freqHMatM$Frequency, y=Frequency, x=Gender, label = round(freqHMatM$Frequency, 4))) + 
  geom_col(position="fill", 
           color = "white", show.legend = TRUE) +
  labs(x = NULL, y = "Frequency", title = "HMatM") +
  theme_classic(base_size = 18) +
  scale_y_continuous(labels=percent) +
  geom_text(position = position_fill(vjust = 0.5))


# I created this graph, and the legends just disappeared. I tried to add it again, but nothing works  :(
ggplot(freqHMatM, aes(fill=freqHMatM$Frequency, y=Frequency, x=Gender, label = round(freqHMatM$Frequency, 4))) + 
  geom_col(position="fill", 
           color = "white", show.legend = TRUE, fill = c("mistyrose3", "mistyrose2", "mistyrose1", "mistyrose4", "mistyrose3", "mistyrose2", "mistyrose1")) +
  labs(x = NULL, y = "Frequency", title = "HMatM") +
  theme_classic(base_size = 18) +
  scale_y_continuous(labels=percent) +
  geom_text(position = position_fill(vjust = 0.5))

You are overwriting the fill aesthetic inside the geom_col() function, instead of that you have to set the colors at the scale level (also notice the syntax differences in my example).

library(ggplot2)
library(scales)
library(forcats)

freqHMatM <- data.frame(
    stringsAsFactors = FALSE, NA,
    HMatM = c(3L,4L,5L,2L,3L,4L,5L),
    Gender = c("Homem", "Homem","Homem", "Mulher","Mulher", "Mulher","Mulher"),
    n = c(5L,14L, 40L,2L,4L,11L,40L),
    Frequency = c(8.47, 23.73, 67.80, 3.51,7.02, 19.30, 70.17)
)

colors <- setNames(c("mistyrose3", "mistyrose2", "mistyrose1", "mistyrose4",
                          "mistyrose3", "mistyrose2", "mistyrose1"), 
                   freqHMatM$Frequency)

ggplot(freqHMatM, aes(x = Gender,
                      y = Frequency,
                      fill = fct_rev(fct_inseq(as_factor(Frequency))),
                      label = round(Frequency, 4))) + 
    geom_col(position ="fill", 
             color = "white") +
    geom_text(position = position_fill(vjust = 0.5)) +
    labs(title = "HMatM",
         x = NULL,
         y = "Frequency",
         fill = "Frequency") +
    scale_y_continuous(labels = percent) +
    scale_fill_manual(values = colors) +
    theme_classic(base_size = 18)

Created on 2020-11-29 by the reprex package (v0.3.0.9001)

Another thing to have in mind is that you are mapping a continuos variable to a discrete scale (by setting a discrete selection of colors), this doesn't seem like a good idea, consider this approach instead.

library(ggplot2)
library(scales)

freqHMatM <- data.frame(
    stringsAsFactors = FALSE, NA,
    HMatM = c(3L,4L,5L,2L,3L,4L,5L),
    Gender = c("Homem", "Homem","Homem", "Mulher","Mulher", "Mulher","Mulher"),
    n = c(5L,14L, 40L,2L,4L,11L,40L),
    Frequency = c(8.47, 23.73, 67.80, 3.51,7.02, 19.30, 70.17)
)

ggplot(freqHMatM, aes(x = Gender,
                      y = Frequency,
                      fill = Frequency,
                      label = round(Frequency, 4))) + 
    geom_col(position ="fill", 
             color = "white") +
    geom_text(position = position_fill(vjust = 0.5)) +
    labs(title = "HMatM",
         x = NULL,
         y = "Frequency",
         fill = "Frequency") +
    scale_y_continuous(labels = percent) +
    scale_fill_gradient(low = "mistyrose4", high = "mistyrose1") +
    theme_classic(base_size = 18)

Created on 2020-11-29 by the reprex package (v0.3.0.9001)

1 Like

wow. Thanks :slight_smile:

But how can I add a legend like this:

I'm using a Likert scale, and want the colors to follow the 1~5 punctuation... I tried to use:

labs(title = "HMatM",
      x = NULL,
      y = "Frequency",
      fill = "HMatM")
#Here, with the "HMatM" instead of "Frequency". But it didn't work.

How can add a legend like in this blue graph I printed here?

I did it!

It's all ok, now. Thanks. You are an R god :laughing:

This topic was automatically closed 7 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.