Reputation: 15
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
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!
Upvotes: 1
Views: 798
Reputation: 8120
Here is another option. Remove the legend from the current points, add a blank point layer of size=0
to add a legend, use guides
to format the legend how you want. I also cleaned up your plot code a bit. I like to keep all my geom_*
, scale_*
, labs
, and theme
lines together so that you don't override something later. I notices a few redundant lines.
library(tidyverse)
ggplot(data, aes(x = run, y = no2c, color = sample)) +
geom_point(aes(shape = sample, size = sample), show.legend = FALSE) + #remove from Legend
geom_point(size = 0)+ #add blank to format legend
geom_line(aes(y=no2c), color="black", data = subset(data, sample == 'F')) +
scale_size_manual(values = c(F = 2, G = 4)) +
scale_color_manual(name="Sample",
labels=c("Analyser", "Grab"),
values = c("F"="black", "G"="red3"))+
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)) +
labs(title = expression(paste("C")),
x = "Run number",
y= expression(paste("NO"[2], " (mg " ~L^-1*'', ")"))) +
theme_bw() +
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"),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
guides(color = guide_legend(override.aes = list(size = c(2,4), shape = c("circle", "triangle"))))
Upvotes: 0
Reputation: 52349
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"))
Upvotes: 2