ggplot2 增添数据 图上添加信息 时间线背景分区
向原有线中增添数据,加入人的信息
(unemp <- qplot(date, unemploy, data=economics, geom="line",
xlab = "", ylab = "No. unemployed (1000s)")) ## 基础图层
presidential <- presidential[-(1:3), ] ##只保存总统名字和任期时间
yrng <- range(economics$unemploy) ##获取范围,会得到最大值和最小值
xrng <- range(economics$date)
unemp + geom_vline(aes(xintercept = as.numeric(start)), data = presidential) ##增加辅助线
unemp + geom_rect(aes(NULL, NULL, xmin = start, xmax = end,
fill = party), ymin = yrng[1], ymax = yrng[2],
data = presidential) + scale_fill_manual(values =
alpha(c("blue", "red"), 0.2)) ##采用party进行背景填充,xy为null
last_plot() + geom_text(aes(x = start, y = yrng[1], label = name),
data = presidential, size = 3, hjust = 0, vjust = 0)
caption <- paste(strwrap("Unemployment rates in the US have
varied a lot over the years", 40), collapse="\n")##以空格连接
unemp + geom_text(aes(x, y, label = caption),
data = data.frame(x = xrng[2], y = yrng[2]),
hjust = 1, vjust = 1, size = 4) ##放入对应的位置
Reference lines: horizontal, vertical, and diagonal
geom_abline(mapping = NULL, data = NULL, ..., slope, intercept,
na.rm = FALSE, show.legend = NA)
geom_hline(mapping = NULL, data = NULL, ..., yintercept, na.rm = FALSE,
show.legend = NA)
geom_vline(mapping = NULL, data = NULL, ..., xintercept, na.rm = FALSE,
show.legend = NA)
Rectangles
Description
geom_rect and geom_tile do the same thing, but are parameterised differently: geom_rect uses the locations of the four corners (xmin, xmax, ymin and ymax), while geom_tile uses the center of the tile and its size (x, y, width, height). geom_raster is a high performance special case for when all the tiles are the same size.
Usage
geom_raster(mapping = NULL, data = NULL, stat = "identity",
position = "identity", ..., hjust = 0.5, vjust = 0.5,
interpolate = FALSE, na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE)
geom_rect(mapping = NULL, data = NULL, stat = "identity",
position = "identity", ..., na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE)
geom_tile(mapping = NULL, data = NULL, stat = "identity",
position = "identity", ..., na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE)

浙公网安备 33010602011771号