I want to arrange mutiple legend in ggplot with multiple rows and columns. However currently, from the documentation I can only decide there direction or manipulate rows/columns within 1 legend. Am I overlook something? Thanks for any reference point to the solution.
Here is the sample code and what I have done and the expect result.
data <- seq(1000, 4000, by=1000)
colorScales <- c("#c43b3b", "#80c43b", "#3bc4c4", "#7f3bc4")
names(colorScales) <- data
ggplot() +
geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
shape=21) +
scale_color_manual(name="Legend 1",
values=colorScales) +
scale_fill_gradientn(name="Legend 2",
labels=comma, limits=c(0, max(data)),
colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
values=c(0, 0.5, 1)) +
scale_size_continuous(name="Legend 3") +
theme(legend.direction = "vertical", legend.box = "vertical")
Output vertical legend:
ggplot() +
geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
shape=21) +
scale_color_manual(name="Legend 1",
values=colorScales) +
scale_fill_gradientn(name="Legend 2",
labels=comma, limits=c(0, max(data)),
colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
values=c(0, 0.5, 1)) +
scale_size_continuous(name="Legend 3") +
theme(legend.direction = "vertical", legend.box = "horizontal")
Output horizontal legend
ggplot() +
geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
shape=21) +
scale_color_manual(name="Legend 1",
values=colorScales) +
scale_fill_gradientn(name="Legend 2",
labels=comma, limits=c(0, max(data)),
colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
values=c(0, 0.5, 1)) +
guides(colour = guide_legend(nrow = 2, byrow = T, override.aes=list(size=4))) +
guides(size = guide_legend(nrow = 2, byrow = T)) +
scale_size_continuous(name="Legend 3") +
theme(legend.direction = "vertical", legend.box = "vertical")
Output legend with vertical layout, 2 columns within each legend:
What I want is this:
See Question&Answers more detail:
os