Variable align ggrepel text labels in faceted alluvial plot - r

I am trying to create a faceted alluvial plot with labels for the stratums on the first axis repelled to the left and left justified and the labels on the right repelled to the right and right justified.
# Small working example
# Install Packages and Libraries
install.packages("ggplot2")
install.packages("ggalluvial")
install.packages("ggrepel")
library(ggplot2)
library(ggalluvial)
library(ggrepel)
# Data Frame with 2 regions, 3 supply sectors and 3 demand sectors
df <- data.frame(region = c("A","A","A","B","B","B"),
supplySector = c("coal","gas","wind","coal","gas","wind"),
demandSector = c("resid","indus","ag","resid","indus","ag"),
value = 10*runif(6)); df
# Faceted plot with ggrepel (nudge_x and hjust assigned for each label) works.
p <- ggplot(df, aes(y = value, axis1 = supplySector, axis2 = demandSector, group=region)) +
ggalluvial::geom_alluvium(aes(fill = supplySector), width = 1/12, color="black", alpha=0.6) +
ggalluvial::geom_stratum(width = 1/12, fill = "grey70", color = "grey10", alpha=1) +
scale_x_discrete(limits = c("supplySector", "demandSector"), expand = c(0.3,0),drop=F) +
facet_wrap(region~.) +
ggrepel::geom_text_repel(stat = "stratum", label.strata = TRUE, direction = "y",
size = 4, segment.color = 'grey50',
nudge_x = rep(c(-3,-3,-3,3,3,3),2),
hjust = rep(c(1,1,1,-1,-1,-1),2)); p
# Faceted plot with ggrepel (nudge_x and hjust assigned for each label)
# does not work when different number of variables in each facet
df1 <- df[-nrow(df),]; df1 # Remove one of the rows from df
# So this gives the following plot with different alluvia in each facet
p1 <- ggplot(df1, aes(y = value, axis1 = supplySector, axis2 = demandSector, group=region)) +
ggalluvial::geom_alluvium(aes(fill = supplySector), width = 1/12, color="black", alpha=0.6) +
ggalluvial::geom_stratum(width = 1/12, fill = "grey70", color = "grey10", alpha=1) +
scale_x_discrete(limits = c("supplySector", "demandSector"), expand = c(0.3,0),drop=F) +
facet_wrap(region~.); p1
# If we try and label these and assigns the nudge and hjust for each axis we get an error
# It expects the same length vector for nudge and hjust for each facet
p1 + ggrepel::geom_text_repel(stat = "stratum", label.strata = TRUE, direction = "y",
size = 4, segment.color = 'grey50',
nudge_x = rep(c(-3,-3,-3,3,3,3),2),
hjust=rep(c(1,1,1,-1,-1,-1),2))
# Gives error: Error: Aesthetics must be either length 1 or the same as the data (10): hjust
# If we adjust the vectors for nudge_x and hjust to 10
p1 + ggrepel::geom_text_repel(stat = "stratum", label.strata = TRUE, direction = "y",
size = 4, segment.color = 'grey50',
nudge_x = c(-3,-3,-3,3,3,3,-3-3,3,3),
hjust = c(1,1,1,-1,-1,-1,1,1,-1,-1))
# Get Error: Error in data.frame(x = data$x + nudge_x, y = data$y + nudge_y) :
# arguments imply differing number of rows: 9, 6
# In addition: Warning message:
# In data$x + nudge_x :
# longer object length is not a multiple of shorter object length
# It can be plotted without specifying the nudge_x and hjust values
p1 + ggrepel::geom_text_repel(stat = "stratum", label.strata = TRUE, direction = "y",
size = 4, segment.color = 'grey50')
In summary, what I am trying to do is:
For plot p1 (with different number of alluvia in different facets)
Label each x axis stratum column
Have axis1 labels repel to the left and be left justified
Have axis2 labels repel to the right and be right justified
This answer suggested the different vector length for labels but it doesn't work for varying facets.
Labelling and theme of ggalluvial plot in R

This is tricky! The nudge_* and *just arguments generally aren't dynamic. One way you could solve for this is to dig into the guts using ggplot_build()
ggplot_build() has all of the "instructions" of how ggplot() builds the chart. You can edit the data and then run plot(ggplot_gtable()) to see the plot with your modifications. I have added comments to help explain these steps.
# here is the base plot + the new layer for labels
plot_and_label <-
p1 +
geom_text_repel(
stat = "stratum", label.strata = TRUE,
direction = "y", size = 4,
segment.color = 'grey50',
nudge_x = 0
)
# this is the plot under the hood
gg_guts <- ggplot_build(plot_and_label)
# the geom_text_repel layer was the 3rd one we added so you can
# access and edit it like this
gg_guts$data[[3]] <-
gg_guts$data[[3]] %>%
mutate(hjust = ifelse(x%%2 == 1, 2, -2))
# once you've made your adjustments, you can plot it again
plot(ggplot_gtable(gg_guts))

Related

Labeling small strata in alluvial plot - different variables on columns/axis [duplicate]

This question already has an answer here:
How to align and label the stratum in ggalluvial using ggrepel (or otherwise)
(1 answer)
Closed 1 year ago.
I am getting problems with applying ggrepel() in an alluvial plot with different variables on columns. Some observations are so small, I need ggrepel to make them readable.
Because there are three columns, I want to apply different ggrepel() functions to each column:
Left (region): Align lables to the left of axis
Middle (supplySector): Do nothing (i.e. leave text in axis)
Right (demandSector): Align to right of axis.
I've found these issues:
https://cran.r-project.org/web/packages/ggalluvial/vignettes/labels.html
and
How to align and label the stratum in ggalluvial using ggrepel (or otherwise)
Difference is: these examples only have 2 columns, and also columns made of the same variable (but subset of the variable). Previous published fixes are through an ifelse(), selecting a subset within the variable.
ReprEx:
library(ggplot2)
library(ggrepel)
library(tidyr)
library(dplyr)
df <- data.frame(region = c("A","A","A","B","B","B"),
supplySector = c("coal","gas","wind","coal","gas","wind"),
demandSector = c("resid","indus","ag","resid","indus","ag"),
Freq = 20*runif(6)); df
p<- ggplot(df, aes(y = Freq, axis1 = region, axis2 = supplySector, axis3=demandSector, label = after_stat(stratum))) +
ggalluvial::geom_alluvium(aes(fill = demandSector), width = 1/12, color="black", alpha=0.8) +
ggalluvial::geom_stratum(width = 1/3, fill = "grey70", color = "grey10", alpha=1) +
scale_x_discrete(limits = c("Region", "Supply Sector", "Demand Sector"), expand = c(0.3,0),drop=F) +
scale_y_continuous("Frequency (n)")+
theme_classic()+
theme(legend.position = "none")
I've tried to feed the colnames(df) == "region" to get a true/false vector into
p + ggrepel::geom_text_repel(
aes(label = ifelse(colnames(df) == "region", as.character(region), NA)),
stat = "stratum", size = 4, direction = "y", nudge_x = -.5
)
I would then repeat this for aes(label = ifelse(colnames(df) == "demandSector" with nudge_x = 1.5.
Maybe I got you wrong. But after a closer look at your example I would call it a duplicate to my answer you linked in your post.
library(ggplot2)
library(ggrepel)
library(ggalluvial)
p + ggrepel::geom_text_repel(
aes(label = ifelse(after_stat(x) == 1, as.character(after_stat(stratum)), NA)),
stat = "stratum", size = 4, direction = "y", nudge_x = -.5
) + ggrepel::geom_text_repel(
aes(label = ifelse(after_stat(x) == 2, as.character(after_stat(stratum)), NA)),
stat = "stratum", size = 4, direction = "y", nudge_x = 0
) + ggrepel::geom_text_repel(
aes(label = ifelse(after_stat(x) == 3, as.character(after_stat(stratum)), NA)),
stat = "stratum", size = 4, direction = "y", nudge_x = +.5
)

How to modify and add an extra legend in a ggplot2 figure

I have data that looks like this:
example.df <- as.data.frame(matrix( c("height","fruit",0.2,0.4,0.7,
"height","veggies",0.3,0.6,0.8,
"height","exercise",0.1,0.2,0.5,
"bmi","fruit",0.2,0.4,0.6,
"bmi","veggies",0.1,0.5,0.7,
"bmi","exercise",0.4,0.7,0.8,
"IQ","fruit",0.4,0.5,0.6,
"IQ","veggies",0.3,0.5,0.7,
"IQ","exercise",0.1,0.4,0.6),
nrow=9, ncol=5, byrow = TRUE))
colnames(example.df) <- c("phenotype","predictor","corr1","corr2","corr3")
So basically three different correlations between 3x3 variables. I want to visualize the increase in correlations as follows:
ggplot(example.df, aes(x=phenotype, y=corr1, yend=corr3, colour = predictor)) +
geom_linerange(aes(x = phenotype,
ymin = corr1, ymax = corr3,
colour = predictor),
position = position_dodge(width = 0.5))+
geom_point(size = 3,
aes(x = phenotype, y = corr1, colour = predictor),
position = position_dodge(width = 0.5), shape=4)+
geom_point(size = 3,
aes(x = phenotype, y = corr2, colour = predictor),
position = position_dodge(width = 0.5), shape=18)+
geom_point(size = 3,
aes(x = phenotype, y = corr3, colour = predictor),
position = position_dodge(width = 0.5))+
labs(x=NULL, y=NULL,
title="Stackoverflow Example Plot")+
scale_colour_manual(name="", values=c("#4682B4", "#698B69", "#FF6347"))+
theme_minimal()
This gives me the following plot:
Problems:
Tthere is something wrong with the way the geom_point shapes are dodged with BMI and IQ. They should be all with on the line with the same colour, like with height.
How do I get an extra legend that can show what the circle, cross, and square represent? (i.e., the three different correlations shown on the line: cross = correlation 1, square = correlation 2, circle = correlation 3).
The legend now shows a line, circle, cross through each other, while just a line for the predictors (exercise, fruit, veggies) would suffice..
Sorry for the multiple issues, but adding the extra legend (problem #2) is the most important one, and I would be already very satisfied if that could be solved, the rest is bonus! :)
See if the following works for you? The main idea is to convert the data frame from wide to long format for the geom_point layer, and map correlation as a shape aesthetic:
example.df %>%
ggplot(aes(x = phenotype, color = predictor, group = predictor)) +
geom_linerange(aes(ymin = corr1, ymax = corr3),
position = position_dodge(width = 0.5)) +
geom_point(data = . %>% tidyr::gather(corr, value, -phenotype, -predictor),
aes(y = value, shape = corr),
size = 3,
position = position_dodge(width = 0.5)) +
scale_color_manual(values = c("#4682B4", "#698B69", "#FF6347")) +
scale_shape_manual(values = c(4, 18, 16),
labels = paste("correlation", 1:3)) +
labs(x = NULL, y = NULL, color = "", shape = "") +
theme_minimal()
Note: The colour legend is based on both geom_linerange and geom_point, hence the legend keys include both a line and a point shape. While it's possible to get rid of the second one, it does take some more convoluted code, and I don't think the plot would be much improved as a result...

Specifying different positions for multiple legends in ggplot2? [duplicate]

Title pretty well covers it.
I have two legends, relating to size and colour, and wish to have one,say, on the top and one within the graph.
Is this possible and, if so, how
TIA
It can be done by extracting separate legends from plots, then arranging the legends in the relevant plot. The code here uses functions from the gtable package to do the extraction, then functions from the gridExtra package to do the arranging. The aim is to have a plot that contains a color legend and a size legend. First, extract the colour legend from a plot that contains the colour legend only. Second, extract the size legend from a plot that contains the size legend only. Third, draw a plot that contains no legend. Fourth, arrange the plot and the two legends into one new plot.
# Some data
df <- data.frame(
x = 1:10,
y = 1:10,
colour = factor(sample(1:3, 10, replace = TRUE)),
size = factor(sample(1:3, 10, replace = TRUE)))
library(ggplot2)
library(gridExtra)
library(gtable)
library(grid)
### Step 1
# Draw a plot with the colour legend
(p1 <- ggplot(data = df, aes(x=x, y=y)) +
geom_point(aes(colour = colour)) +
theme_bw() +
theme(legend.position = "top"))
# Extract the colour legend - leg1
leg1 <- gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box")
### Step 2
# Draw a plot with the size legend
(p2 <- ggplot(data = df, aes(x=x, y=y)) +
geom_point(aes(size = size)) +
theme_bw())
# Extract the size legend - leg2
leg2 <- gtable_filter(ggplot_gtable(ggplot_build(p2)), "guide-box")
# Step 3
# Draw a plot with no legends - plot
(plot <- ggplot(data = df, aes(x=x, y=y)) +
geom_point(aes(size = size, colour = colour)) +
theme_bw() +
theme(legend.position = "none"))
### Step 4
# Arrange the three components (plot, leg1, leg2)
# The two legends are positioned outside the plot:
# one at the top and the other to the side.
plotNew <- arrangeGrob(leg1, plot,
heights = unit.c(leg1$height, unit(1, "npc") - leg1$height), ncol = 1)
plotNew <- arrangeGrob(plotNew, leg2,
widths = unit.c(unit(1, "npc") - leg2$width, leg2$width), nrow = 1)
grid.newpage()
grid.draw(plotNew)
# OR, arrange one legend at the top and the other inside the plot.
plotNew <- plot +
annotation_custom(grob = leg2, xmin = 7, xmax = 10, ymin = 0, ymax = 4)
plotNew <- arrangeGrob(leg1, plotNew,
heights = unit.c(leg1$height, unit(1, "npc") - leg1$height), ncol = 1)
grid.newpage()
grid.draw(plotNew)
Using ggplot2and cowplot (= ggplot2 extension).
The approach is similar to Sandy's one as it takes out the legend as seperate objects and lets you do the placement independently. It was primarly designed for multiple legends which belong to two or more plots in a grid of plots.
The idea is as follows:
Create Plot1, Plot2,...,PlotX without legends
Create Plot1, Plot2,...,PlotX with legends
Extract legends from step 1 & 2 into separate objects
Set up legend grid and arrange legends they way you want to
Create grid combining plots and legends
It seems kinda complicated and time/code consuming but set up once, you can adapt and use it for every kind of plot/legend customization.
library(ggplot2)
library(cowplot)
# Some data
df <- data.frame(
Name = factor(rep(c("A", "B", "C"), 12)),
Month = factor(rep(1:12, each = 3)),
Temp = sample(0:40, 12),
Precip = sample(50:400, 12)
)
# 1. create plot1
plot1 <- ggplot(df, aes(Month, Temp, fill = Name)) +
geom_point(
show.legend = F, aes(group = Name, colour = Name),
size = 3, shape = 17
) +
geom_smooth(
method = "loess", se = F,
aes(group = Name, colour = Name),
show.legend = F, size = 0.5, linetype = "dashed"
)
# 2. create plot2
plot2 <- ggplot(df, aes(Month, Precip, fill = Name)) +
geom_bar(stat = "identity", position = "dodge", show.legend = F) +
geom_smooth(
method = "loess", se = F,
aes(group = Name, colour = Name),
show.legend = F, size = 1, linetype = "dashed"
) +
scale_fill_grey()
# 3.1 create legend1
legend1 <- ggplot(df, aes(Month, Temp)) +
geom_point(
show.legend = T, aes(group = Name, colour = Name),
size = 3, shape = 17
) +
geom_smooth(
method = "loess", se = F, aes(group = Name, colour = Name),
show.legend = T, size = 0.5, linetype = "dashed"
) +
labs(colour = "Station") +
theme(
legend.text = element_text(size = 8),
legend.title = element_text(
face = "italic",
angle = -0, size = 10
)
)
# 3.2 create legend2
legend2 <- ggplot(df, aes(Month, Precip, fill = Name)) +
geom_bar(stat = "identity", position = "dodge", show.legend = T) +
scale_fill_grey() +
guides(
fill =
guide_legend(
title = "",
title.theme = element_text(
face = "italic",
angle = -0, size = 10
)
)
) +
theme(legend.text = element_text(size = 8))
# 3.3 extract "legends only" from ggplot object
legend1 <- get_legend(legend1)
legend2 <- get_legend(legend2)
# 4.1 setup legends grid
legend1_grid <- cowplot::plot_grid(legend1, align = "v", nrow = 2)
# 4.2 add second legend to grid, specifying its location
legends <- legend1_grid +
ggplot2::annotation_custom(
grob = legend2,
xmin = 0.5, xmax = 0.5, ymin = 0.55, ymax = 0.55
)
# 5. plot "plots" + "legends" (with legends in between plots)
cowplot::plot_grid(plot1, legends, plot2,
ncol = 3,
rel_widths = c(0.45, 0.1, 0.45)
)
Created on 2019-10-05 by the reprex package (v0.3.0)
Changing the order of the final plot_grid() call moves the legends to the right:
cowplot::plot_grid(plot1, plot2, legends, ncol = 3,
rel_widths = c(0.45, 0.45, 0.1))
From my understanding, basically there is very limited control over legends in ggplot2. Here is a paragraph from the Hadley's book (page 111):
ggplot2 tries to use the smallest possible number of legends that accurately conveys the aesthetics used in the plot. It does this by combining legends if a variable is used with more than one aesthetic. Figure 6.14 shows an example of this for the points geom: if both colour and shape are mapped to the same variable, then only a single legend is necessary. In order for legends to be merged, they must have the same name (the same legend title). For this reason, if you change the name of one of the merged legends, you’ll need to change it for all of them.

How to merge legends for color and shape when geom_hline has a separate (additional) entry in the color legend?

I have the following code, which produces the following plot:
cols <- brewer.pal(n = 3, name = 'Dark2')
p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() +
geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) +
geom_line(position=pd) +
geom_point(aes(shape=Approach, colour = Approach), size = 4) +
geom_hline(aes(yintercept = cp.best$slope, colour = "C2P"), show_guide = FALSE) +
scale_color_manual(name="Approach", breaks=c("C2P", "P2P", "CP2P"), values = cols[c(1,3,2)]) +
scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal',
legend.position = 'top',
plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"),
text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))
p4 <- p4 + guides(colour=guide_legend(override.aes=list(shape=c(NA,17,16))))
p4
When I try show_guide = FALSE in geom_point, the shape of the point in the upper legend are all set to default solid circles.
How can I make the lower legend to disappear, without affecting the upper legend?
This is a solution, complete with reproducible data:
library("ggplot2")
library("grid")
library("RColorBrewer")
cp2p <- data.frame(xval = 10 * 2:15, yval = cumsum(c(0.55, rnorm(13, 0.01, 0.005))), Approach = "CP2P", stringsAsFactors = FALSE)
p2p <- data.frame(xval = 10 * 1:15, yval = cumsum(c(0.7, rnorm(14, 0.01, 0.005))), Approach = "P2P", stringsAsFactors = FALSE)
pd <- position_dodge(0.1)
cp.best <- list(slope = 0.65)
all.m <- rbind(p2p, cp2p)
all.m$Approach <- factor(all.m$Approach, levels = c("C2P", "P2P", "CP2P"))
all.m$se <- rnorm(29, 0.1, 0.02)
all.m[nrow(all.m) + 1, ] <- all.m[nrow(all.m) + 1, ] # Creates a new row filled with NAs
all.m$Approach[nrow(all.m)] <- "C2P"
cols <- brewer.pal(n = 3, name = 'Dark2')
p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() +
geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) +
geom_line(position=pd) +
geom_point(aes(shape=Approach, colour = Approach), size = 4, na.rm = TRUE) +
geom_hline(aes(yintercept = cp.best$slope, colour = "C2P")) +
scale_color_manual(values = c(C2P = cols[1], P2P = cols[2], CP2P = cols[3])) +
scale_shape_manual(values = c(C2P = NA, P2P = 16, CP2P = 17)) +
scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal',
legend.position = 'top',
plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"),
text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))
p4
The trick is to make sure that all of the desired levels of all.m$Approach appear in all.m, even if one of them gets dropped out of the graph. The warning about the omitted point is suppressed by the na.rm = TRUE argument to geom_point.
Short answer:
Just add a dummy geom_point layer (transparent points) where shape is mapped to the same level as in geom_hline.
geom_point(aes(shape = "int"), alpha = 0)
Longer answer:
Whenever possible, ggplot merges / combines legends of different aesthetics. For example, if colour and shape is mapped to the same variable, then the two legends are combined into one.
I illustrate this using simple data set with 'x', 'y' and a grouping variable 'grp' with two levels:
df <- data.frame(x = rep(1:2, 2), y = 1:4, grp = rep(c("a", "b"), each = 2))
First we map both color and shape to 'grp'
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4)
Fine, the legends for the aesthetics, color and shape, are merged into one.
Then we add a geom_hline. We want it to have a separate color from the geom_lines and to appear in the legend. Thus, we map color to a variable, i.e. put color inside aes of geom_hline. In this case we do not map the color to a variable in the data set, but to a constant. We may give the constant a desired name, so we don't need to rename the legend entries afterwards.
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4) +
geom_hline(aes(yintercept = 2.5, color = "int"))
Now two legends appears, one for the color aesthetics of geom_line and geom_hline, and one for the shape of the geom_points. The reason for this is that the "variable" which color is mapped to now contains three levels: the two levels of 'grp' in the original data, plus the level 'int' which was introduced in the geom_hline aes. Thus, the levels in the color scale differs from those in the shape scale, and by default ggplot can't merge the two scales into one legend.
How to combine the two legends?
One possibility is to introduce the same, additional level for shape as for color by using a dummy geom_point layer with transparent points (alpha = 0) so that the two aesthetics contains the same levels:
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4) +
geom_hline(aes(yintercept = 2.5, color = "int")) +
geom_point(aes(shape = "int"), alpha = 0) # <~~~~ a blank geom_point
Another possibility is to convert the original grouping variable to a factor, and add the "geom_hline level" to the original levels. Then use drop = FALSE in scale_shape_discrete to include "unused factor levels from the scale":
datadf$grp <- factor(df$grp, levels = c(unique(df$grp), "int"))
ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
geom_line() +
geom_point(size = 4) +
geom_hline(aes(yintercept = 2.5, color = "int")) +
scale_shape_discrete(drop = FALSE)
Then, as you already know, you may use the guides function to "override" the shape aesthetics in the legend, and remove the shape from the geom_hline entry by setting it to NA:
guides(colour = guide_legend(override.aes = list(shape = c(16, 17, NA))))

How do I position two legends independently in ggplot

Title pretty well covers it.
I have two legends, relating to size and colour, and wish to have one,say, on the top and one within the graph.
Is this possible and, if so, how
TIA
It can be done by extracting separate legends from plots, then arranging the legends in the relevant plot. The code here uses functions from the gtable package to do the extraction, then functions from the gridExtra package to do the arranging. The aim is to have a plot that contains a color legend and a size legend. First, extract the colour legend from a plot that contains the colour legend only. Second, extract the size legend from a plot that contains the size legend only. Third, draw a plot that contains no legend. Fourth, arrange the plot and the two legends into one new plot.
# Some data
df <- data.frame(
x = 1:10,
y = 1:10,
colour = factor(sample(1:3, 10, replace = TRUE)),
size = factor(sample(1:3, 10, replace = TRUE)))
library(ggplot2)
library(gridExtra)
library(gtable)
library(grid)
### Step 1
# Draw a plot with the colour legend
(p1 <- ggplot(data = df, aes(x=x, y=y)) +
geom_point(aes(colour = colour)) +
theme_bw() +
theme(legend.position = "top"))
# Extract the colour legend - leg1
leg1 <- gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box")
### Step 2
# Draw a plot with the size legend
(p2 <- ggplot(data = df, aes(x=x, y=y)) +
geom_point(aes(size = size)) +
theme_bw())
# Extract the size legend - leg2
leg2 <- gtable_filter(ggplot_gtable(ggplot_build(p2)), "guide-box")
# Step 3
# Draw a plot with no legends - plot
(plot <- ggplot(data = df, aes(x=x, y=y)) +
geom_point(aes(size = size, colour = colour)) +
theme_bw() +
theme(legend.position = "none"))
### Step 4
# Arrange the three components (plot, leg1, leg2)
# The two legends are positioned outside the plot:
# one at the top and the other to the side.
plotNew <- arrangeGrob(leg1, plot,
heights = unit.c(leg1$height, unit(1, "npc") - leg1$height), ncol = 1)
plotNew <- arrangeGrob(plotNew, leg2,
widths = unit.c(unit(1, "npc") - leg2$width, leg2$width), nrow = 1)
grid.newpage()
grid.draw(plotNew)
# OR, arrange one legend at the top and the other inside the plot.
plotNew <- plot +
annotation_custom(grob = leg2, xmin = 7, xmax = 10, ymin = 0, ymax = 4)
plotNew <- arrangeGrob(leg1, plotNew,
heights = unit.c(leg1$height, unit(1, "npc") - leg1$height), ncol = 1)
grid.newpage()
grid.draw(plotNew)
Using ggplot2and cowplot (= ggplot2 extension).
The approach is similar to Sandy's one as it takes out the legend as seperate objects and lets you do the placement independently. It was primarly designed for multiple legends which belong to two or more plots in a grid of plots.
The idea is as follows:
Create Plot1, Plot2,...,PlotX without legends
Create Plot1, Plot2,...,PlotX with legends
Extract legends from step 1 & 2 into separate objects
Set up legend grid and arrange legends they way you want to
Create grid combining plots and legends
It seems kinda complicated and time/code consuming but set up once, you can adapt and use it for every kind of plot/legend customization.
library(ggplot2)
library(cowplot)
# Some data
df <- data.frame(
Name = factor(rep(c("A", "B", "C"), 12)),
Month = factor(rep(1:12, each = 3)),
Temp = sample(0:40, 12),
Precip = sample(50:400, 12)
)
# 1. create plot1
plot1 <- ggplot(df, aes(Month, Temp, fill = Name)) +
geom_point(
show.legend = F, aes(group = Name, colour = Name),
size = 3, shape = 17
) +
geom_smooth(
method = "loess", se = F,
aes(group = Name, colour = Name),
show.legend = F, size = 0.5, linetype = "dashed"
)
# 2. create plot2
plot2 <- ggplot(df, aes(Month, Precip, fill = Name)) +
geom_bar(stat = "identity", position = "dodge", show.legend = F) +
geom_smooth(
method = "loess", se = F,
aes(group = Name, colour = Name),
show.legend = F, size = 1, linetype = "dashed"
) +
scale_fill_grey()
# 3.1 create legend1
legend1 <- ggplot(df, aes(Month, Temp)) +
geom_point(
show.legend = T, aes(group = Name, colour = Name),
size = 3, shape = 17
) +
geom_smooth(
method = "loess", se = F, aes(group = Name, colour = Name),
show.legend = T, size = 0.5, linetype = "dashed"
) +
labs(colour = "Station") +
theme(
legend.text = element_text(size = 8),
legend.title = element_text(
face = "italic",
angle = -0, size = 10
)
)
# 3.2 create legend2
legend2 <- ggplot(df, aes(Month, Precip, fill = Name)) +
geom_bar(stat = "identity", position = "dodge", show.legend = T) +
scale_fill_grey() +
guides(
fill =
guide_legend(
title = "",
title.theme = element_text(
face = "italic",
angle = -0, size = 10
)
)
) +
theme(legend.text = element_text(size = 8))
# 3.3 extract "legends only" from ggplot object
legend1 <- get_legend(legend1)
legend2 <- get_legend(legend2)
# 4.1 setup legends grid
legend1_grid <- cowplot::plot_grid(legend1, align = "v", nrow = 2)
# 4.2 add second legend to grid, specifying its location
legends <- legend1_grid +
ggplot2::annotation_custom(
grob = legend2,
xmin = 0.5, xmax = 0.5, ymin = 0.55, ymax = 0.55
)
# 5. plot "plots" + "legends" (with legends in between plots)
cowplot::plot_grid(plot1, legends, plot2,
ncol = 3,
rel_widths = c(0.45, 0.1, 0.45)
)
Created on 2019-10-05 by the reprex package (v0.3.0)
Changing the order of the final plot_grid() call moves the legends to the right:
cowplot::plot_grid(plot1, plot2, legends, ncol = 3,
rel_widths = c(0.45, 0.45, 0.1))
From my understanding, basically there is very limited control over legends in ggplot2. Here is a paragraph from the Hadley's book (page 111):
ggplot2 tries to use the smallest possible number of legends that accurately conveys the aesthetics used in the plot. It does this by combining legends if a variable is used with more than one aesthetic. Figure 6.14 shows an example of this for the points geom: if both colour and shape are mapped to the same variable, then only a single legend is necessary. In order for legends to be merged, they must have the same name (the same legend title). For this reason, if you change the name of one of the merged legends, you’ll need to change it for all of them.

Resources