R数据可视化-7 注解
绘图中的说明元素除了坐标轴标签、刻度线和图例等,还可以包括自由添加的图形元素和说明文本,这些注解可以为你的读者提供更多的信息。
1 添加文本注解
使用annotate()生成一条文本注解,通过x和y指定文本位置,可以是具体数值或者Inf和-Inf,表示图形的边缘,使用hjust和vjust进行水平方向和竖直方向上的微调,使用family、color、size分别指定字体、颜色、大小。
1 |
ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point() + annotate("text", x=3, y=48, label="Group 1", family="serif", color="darkred", size=5) + annotate("text", x=mean(range(faithful$eruptions)), y=-Inf, label="Group 2", vjust=-1) |
2 添加数学表达式
还是使用annotate(),不过需要制定parse为TRUE,表示对文本进行公式解析。
1 |
ggplot(data.frame(x=c(-3, 3)), aes(x=x)) + stat_function(fun=dnorm) + annotate("text", x=0, y=0.05, parse=TRUE, size=4, label="'Function: ' * y==frac(1, sqrt(2*pi)) * e^{-x^2/2}") |
更多和公式语法有关的内容可参考?plotmath,更多数学表达式的图示可参考?demo(plotmath)。
3 添加直线
使用geom_hline()、geom_vline()、geom_abline()分别绘制水平线、竖直线和有角度的线。如果x轴或y轴为类别型变量,则第一个水平为数值1,第二个水平为数值2,依此类推。
1 2 |
library(gcookbook) ggplot(heightweight, aes(x=ageYear, y=heightIn, color=sex)) + geom_point() + geom_hline(yintercept=60) + geom_vline(xintercept=14) + geom_abline(intercept=37.4, slope=1.75) |
同样可以通过指定类别型变量绘制多条直线。
1 2 3 |
library(plyr) hw_means <- ddply(heightweight, "sex", summarise, heightIn=mean(heightIn)) ggplot(heightweight, aes(x=ageYear, y=heightIn, color=sex)) + geom_point() + geom_hline(aes(yintercept=heightIn, color=sex), data=hw_means, linetype="dashed", size=1) |
4 添加线段和箭头
在annotate()中指定segment可以添加线段,还可以为线段添加箭头,箭头默认角度angle为30度,默认长度length为0.2英寸,使用x、xend、y、yend指定线段的起始位置。如果x轴或y轴为类别型变量,则相应地第一个水平使用数值1,第二个水平使用数值2,依次类推。
1 2 |
library(grid) ggplot(subset(climate, Source=="Berkeley"), aes(x=Year, y=Anomaly10y)) + geom_line() + annotate("segment", x=1850, xend=1820, y=-.8, yend=-.95, color="blue", size=2, arrow=arrow()) + annotate("segment", x=1950, xend=1980, y=-.25, yend=-.25, arrow=arrow(ends="both", angle=90, length=unit(.2, "cm"))) |
5 添加矩形阴影
在annotate()中指定rect可以添加矩形,其实只要传递了合适的参数,任意几何对象都可以配合annotate()使用。
1 2 |
library(gcookbook) ggplot(subset(climate, Source=="Berkeley"), aes(x=Year, y=Anomaly10y)) + geom_line() + annotate("rect", xmin=1950, xmax=1980, ymin=-1, ymax=1, alpha=.1, fill="blue") |
6 向独立分面添加注解
使用分面变量生成一个新的数据框,并设定每个分面要绘制的值,然后配合新数据框使用geom_text()。
1 2 3 |
p <- ggplot(mpg, aes(x=displ, y=hwy)) + geom_point() + facet_grid(.~drv) f_labels <- data.frame(drv=c("4", "f", "r"), label=c("4wd", "Front", "Rear")) p + geom_text(x=6, y=40, aes(label=label), data=f_labels) |
再来一个为每个分面添加拟合直线公式的例子。
1 2 3 4 5 6 7 8 9 10 |
lm_labels <- function(dat) { mod <- lm(hwy ~ displ, data=dat) formula <- sprintf("italic(y) == %.2f%+.2f * italic(x)", round(coef(mod)[1], 2), round(coef(mod)[2], 2)) r <- cor(dat$displ, dat$hwy) r2 <- sprintf("italic(R^2) == %.2f", r^2) data.frame(formula=formula, r2=r2, sringAsFactors=FALSE) } library(plyr) labels <- ddply(mpg, "drv", lm_labels) ggplot(mpg, aes(x=displ, y=hwy)) + geom_point() + facet_grid(.~drv) + geom_smooth(method=lm, se=FALSE) + geom_text(x=3, y=40, aes(label=formula), data=labels, parse=TRUE, hjust=0) + geom_text(x=3, y=35, aes(label=r2), data=labels, parse=TRUE, hjust=0) |
18
2016-08