Search code examples
rggplot2

Legend title in ggplot2


I am struggling to change legend title in ggpplot2. The plot is produced with legend title saying "Enrichment_Score", and I want to change to "Enrichment Score".

If anyone can advice would be brilliant.

D1 <- ggplot(Practice, aes(Practice$case, Practice$pathway, 
      colour = Enrichment_score, size = Practice$q.value)) +
  geom_point() +
  scale_colour_gradientn(colours = cols) +
  theme(legend.position="bottom") +
  scale_size(range = c(1,15)) +
  guides(size=guide_legend(title = "FDR q value"),
         scale_color_gradient=guide_legend("Enrichment Score"))

 
D1 + ggtitle("Gene Set Enrichment Treg cells") +
  xlab("Case") + ylab("Hallmark Gene Sets") +
  theme_bw() ```

Treg cells


Solution

  • You can rename aesthetics in legends for example via the ggplot2 function labs like in the following reprex.

    library(ggplot2)
    
    df <- data.frame(
      x = runif(100),
      y = runif(100),
      z1 = rnorm(100),
      z2 = abs(rnorm(100))
    )
    
    ggplot(df, aes(x, y)) +
      geom_point(aes(colour = z1)) +
      scale_colour_gradient2() +
      labs(colour = "My Legend Name")
    

    reprex output

    Created on 2021-10-13 by the reprex package (v2.0.1)