ggplot2 legend : Easy steps to change the position and the appearance of a graph legend in R software
ggplot2图例:轻松更改图例的位置和外观
本教程的目的是描述如何更改使用ggplot2包生成的图的图例。
(一):图例位置
rm(list = ls())
#convert dose column from a numeric to a factor variable
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
p1 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
#Change the legend position
p2 <- p1 + theme(legend.position="top")
p3 <- p1 + theme(legend.position="bottom")
#the argument legend.position can be also a numeric vector c(x,y).
p4 <- p1 + theme(legend.position=c(0.8,0.2))
library(ggpubr)
ggarrange(p1, p2, p3, p4,
labels = c("A","B","C","D"),
nrow = 2, ncol = 2)
(二):图例格式
# Change the legend title and text font styles
# legend title
p5 <- p1 + theme(legend.title = element_text(colour="blue", size=10,
face="bold"))
# legend labels
p6 <- p1 + theme(legend.text = element_text(colour="blue", size=10,
face="bold"))
ggarrange(p5, p6,
labels = c("A","B"),
nrow = 1)
(三):图例背景、顺序
# Change the background color of the legend box
p7 <- p1 + theme(legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid"))
p8 <- p1 + theme(legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid",
colour ="darkblue"))
ggarrange(p7, p8,
labels = c("A","B"),
nrow = 1)
# Change the order of legend items
p9 <- p1 + scale_x_discrete(limits=c("2", "0.5", "1"))
ggarrange(p7, p8,p9,
labels = c("A","B","C"),
nrow = 1)
(四):条形图图例
#删除条形图例中的斜线
# Default plot
p10 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) + geom_bar()
# Change bar plot border color, but slashes are added in the legend
p11 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) +
geom_bar(colour="black")
# Hide the slashes:
#1. plot the bars with no border color,
#2. plot the bars again with border color, but with a blank legend.
p12 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose))+
geom_bar() +
geom_bar(colour="black", show_guide=FALSE)
ggarrange(p10, p11, p12,
labels = c("A","B","C"),
nrow = 1)
(五):图例位置、顺序
# guides() : set or remove the legend for a specific aesthetic
mtcars$cyl<-as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)
head(mtcars)
m <- ggplot(data = mtcars,
aes(x=mpg, y=wt, color=cyl, size=qsec, shape=gear))+
geom_point()
# Change the legend position
m1 <- m + theme(legend.position="bottom")
m2 <- m + guides(color = guide_legend(order=1),
size = guide_legend(order=2),
shape = guide_legend(order=3))
m3 <- qplot(data = mpg, x = displ, y = cty, size = hwy,
colour = cyl, shape = drv) +
guides(colour = guide_colourbar(order = 1),
alpha = guide_legend(order = 2),
size = guide_legend(order = 3))
ggarrange(m1, m2, m3,
labels = c("A","B","C"),
nrow = 1)
(六):图例增删
# Remove a legend for a particular aesthetic
m4 <- m + guides(color = FALSE, size = FALSE)
# Remove legend for the point shape
m5 <- m + scale_shape(guide=FALSE)
# Remove legend for size
m6 <- m + scale_size(guide=FALSE)
# Remove legend for color
m7 <- m + scale_color_manual(values=c('#999999','#E69F00','#56B4E9'),
guide=FALSE)
ggarrange(m4, m5, m6,m7,
labels = c("A","B","C","D"),
nrow = 2, ncol = 2)