【4.3】ggplot2--对图形的一些调整

1.标题换行

> library(gcookbook)
> library('ggplot2')
> p = ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
> p + ggtitle("Age and Height of\n Schoolchildren")

ggtitle跟 labs(title = "Title text")等价

还有一种方式
# Move the title inside
p + ggtitle("Age and Height of Schoolchildren") +
	theme(plot.title=element_text(vjust = -2.5))
这种方式的缺点是仍然保留了title的位置
	
# Use a text annotation instead
p + annotate("text", x=mean(range(heightweight$ageYear)), y=Inf,
			 label="Age and Height of Schoolchildren", vjust=1.5, size=6)

2.改变text

library(gcookbook) # For the data set
# Base plot
p = ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
# Controlling appearance of theme items
p + theme(axis.title.x=element_text(size=16, lineheight=.9, family="Times",face="bold.italic", colour="red"))
p + ggtitle("Age and Height\nof Schoolchildren") +
	theme(plot.title=element_text(size=rel(1.5), lineheight=.9, family="Times", face="bold.italic", colour="red"))
	
# rel(1.5) means that the font will be 1.5 times the base font size of the theme.
# For theme elements, font size is in points.
To set the appearance of text geoms (text that’s in the plot itself, with geom_text() or annotate()), set the text properties. For example:
p + annotate("text", x=15, y=53, label="Some text", size = 7, family="Times",fontface="bold.italic", colour="red")

p + geom_text(aes(label=weightLb), size=4, family="Times", colour="red")

ggplot涉及到改变文本的又两种方式:theme和text geom:

3.改变theme

> p = ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
> p + theme_grey()
> p + theme_bw()

library(gcookbook) # For the data set
# Base plot
p = ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point()

# Options for the plotting area
p + theme(
	panel.grid.major = element_line(colour="red"),
	panel.grid.minor = element_line(colour="red", linetype="dashed", size=0.2),
	panel.background = element_rect(fill="lightblue"),
	panel.border = element_rect(colour="blue", fill=NA, size=2))

# Options for text items
p + ggtitle("Plot title here") +
	theme(
	axis.title.x = element_text(colour="red", size=14),
	axis.text.x  = element_text(colour="blue"),
	axis.title.y = element_text(colour="red", size=14, angle = 90),
	axis.text.y  = element_text(colour="blue"),
	plot.title = element_text(colour="red", size=20, face="bold"))

# Options for the legend
p + theme(legend.background = element_rect(fill="grey85", colour="red", size=1),legend.title = element_text(colour="blue", face="bold", size=14),

4.自定义背景

library(gcookbook) # For the data set
# Start with theme_bw() and modify a few things
mytheme = theme_bw() +
	theme(text       = element_text(colour="red"),
		  axis.title = element_text(size = rel(1.25)))
# Base plot
p = ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
# Plot with modified theme
p + mytheme

5.网格线

library(gcookbook) # For the data set
p = ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
p + theme(panel.grid.major = element_blank(),
		  panel.grid.minor = element_blank())

 #Hide the vertical grid lines (which intersect with the x-axis)
p + theme(panel.grid.major.x = element_blank(),
		  panel.grid.minor.x = element_blank())
# Hide the horizontal grid lines (which intersect with the y-axis)
p + theme(panel.grid.major.y = element_blank(),
		  panel.grid.minor.y = element_blank())

参考资料:

R Graphics Cookbook by Winston Chang (O’Reilly)

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn