I'm using a shared legend in R, which is using this code:
g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
Reference: Combining grid_arrange_shared_legend() and facet_wrap_labeller() in R
What is "structure(c(3L,3L,……))" in my legend? How to avoid it?
My code:
library(RColorBrewer)
library(ggplot2)
library(gridExtra)
wq <- read.csv('wineQualityReds.csv')
quality_factor <- factor(wq$quality)
plot_three <- function(feature) {
ggplot(data=wq,
aes_string(x = feature, y = wq$pH, color = quality_factor)) +
geom_tile() +
scale_color_brewer(palette='Blues') +
geom_smooth(method = loess, aes(group = 1)) + theme(legend.position = "none")
}
ph1 <- plot_three("volatile.acidity") +
labs(x='volatile.acidity(g / dm^3)', y='pH')
ph2 <- plot_three("citric.acid") +
labs(x='citric.acid(g / dm^3)', y='pH')
ph3 <- plot_three("chlorides") +
labs(x='chlorides(g / dm^3)', y='pH')
ph4 <- plot_three("free.sulfur.dioxide") +
labs(x='free.sulfur.dioxide(mg / dm^3)', y='pH')
ph5 <- plot_three("sulphates") +
labs(x='sulphates(g / dm^3)', y='pH')
ph6 <- plot_three("alcohol") +
labs(x='alcohol(% by volume)', y='pH')
g <- ggplotGrob(ph1 + theme(legend.position="bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
grid.arrange(arrangeGrob(ph1, ph2, ph3, ph4, ph5, ph6, ncol =3),
legend, top = "Relationship between pH and features via different quality", heights=c(9,1))
Dataset: redwineQuality
https://s3.amazonaws.com/udacity-hosted-downloads/ud651/wineQualityReds.csv