0

I tried to customize the legend to and I want to fill the legend with appointed colors I want and delete any slash or symbol inside.

theme(axis.title.x = element_blank(),
      axis.title.y = element_blank(),
      axis.title.y.right = element_blank(),
      axis.ticks.x=element_blank(),
      axis.ticks.y=element_blank(),
      axis.text.x=element_text(angle = 45, size = 10, vjust = 0.5, face = "bold"),
      axis.text.y=element_blank(),
      axis.line = element_line(colour = "white"),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      panel.border = element_blank(),
      panel.background = element_blank(),
      plot.background=element_blank(),
      legend.position="left",
      legend.title=element_blank(),
      legend.text = element_text(size = 16, face = "bold"),
      legend.key = element_rect(fill = c("red", "blue")))

enter image description here

However, I though I still need to add something to fulfill my desire.
legend.key seems incorrect here.
What I want is: oh! but the first is red rather than orange.

enter image description here

Rémi Coulaud
  • 1,684
  • 1
  • 8
  • 19
Peter Chen
  • 1,464
  • 3
  • 21
  • 48

1 Answers1

2

The best solution I find is to use guides function. Indeed, the following code does the work :

df <- data.frame(value = rnorm(100), group = as.factor(sample(c(1, 2), size = 100, replace = T)))

ggplot(df, aes(x = value, y=value, col = group)) +
  scale_color_manual(values = c("1" = "red", "2" = "blue")) +
  geom_point() + 
  guides(colour = guide_legend(override.aes = list(shape = 15, size = 10))) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.title.y.right = element_blank(),
        axis.ticks.x=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.x=element_text(angle = 45, size = 10, vjust = 0.5, face = "bold"),
        axis.text.y=element_blank(),
        axis.line = element_line(colour = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        plot.background=element_blank(),
        legend.position="left",
        legend.title = element_blank(),
        legend.text = element_text(size = 16, face = "bold"),
        legend.key = element_blank(),
        legend.box.background =  element_blank())

My insipiration is several other post on stackoverflow :

ggplot2 custom legend shapes

How to increase the size of points in legend of ggplot2?

Rémi Coulaud
  • 1,684
  • 1
  • 8
  • 19