title: "ggplot2 上篇"
author: "li_volleyball"
date: "2016年4月16日"
output: word_document
单变量
连续
library(ggplot2)
a<-ggplot(mpg,aes(hwy))
a+geom_area(stat = 'bin')
a+geom_area(stat = 'bin',binwidth=20)
a+geom_area(stat = 'bin',color='red')
a+geom_area(stat = 'bin',fill='yellow',color='black')
a+geom_area(stat = 'bin',fill='yellow',color='black',linetype=2,size=2)
b<-ggplot(mpg,aes(hwy))
b+geom_area(aes(y=..density..),stat='bin')
a+geom_density(kernel="gaussian")
a+geom_dotplot()
a+geom_freqpoly()
a+geom_histogram(binwidth = 5)
离散
b1<-ggplot(mpg,aes(fl))
b1+geom_bar()
a+geom_bar()
Graphical Primitives
library(maps)
world_map<-map_data("world")
c<-ggplot(world_map,aes(long,lat))
c+geom_polygon(aes(group=group))
d<-ggplot(economics,aes(date,unemploy))
d+geom_path()
d+geom_ribbon(aes(ymin=unemploy-900,ymax=unemploy+900))
e<-ggplot(seals,aes(x=long,y=lat))
e+geom_segment(aes(xend=long+delta_long,yend=lat+delta_lat),
arrow = arrow(length = unit(0.1,"cm"))) +
borders("state")
e+geom_rect(aes(xmin=long,ymin=lat,xmax=long+delta_long,ymax=lat+delta_lat))
双变量
连续x,连续y
f<-ggplot(mpg,aes(cty,hwy))
f+geom_blank() #清空图层
f+geom_point() #绘制散点图
f+geom_point(aes(shape=factor(cyl)))
f+geom_point(aes(colour=factor(model)))
f+geom_jitter() #通过在离散型分布上添加随机噪声以避免遮盖绘制问题
f+geom_jitter(aes(colour=factor(cyl)))+geom_quantile()
#引入线性分位回归,quantile regression
m <- ggplot(mpg, aes(displ, 1 / hwy)) + geom_point()
m
m + geom_quantile()
m + geom_quantile(quantiles = 0.5)
q10 <- seq(0.05, 0.95, by = 0.05)
m + geom_quantile(quantiles = q10)
m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
f+geom_jitter()+geom_rug(sides = "bl") # bl bottom/left
f+geom_smooth(span=2)
f+geom_jitter()+geom_smooth(span=2)
f+geom_jitter()+geom_smooth(span=2,method = "lm",se=F)
f+geom_text(aes(label=cty))
f+geom_jitter(aes(colour=factor(cty)))+geom_text(aes(label=cty))
x离散 y连续
g<-ggplot(mpg,aes(class,hwy))
g+geom_bar(stat="identity")
g+geom_boxplot(aes(colour=factor(class)))
g+geom_dotplot(binaxis = "y",stackdir = "center",aes(colour=factor(class)))
g+geom_violin(scale = "area",aes(fill=factor(class)))
x离散 y离散
h<-ggplot(diamonds,aes(cut,color))
h+geom_jitter(aes(colour=factor(cut)))
连续二维分布
i<-ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)
i+geom_jitter()
i+geom_bin2d()
i+ geom_bin2d(bins = 10)
i + geom_bin2d(bins = 30)
i+geom_bin2d(binwidth=c(0.1,0.1))
i+geom_density2d()
i+geom_hex() #六边形
连续函数
j<-ggplot(economics,aes(date,unemploy))
j+geom_area()
j+geom_line()
j+geom_step(direction="hv")
可视化误差 error
df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
k <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))
k+geom_crossbar(fatten=2)
k+geom_errorbar()
k+geom_linerange()
k+geom_pointrange()
地图
data <- data.frame(murder = USArrests$Murder,
state = tolower(rownames(USArrests)))
head(data)
map <- map_data("state")
head(map)
l <- ggplot(data, aes(fill = murder))
l + geom_map(aes(map_id = state), map=map)+expand_limits(x= map$long, y= map$lat)
三变量
head(seals)
seals$z <- with(seals, sqrt(long^2 +lat^2))
m <- ggplot(seals, aes(long, lat))
m + geom_contour(aes(z = z))
m + geom_raster(aes(fill = z), hjust=0.5,vjust=0.5, interpolate=FALSE) #光栅
m + geom_tile(aes(fill = z)) #瓦片