Reputation: 454
Hello all and thanks in advance,
I am trying to have my legend show filled shapes, opposed to simply outlining with color (shown below):
My code is as follows:
p.ch4 <- ggplot(ch4.data, aes(x=date, y=value, group = type,
fill=type, color=type)) +
geom_line() +
geom_point(aes(shape=type), color = "black", size =4) +
geom_point(aes(shape=type, color=type), size=3) +
scale_shape_manual(values = c(21:24)) +
scale_fill_manual(values = c("darkorchid3","indianred3","dodgerblue3")) +
scale_color_manual(values = c("darkorchid3","indianred3","dodgerblue3")) +
annotate("rect", xmin=as.POSIXct("2017-11-09"), xmax=as.POSIXct("2018-04-09"),ymin=0,ymax=75,alpha=.25) +
scale_y_continuous(sec.axis = sec_axis(~., name = expression(paste('Methane (', mu, 'M)')))) +
labs(y = expression(paste('Methane (', mu, 'M)')), x = "", color = "", shape = "") +
theme_linedraw(base_size = 18) +
#theme(legend.position = "none") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
strip.text = element_text(face = "bold")) +
theme(axis.text = element_text(angle = 45, hjust = 1))
print(p.ch4)
I realize I have done a lot of work regarding datapoint colors, outlines, and shapes, and I wondered if therein lies my issue?
For reproducibility, here is my dataset "ch4.data":
> dput(ch4.data)
structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("SWI",
"4cm", "12cm"), class = "factor"), date = structure(c(1537167600,
1535266800, 1533279600, 1531292400, 1529305200, 1527318000, 1526713200,
1527231600, 1525762800, 1524294000, 1522825200, 1521356400, 1516089600,
1511596800, 1509606000, 1537340400, 1534575600, 1531119600, 1530255600,
1527663600, 1525071600, 1517212800, 1513756800, 1510300800, 1537167600,
1535094000, 1533020400, 1530946800, 1528873200, 1528441200, 1526713200,
1524898800, 1522393200, 1519545600, 1516435200, 1514361600, 1510992000
), class = c("POSIXct", "POSIXt"), tzone = ""), value = c(52.43,
40.37, 12.92, 5.12, 4.93, 2.4, 1.97, 3.07, 5.7, 3.56, 2.74, 0.67,
0.78, 0.48, 0.7, 0.3, 0.21, 4.11, 1.54, 4.86, 7.42, 3.62, 0.81,
0.37, 6.2, 9.14, 34.11, 57.83, 63.02, 63.56, 54.13, 42.92, 32.59,
23.23, 9.97, 5.62, 0.52), geochem = c("ch4", "ch4", "ch4", "ch4",
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4",
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4",
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4",
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4")), row.names = c(NA,
-37L), class = "data.frame")
Thank you!
Upvotes: 2
Views: 1036
Reputation: 124213
This could be achieved like so. Important step was to give all three scales the same name so that the legends are merged into one:
BTW: I also dropped the second geom_point layer and simply added the black color as an argument.
EDIT: As @chemdork123 correctly pointed out in his comment, the issue could have been more easily be solved by setting the same labels for all three scales in the labs()
statement or by dropping the empty, i.e. ""
labels for the color
and shape
scales.
library(ggplot2)
ggplot(ch4.data, aes(x=date, y=value)) +
geom_line(aes(color=type)) +
geom_point(aes(shape=type, fill=type), color = "black", size=4) +
scale_shape_manual(name = "type", values = c(21:23)) +
scale_fill_manual(name = "type", values = c("darkorchid3","indianred3","dodgerblue3")) +
scale_color_manual(name = "type", values = c("darkorchid3","indianred3","dodgerblue3")) +
annotate("rect", xmin=as.POSIXct("2017-11-09"), xmax=as.POSIXct("2018-04-09"),ymin=0,ymax=75,alpha=.25) +
scale_y_continuous(sec.axis = sec_axis(~., name = expression(paste('Methane (', mu, 'M)')))) +
labs(y = expression(paste('Methane (', mu, 'M)')), x = "", color = "", shape = "") +
theme_linedraw(base_size = 18) +
#theme(legend.position = "none") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
strip.text = element_text(face = "bold")) +
theme(axis.text = element_text(angle = 45, hjust = 1))
Created on 2020-08-24 by the reprex package (v0.3.0)
Upvotes: 3