Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Duplicated legends in ggplot2 for colour and point shape

I have been fighting with this graph all day, comparing to other plots I have in which the issue does not occur and look at a good few links but there has been no success.

My graph has two legends. I would like to merge them. The outcome I want is for the legend to show as "Sample", "Analyser", "Grab" with a small black circle for analyser and a larger red triangle for grab.

Example plot with duplicated legend

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tested the "guides" command but I have had no luck, I am sure it is simply something I’m doing on my code but I cannot put my finger on it.

See the data set and graph code below:

Data set

data<-structure(list(run = c(66, 66, 67, 68, 69), date = structure(c(18901,
18901, 18901, 18901, 18902), class = "Date"), sample = structure(c(1L,
2L, 1L, 1L, 1L), levels = c("F", "G"), class = "factor"), no3c = c(3.097800016,
2.99, 3.577300072, 3.960299969, 3.103699923), no2c = c(0.219300002,
0.2, 0.325800002, 0.311100006, 0.386000007)), row.names = 66:70, class = "data.frame")

Graph

plot <- ggplot(data, aes(x = run, y = no2c, color = sample)) + 
  labs(title = expression(paste("C"))) + 
  xlab("Run number") +
  ylab(expression(paste("NO"[2], " (mg " ~L^-1*'', ")"))) + 
  geom_point(aes(shape = sample, size = sample)) + 
  scale_size_manual(values = c(F = 2, G = 4)) +
  geom_line(aes(y=no2c, color="no2c"), color="black", data = subset(data, sample == 'F')) +
  scale_x_continuous(breaks=seq(65,70,1),limits = c(65,70)) +
  scale_y_continuous(breaks=seq(0,0.6,0.1), limits = c(0,0.6)) +
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  theme(text = element_text(size=12),
        panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        legend.position = "bottom", legend.title=element_text(size=12), legend.text = element_text(size=12),
        axis.title.y = element_text(size=12),
        plot.title = element_text(size=14, face="bold")) +
  scale_color_manual(name="Sample", 
                     labels=c("Analyser", "Grab"),
                     values = c("F"="black", "G"="red3"))

Thank you so much for your time and help!

>Solution :

You need the same labels and name for all scales (here, color, size and shape):

... +
  scale_color_manual(name = "Sample", 
                     labels = c("Analyser", "Grab"),
                     values = c("black", "red3")) +
  scale_size_manual(name = "Sample", 
                    values = c(2, 4), 
                    labels = c("Analyser", "Grab")) +
  scale_shape_manual(name = "Sample",
                     values = c(16, 17),
                     labels = c("Analyser", "Grab"))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading