ggplot2 Graphical Parameters
- Main title, axis labels and legend title
- Legend position and appearance
- Change colors automatically and manually
- Point shapes, colors and size
- Add text annotations to a graph
- Line types
- Themes and background colors
- Axis scales and transformations
- Axis ticks: customize tick marks and labels, reorder and select items
- Add straight lines to a plot: horizontal, vertical and regression lines
- Rotate a plot: flip and reverse
- Faceting: split a plot into a matrix of panels
1.標(biāo)題:主標(biāo)題,軸和圖例標(biāo)題
ggplot2軟件包修改繪圖標(biāo)題(主標(biāo)題脯倒,軸標(biāo)簽和圖例標(biāo)題)主要是通過以下函數(shù):
- ggtitle(label)#主標(biāo)題
- xlab(label)#x軸標(biāo)簽
- ylab(label)#y軸標(biāo)簽
- labs(...)#用于主標(biāo)題播急,軸標(biāo)簽和圖例標(biāo)題
1.1 數(shù)據(jù)準(zhǔn)備
# 將dose轉(zhuǎn)換weight因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# 檢查數(shù)據(jù)
head(ToothGrowth)
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
p
1.2 更改主標(biāo)題及軸標(biāo)題
1.2.1 使用 ggtitle(), xlab() and ylab()
p + ggtitle("Plot of length \n by dose") +
xlab("Dose (mg)") + ylab("Teeth length")
1.2.2 使用labs()
p +labs(title="Plot of length \n by dose",
x ="Dose (mg)", y = "Teeth length")
1.3 使用labs()更改圖例
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+
geom_boxplot()
p
# 更改圖例
p1 <- p + labs(fill = "Dose (mg)")
1.4 更改主標(biāo)題和軸標(biāo)簽的外觀
可以使用theme()和element_text()函數(shù)自定義主標(biāo)題以及x和y軸標(biāo)簽
# main title
p + theme(plot.title = element_text(family, face, colour, size))
# x axis title
p + theme(axis.title.x = element_text(family, face, colour, size))
# y axis title
p + theme(axis.title.y = element_text(family, face, colour, size))
## family : font family
## face : font face. Possible values are “plain”, “italic”, “bold” and “bold.italic”
## colour : text color
## size : text size in pts
## hjust : horizontal justification (in [0, 1])
## vjust : vertical justification (in [0, 1])
## lineheight : line height. In multi-line text, the lineheight argument is used to change the
## spacing between lines.
## color : an alias for colour
- example
# Default plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
ggtitle("Plot of length \n by dose") +
xlab("Dose (mg)") + ylab("Teeth length")
p
# Change the color, the size and the face of
# the main title, x and y axis labels
p1 <- p + theme(
plot.title = element_text(color="red", size=14, face="bold.italic"),
axis.title.x = element_text(color="blue", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold")
)
ggarrange(p,p1)
1.5 移除x和y軸標(biāo)簽
可以使用element_blank()函數(shù)隱藏主要標(biāo)題和軸標(biāo)簽
p + theme(
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())
2 圖例的位置及外觀
2.1 數(shù)據(jù)準(zhǔn)備
# 將dose轉(zhuǎn)換weight因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# 檢查數(shù)據(jù)
head(ToothGrowth)
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p
2.2 更改圖例位置
可以使用theme()函數(shù)來更改圖例的位置
# legend.position are : “l(fā)eft”,“top”, “right”, “bottom”,vector c(x,y)
p1 <- p + theme(legend.position="top")
p2 <- p + theme(legend.position="bottom")
p3 <- p + theme(legend.position = c(0.8, 0.2))
ggarrange(p1,p2,p3,nrow=1)
2.3 更改圖例標(biāo)題和文本字體樣式
# 圖例標(biāo)題
p4 <- p + theme(legend.title = element_text(colour="blue", size=10,
face="bold"))
# 圖例標(biāo)簽
p5 <- p + theme(legend.text = element_text(colour="blue", size=10,
face="bold"))
ggarrange(p4,p5)
2.4 更改圖例框的背景顏色
# 背景顏色修改
p6 <- p + theme(legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid"))
p7 <- p + theme(legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid",
colour ="darkblue"))
ggarrange(p6,p7)
2.5 更改圖例項(xiàng)的順序
p + scale_x_discrete(limits=c("2", "0.5", "1"))
2.6 移除圖例
# Remove only the legend title
p8 <- p + theme(legend.title = element_blank())
# Remove the plot legend
p9 <- p + theme(legend.position='none')
ggarrange(p8,p9)
2.7 刪除條形圖例中的斜線
# 默認(rèn)圖
p10 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) + geom_bar()
# 更改條形圖邊框顏色饿肺,
# 但在圖例中添加了斜杠
p11 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) +
geom_bar(colour="black")
p11
# 隱藏斜線:
# 1.繪制沒有邊框顏色的條,
# 2.再次用邊框顏色繪制條形,但帶有空白圖例匿又。
p12 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose))+
geom_bar() +
geom_bar(colour="black", show.legend = FALSE)
ggarrange(p10,p11,p12,nrow = 1)
然而很尷尬,第二幅圖的圖例并沒有出現(xiàn)斜線
3. guides() 設(shè)置或刪除圖例以獲得特定的美感
3.1 設(shè)置或刪除具有特定美感的圖例(填充建蹄,顏色碌更,大小,形狀等)
# Prepare the data : convert cyl and gear to factor variables
mtcars$cyl<-as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)
head(mtcars)
p <- ggplot(data = mtcars,
aes(x=mpg, y=wt, color=cyl, size=qsec, shape=gear))+
geom_point()
# Print the plot without guide specification
p
3.2 更改多個(gè)指南的圖例位置
# Change the legend position
p +theme(legend.position="bottom")
# Horizontal legend box
p +theme(legend.position="bottom", legend.box = "horizontal")
3.3 更改多個(gè)圖例的順序
p+guides(color = guide_legend(order=1),
size = guide_legend(order=2),
shape = guide_legend(order=3))
如果使用連續(xù)顏色洞慎,則可以使用功能guide_colourbar()更改顏色指南的順序:
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))
3.4 刪除具有特殊美感的圖例
p+guides(color = FALSE, size = FALSE)
# Remove legend for the point shape
p1 <- p+scale_shape(guide=FALSE)
# Remove legend for size
p2 <- p +scale_size(guide=FALSE)
# Remove legend for color
p3 <- p + scale_color_manual(values=c('#999999','#E69F00','#56B4E9'),
guide=FALSE)
ggarrange(p1,p2,p3,nrow = 1)