# install.packages('ggplot2') 安装ggplot2包 install.packages('gcookbook')
# 安装gcookbook包
library(ggplot2) #加载ggplot2包
library(gcookbook) #加载本书的数据包
library(grid)
head(faithful)
## eruptions waiting
## 1 3.600 79
## 2 1.800 54
## 3 3.333 74
## 4 2.283 62
## 5 4.533 85
## 6 2.883 55
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_point()
p + annotate("text", x = 4.5, y = 48, label = "Group 1") + annotate("text",
x = 4.5, y = 66, label = "Group 2")
p + annotate("text", x = 3, y = 48, label = "Group 1", family = "serif", fontface = "italic",
colour = "darkred", size = 3)
# 当希望添加独立的文本对象时,千万不要使用geom_text()。annotate(geom='text')会向图形添加一个单独的文本对象,而geom_text()却会根据数据创建许多的文本对象
p + annotate("text", x = 3, y = 48, label = "Group 1", alpha = 0.1) + geom_text(x = 4.5,
y = 66, label = "Group 2", alpha = 0.1)
# 如果坐标轴是连续型的,可以使用特殊字符x=-Inf,y=-Inf在绘图区域的边缘放置文本注解,同时也需要使用hjust和vjust来调整文本相对于边角的位置
p + annotate("text", x = -Inf, y = Inf, label = "Upper left", hjust = -0.2,
vjust = 2) + annotate("text", x = mean(range(faithful$eruptions)), y = -Inf,
vjust = -0.4, label = "Bottom middle")
p <- ggplot(data.frame(x = c(-3, 3)), aes(x = x)) + stat_function(fun = dnorm)
p + annotate("text", x = 2, y = 0.3, parse = TRUE, label = "frac(1,sqrt(2*pi))*e^({-x^2/2})")
p + annotate("text", x = 0, y = 0.05, parse = TRUE, size = 4, label = "'Function:' * y==frac(1,sqrt(2*pi)) *e^{-x^2/2}")
p <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) + geom_point()
p + geom_hline(yintercept = 60) + geom_vline(xintercept = 14)
p + geom_abline(intercept = 37.5, slope = 1.75)
p <- ggplot(subset(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
p + annotate("segment", x = 1950, xend = 1980, y = -0.25, yend = -0.25)
library(grid) #为了使用arrow()
p + annotate("segment", x = 1850, xend = 1820, y = -0.8, yend = -0.95, colour = "blue",
size = 2, arrow = arrow()) + annotate("segment", x = 1950, xend = 1980,
y = -0.25, yend = -0.25, arrow = arrow(ends = "both", angle = 90, length = unit(0.2,
"cm")))
p <- ggplot(subset(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
p + annotate("rect", xmin = 1950, xmax = 1980, ymin = -1, ymax = 1, alpha = 0.1,
fill = "blue")
p + annotate("rect", xmin = 1950, xmax = 1980, ymin = -Inf, ymax = Inf, alpha = 0.1,
fill = "blue") #ymin=-Inf,ymax=Inf将阴影的延生到y轴上下边界
p + annotate("rect", xmin = -Inf, xmax = Inf, ymin = -0.25, ymax = 0, alpha = 0.1,
fill = "blue")
#基本图形
p<-ggplot(mpg,aes(x=displ,y=hwy))+geom_point()+facet_grid(.~drv)
p
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)
#如果使用annotate(),标签将所有分面上出现
p+annotate("text",x=6,y=40,label="label text")
#此函数返回一个数据框,其中的字符串表示回归公式和r^2值,这些字符将被认为是R中的数字表达式
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,stringsAsFactors = FALSE)
}
lm_labels(mpg)
## formula r2
## 1 italic(y) == 35.70 -3.53 * italic(x) italic(R^2)== 0.59
library(plyr)
labels<-ddply(mpg,"drv",lm_labels)
labels
## drv formula r2
## 1 4 italic(y) == 30.68 -2.88 * italic(x) italic(R^2)== 0.65
## 2 f italic(y) == 37.38 -3.60 * italic(x) italic(R^2)== 0.36
## 3 r italic(y) == 25.78 -0.92 * italic(x) italic(R^2)== 0.04
p+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)