Home > Enterprise >  Legend not changing colors in ggplot2
Legend not changing colors in ggplot2

Time:01-31

I am trying to plot a scatter plot with 22 variables, so they all need to have different markers. I thought of repeating some shapes and colors from RColorBrew, and everything works fine, except for the legend that does not update with the colors I selected (please see image below). I also attached a working example below. What could I possibly be doing wrong?

#!/usr/bin/env Rscript

library(ggplot2)
library(ggpubr)
library(RColorBrewer)

theme_set(
    theme_pubr()
  )

data <- data.frame(
    x = c(1:22),
    y = as.factor(c(1:22))
)

shapes <- rep(15:18, 6)

colors <- rep(brewer.pal(n = 11, name = "Paired"), 2)

plot <- ggplot(data, aes(x=x, y=y, group=y, size=9, color=colors))  
  geom_point(aes(shape=y))  
  scale_shape_manual(values=shapes)  
  scale_size(guide="none")  
  guides(fill="none", color="none")

plot <- plot   theme(panel.grid.major=element_line(colour="gray", size=0.2),
  panel.grid.minor=element_line(colour="gray", size=0.2))

print(plot)

Black Legend

CodePudding user response:

There are two issues:

  1. I you want to have your colors make use of scale_color_manual and map y on the color aes as you did with shape

  2. The reason your legend does not get colored is that you have set guides(color = "none")

library(ggplot2)
library(ggpubr)
library(RColorBrewer)

theme_set(
  theme_pubr()
)

data <- data.frame(
  x = c(1:22),
  y = as.factor(c(1:22))
)

shapes <- rep(15:18, 6)

colors <- rep(brewer.pal(n = 11, name = "Paired"), 2)

plot <- ggplot(data, aes(x = x, y = y, group = y, size = 9))  
  geom_point(aes(shape = y, color = y))  
  scale_shape_manual(values = shapes)  
  scale_color_manual(values = colors)  
  scale_size(guide = "none")  
  guides(fill = "none")

plot   theme(
  panel.grid.major = element_line(colour = "gray", size = 0.2),
  panel.grid.minor = element_line(colour = "gray", size = 0.2)
)

CodePudding user response:

I found a number of things going on. I broke them down one by one here:

  • You're including colors in the aes but it's not a part of the data, it's just the color palette, so that shouldn't be included here. aes is telling ggplot what data to show, not how to format it.
  • geom_point needs both a color and shape aes argument so that it can combine them and later tell the legend how it has done so.
  • To control the color palette used, I added scale_color_manual, similar to how you were already using scale_shape_manual to manually adjust formatting
  • Remove the guides line at the end. by setting color = "none" it's blocking color from being added to the legend.

Try the segment of code below.

data <- data.frame(
  x = c(1:22),
  y = as.factor(c(1:22))
)

shapes <- rep(15:18, 6)

colors <- rep(brewer.pal(n = 11, name = "Paired"), 2)

#OLD: plot <- ggplot(data, aes(x=x, y=y, group=y, size=9, color=colors))  
plot <- ggplot(data, aes(x=x, y=y, group=y, size=9))    
  
  #OLD: geom_point(aes(shape=y))  
  geom_point(aes(shape=y, color=y))  
  
  scale_shape_manual(values=shapes)  
  
  #NEW LINE
  scale_color_manual(values = colors)   

    scale_size(guide="none") 
# REMOVED guides(fill="none", color="none")


plot <- plot   theme(panel.grid.major=element_line(colour="gray", size=0.2),
                     panel.grid.minor=element_line(colour="gray", size=0.2))

print(plot)
  •  Tags:  
  • Related