I have a connected scatterplot using geom_path and geom_point. I currently have the lines connecting the points by a grouping variable (Species_ordered) and each level of that variable symbolized by a different shape. I have another grouping variable (Type) which has two levels. I have colored the lines and points based on Type, but I also want the symbols in the legend to be grouped by Type, with labels. Something like this:
Sample
Native:
[] label
[] label
[] label
Non-Native:
[] label
[] label
[] label
I currently have this code for a related plot which successfully combined the points and lines into a single legend (thanks to help on this question).
ggplot(great_hollow, aes(x = d15N, y = Sample_ordered), color =
Species_ordered) +
geom_path(aes(group = Species_ordered, colour = Species_ordered)) +
geom_point(aes(shape = Species_ordered, fill = Species_ordered),
colour = "black", size = 3) +
scale_shape_manual(values = c(21, 22, 24, 21, 23, 25)) +
facet_wrap(~Type, scales = "fixed") +
ggtitle(expression(paste(delta^{15},
"N Values by Site and Trophic Level"))) +
scale_x_continuous(expression(paste(delta^{15}, "N (\u2030 VS air)"))) +
scale_y_discrete(limits=rev) +
labs(color = "Sample", shape = "Sample", fill = "Sample") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
axis.title.y = element_blank(),
plot.background = element_rect(color = 1, linewidth = 1),
plot.margin = margin(t = 10, r = 10, b = 10, l = 10))
The resulting plot:
I'm trying to create a plot like it where the legend is still combined but the plot is not faceted, there are only two colors (by Type), and the different levels of Species_ordered are grouped by Type in the legend.
This is what I have so far based on this question, using ggnewscale
:
ggplot(great_hollow, aes(x = d15N, y = Sample_ordered), color = Type) +
geom_path(aes(group = Species_ordered, colour = Type)) +
geom_point(aes(shape = Species_ordered, fill = Type),
colour = "black", size = 3) +
scale_shape_manual(values = c(21, 22, 24, 21, 23, 25)) +
scale_fill_manual(name = "Native",
values = c("blue", "red"),
breaks = great_hollow$Type[c(1,5,6)],
labels = great_hollow$Species_ordered[c(1,5,6)],
guide = guide_legend(title.position = "top", order = 1))
+
new_scale_fill() +
geom_path(aes(group = Species_ordered, colour = Type)) +
scale_colour_manual(values = c("blue", "red")) +
geom_point(aes(shape = Species_ordered, fill = Type), colour = "black",
size = 3) +
scale_fill_manual(name = "Non-Native",
values = c("blue", "red"),
breaks = great_hollow$Type[c(2:4)],
labels = great_hollow$Species_ordered[c(2:4)],
guide = guide_legend(title.position = "top", order = 2))
+
scale_colour_manual(values = c("blue", "red")) +
ggtitle(expression(paste(delta^{15},
"N Values by Site and Trophic Level"))) +
scale_x_continuous(expression(paste(delta^{15}, "N (\u2030 VS air)"))) +
scale_y_discrete(limits=rev) +
labs(colour = "Sample", shape = "Sample", fill = "Sample") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
axis.title.y = element_blank(),
plot.background = element_rect(color = 1, linewidth = 1),
plot.margin = margin(t = 10, r = 10, b = 10, l = 10))
The resulting plot:
To be perfectly honest I can't quite figure out what the answerer was doing with "breaks" inside scale_fill_manual() so my issue might be there. I'm not sure why the "Native" and "Non-Native" legend groups appear by themselves and not integrated with the other legends or why they each only show one of the three labels from Species_ordered. I'm also not sure why the red points are switched to grey.
------ EDIT ------
After further digging I've made some progress. I was able to fix the mismatched coloring and merge the legends, but the legend does not show the line through the shape like it did in my first graph. I've also managed to separate the legend items into columns, but I still can't figure out how to sort them into those columns by Type or add subheadings.
ggplot(great_hollow, aes(x = d15N, y = Sample_ordered), colour = Type) +
geom_path(aes(group = Species_ordered, colour = Type)) +
scale_colour_manual(values = c("blue", "red")) +
geom_point(aes(shape = Species_ordered, fill = Type), colour = "black", size = 3) +
scale_shape_manual(name = "Species",
labels = great_hollow$Species_ordered,
values = c(21, 22, 24, 21, 23, 25)) +
scale_fill_manual(name = "Species",
labels = great_hollow$Species_ordered,
values = c("blue", "red"),
guide = "none",
aesthetics = c("colour", "fill")) +
guides(shape = guide_legend(ncol = 2, override.aes = list(fill = c("blue", "red", "red", "red", "blue", "blue")))) +
ggtitle(expression(paste(delta^{15},"N Values by Site and Trophic Level"))) +
scale_x_continuous(expression(paste(delta^{15}, "N (\u2030 VS air)"))) +
scale_y_discrete(limits=rev) +
theme_bw() +
theme(axis.title.y = element_blank())
And the resulting graph: Colors and columns mostly fixed, no subheadings