0

EDIT: Stack said my question is very similar to this one (Combine legends for color and shape into a single legend) but that person wanted to join two legends in one and I just want the wrong one to disappear. I added the labs(color="Years", shape = "Years") line, but it still doesn't work. Keeps two legends with one all in black with the correct symbol and the other colorful with the wrong symbol.

#-----------------

I'm creating a geom_point graph and everything was working fine... Until I had to plot my legends.

What I need is the colorful legend, but with the diamond symbol that appears in the all black legend. I've searching for hours and tried to change my code many times, but could not realize what is going on. My code is above the image and I hope someone knows what is happening :c please

enter image description here

dataframe name = correl_temp columns names = ano, correlacao, ajuste and x (x is classified as factor)

ggplot(correl_temp, aes(x = ajuste, y = correlacao, groups = x, label = correlacao)) + 
  #
  geom_point(aes(shape = x, color = x), size = 5) +
  geom_text(hjust = 0.5, vjust = -1.2) +
  scale_shape_manual(values=c(18,18,18,18,18,18,18,18,18)) +
  scale_color_manual(
                     values = c("#ffd700","#ffb14e","#fa8775",
                              "#ea5f94","#cd34b5","#9d02d7",
                              "#0000ff","#072881","#000000"),
                     labs(color="Years")) +
  theme(legend.position="top") +
  #
  theme_bw() +
  scale_y_continuous(n.breaks = 10) +
  scale_x_continuous(n.breaks = 10) +
  ggtitle("Values of correlation between temperature and mosquitoes captured") +
  xlab("Temporal Adjustment (epidemiological weeks)") + ylab("Correlation") +
  theme(axis.text = element_text(size = 18, family="Arial"),
        axis.title = element_text(size = 18, family = 'Arial'),
        plot.title = element_text(size = 20, hjust = 0.5, margin = margin(b = 10)))

I tried removing scale_shape_manual, but had a mess with my symbols and it wasn't solved at all... Tried removing scale_color_manual. The black legend disappeared but then the colors changed... I removed only "labels" from scale_color_manual and it was all the same: black legend with right symbol, colorful legend with wrong symbol...

3
  • Use labs(color="Years", shape = "Years") - the aesthetics have to share both name and labels.
    – lotus
    Commented Oct 5, 2023 at 3:14
  • You should provide data so your issue is reproducible. Regardless, you have an error where you've included labs() inside scale_shape_manual(). Fix the parentheses and make sure labs() is its own layer and it will work.
    – lotus
    Commented Oct 5, 2023 at 3:41
  • Hi - you need the manual scale settings to be the same and they will merge - but the easiest way here is to remove the scale_shape_manual statement and add shape = 18 outside of the aes statement in geom_point so geom_point(aes(shape = x, color = x), size = 5, shape =18) +
    – Bill Perry
    Commented Oct 6, 2023 at 16:00

0

Browse other questions tagged or ask your own question.