Home > Software engineering >  How do I set legend element titles?
How do I set legend element titles?

Time:11-25

I am using code from this biostars post to get myself more acquainted with creating plots in ggplot. I am a bit stuck on setting the legend variables though

Is there a way to set the colour and control the number of breaks/dots in the legend (under numDEInCat)

term <-c("snoRNA binding", "preprophase band", "kinesin complex", "microtubule motor activity", "DNA replication")
fc <-runif(5, 1.00, 5.00)
padj_BH <-runif(5, 0.001, 0.05)
numDEInCat <-runif(5, 30, 300)

ggdata <- data.frame(term,fc,padj_BH,  numDEInCat)


gg1 <- ggplot(ggdata,
  aes(x = term, y = fc, size = numDEInCat, color = padj_BH))  

  expand_limits(y = 1)  
  geom_point(shape = 16,inherit.aes = T)  
  scale_size(range = c(2.5,12.5))  
  scale_color_gradient(low= "#ff0303",
                       high="#1e00b3")   #colour for p value
  

  xlab('')   ylab('Fold Enrichment')   #lavel fold enrichment axis
  labs(
    title = "Gene Ontology all",
    subtitle = 'BH-adjusted',
    caption = '', 
    color="Adjusted P-value", #label the aacolor
    size="count")    #label dot size



  theme_bw(base_size = 24)  
  theme(
    legend.position = 'right',
    legend.background = element_rect(),
    plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
    plot.subtitle = element_text(angle = 0, size = 14, face = 'bold', vjust = 1),
    plot.caption = element_text(angle = 0, size = 12, face = 'bold', vjust = 1),

    axis.text.x = element_text(angle = 0, size = 12, face = 'bold', hjust = 1.10),
    axis.text.y = element_text(angle = 0, size = 12, face = 'bold', vjust = 0.5),
    axis.title = element_text(size = 12, face = 'bold'),
    axis.title.x = element_text(size = 12, face = 'bold'),
    axis.title.y = element_text(size = 12, face = 'bold'),
    axis.line = element_line(colour = 'black'),

    #Legend
    legend.key = element_blank(), # removes the border
    legend.key.size = unit(1, "cm"), # Sets overall area/size of the legend
    legend.text = element_text(size = 14, face = "bold"), # Text size
    title = element_text(size = 14, face = "bold"))  

  coord_flip()

gg1

Gene ontology plot

CodePudding user response:

I think what you're looking for are guides(size = guide_legend(override.aes(BLABLA))) and scale_size(breaks = c(BLABLA))

gg1  
  guides(size = guide_legend(override.aes = list(colour = "red")))   
  scale_size(limits = c(1, 1000), breaks = c(10, 500, 1000))

Created on 2021-11-18 by the reprex package (v2.0.1)

  • Related