分享下面這本書,手把手ggplot2基礎資料,自己整理了一番,長文,建議參考目錄,選擇性閱讀~~
手把手ggplot2資料,可幫助快速繪制下面這樣的圖~~~~
- 拼圖
- 個性化圖例
- 資料變換
- 多組柱狀圖
- 添加誤差棒
- 添加輔助線
- 分面圖
。。。。。。
目錄
1、ggplot2繪制基礎條形圖和線形圖(basic bar or line graphs)
1.1、預設條形圖
1.2、不同柱子上不同色、添加圖例
1.3、柱子添加黑色外框
1.4、給條形圖添加标題、設定柱子填充色、關閉圖例
1.5、資料集計數條形圖
1.6、基礎線性圖
1.7、線性圖添加資料點
1.8、設定線形圖線型及點的形狀
1.9、設定線性圖的标題
1.10、多組資料堆積條形圖
1.11、多組資料并列條形圖
1.12、多組資料并列條形圖修改柱子填充色、外框色
1.13、修改x軸變量
1.14、多組資料線性圖
1.15、多組資料線性圖按變量填色
16、多組資料線性圖按變量使用不同圖形标記
17、完美的柱狀圖
18、完美的線性圖
2、ggplot2添加誤差棒和均值(means and error bars)
2.1、預設添加折線圖誤差棒
2.2、折線圖誤差棒防重疊
2.3、按照95%置信區間添加折線圖誤差棒
2.4、修改折線圖誤差棒顔色
2.5、完美的誤差棒折線圖
2.6、柱狀圖添加誤差棒
2.7、按照95%置信區間添加柱狀圖誤差棒
2.8、完美的帶誤差棒的條形圖
3、分布圖
3.1、簡單直方圖
3.2、輪廓直方圖
3.3、核密度圖
3.4、核密度圖結合直方圖
3.5、直方圖添加均值線
3.6、多組資料預設直方圖
3.9、多組資料連接配接直方圖
3.10、多組資料核密度圖
3.11、多組資料直方圖添加均值線
3.12、多組資料直方圖添加均值線
3.13、分面多組資料直方圖
3.14、分面多組資料直方圖添加均值線
3.15、基礎箱圖
3.16、不同變量填充不同色箱圖、圖例
3.17、不同變量填充不同色箱圖、無圖例
3.18、水準箱圖
3.19、箱圖添加均值點
4、ggplot2繪制散點圖
4.1、基礎散點圖
4.2、散點圖添加趨勢線
4.3、散點圖添加置信區間區域
4.4、散點圖不同變量添加不同色
4.5、散點圖不同變量添加不同色、添加趨勢線
4.6、散點圖不同變量設定不同标記
5、ggplot2設定标題
5.1、預設标題格式
5.2、長标題分行顯示
5.3、标題字型設定
6、坐标軸|刻度值|刻度設定
6.1、調整變量順序
6.2、設定刻度标簽名稱
6.3、隐藏網格線、刻度标簽、短标記線
6.4、設定刻度範圍
6.5、tick markers隐藏
6.6、刻度機關轉換為對數, 指數、開方等
6.7、刻度字型等屬性設定
7、ggplot2圖例設定
7.1、預設圖例
7.2、三種方法不顯示圖例
7.3、修改圖例顯示順序
7.4、去掉圖例标題
7.5、圖例标題屬性修改(legend titles and labels)
7.6、多特征圖例
7.7、圖例标題字号、顔色設定
7.8、圖例添加背景色
7.9、圖例位置
8、ggplot2添加輔助線
8.1、所有柱子添加水準直線
8.2、每個柱子各自添加輔助線
8.3、分面圖中添加輔助線
9、ggplot2圖形分面
9.1、y軸方向分面
9.2、x軸方向分面
9.3、x軸y軸方向同時分面
9.4、分面為指定的行數和列數
9.5、分面标簽個性化設定(背景色、字型、字号等)
9.6、各分面圖軸刻度個性化
10、ggplot2拼圖
11、ggplot2圖形儲存
11.1、R預設儲存方式
11.2、ggsave儲存圖
12、字型設定
12.1、字型用法
12.2、可用字型·
1、ggplot2繪制基礎條形圖和線形圖(basic bar or line graphs)
1.1、預設條形圖
#準備資料
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
dat
#> time total_bill
#> 1 Lunch 14.89
#> 2 Dinner 17.23
#加載ggplot2
library(ggplot2)
#繪圖
ggplot(data=dat, aes(x=time, y=total_bill)) +
geom_bar(stat="identity")
1.2、不同柱子上不同色、添加圖例
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity")
1.3、柱子添加黑色外框
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", stat="identity") +
guides(fill=FALSE)
1.4、給條形圖添加标題、設定柱子填充色、關閉圖例
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") +
guides(fill=FALSE) +
xlab("Time of day") + ylab("Total bill") +
ggtitle("Average bill for 2 people")
1.5、資料集計數條形圖
library(reshape2)
# Look at fist several rows
head(tips)#對如下資料集day計數繪圖
total_bill tip sex smoker day time size
1 16.99 1.01 Female No Sun Dinner 2
2 10.34 1.66 Male No Sun Dinner 3
3 21.01 3.50 Male No Sun Dinner 3
4 23.68 3.31 Male No Sun Dinner 2
5 24.59 3.61 Female No Sun Dinner 4
6 25.29 4.71 Male No Sun Dinner 4
#繪圖
ggplot(data=tips, aes(x=day)) +
geom_bar(stat="count")
1.6、基礎線性圖
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +
geom_line()
1.7、線性圖添加資料點
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +
geom_line() +
geom_point()
1.8、設定線形圖線型及點的形狀
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +
geom_line(colour="red", linetype="dashed", size=1.5) +
geom_point(colour="red", size=4, shape=21, fill="white")
1.9、設定線性圖的标題
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab("Time of day") + ylab("Total bill") +
ggtitle("Average bill for 2 people")
1.10、多組資料堆積條形圖
dat1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
dat1
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity")
1.11、多組資料并列條形圖
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge())
1.12、多組資料并列條形圖修改柱子填充色、外框色
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge(), colour="black") +
scale_fill_manual(values=c("#999999", "#E69F00"))
1.13、修改x軸變量
ggplot(data=dat1, aes(x=sex, y=total_bill, fill=time)) +
geom_bar(stat="identity", position=position_dodge(), colour="black")
1.14、多組資料線性圖
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex)) +
geom_line() +
geom_point()
1.15、多組資料線性圖按變量填色
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, colour=sex)) +
geom_line() +
geom_point()
16、多組資料線性圖按變量使用不同圖形标記
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex)) +
geom_line() +
geom_point()
17、完美的柱狀圖
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(colour="black", stat="identity",
position=position_dodge(),
size=.3) + # Thinner lines
scale_fill_hue(name="Sex of payer") + # Set legend title
xlab("Time of day") + ylab("Total bill") + # Set axis labels
ggtitle("Average bill for 2 people") + # Set title
theme_bw()
18、完美的線性圖
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) +
geom_line(aes(linetype=sex), size=1) + # Set linetype by sex
geom_point(size=3, fill="white") + # Use larger points, fill with white
expand_limits(y=0) + # Set y range to include 0
scale_colour_hue(name="Sex of payer", # Set legend title
l=30) + # Use darker colors (lightness=30)
scale_shape_manual(name="Sex of payer",
values=c(22,21)) + # Use points with a fill color
scale_linetype_discrete(name="Sex of payer") +
xlab("Time of day") + ylab("Total bill") + # Set axis labels
ggtitle("Average bill for 2 people") + # Set title
theme_bw() +
theme(legend.position=c(.7, .4)) # Position legend inside
# This must go after theme_bw
2、ggplot2添加誤差棒和均值(means and error bars)
2.1、預設添加折線圖誤差棒
#準備資料
tg <- ToothGrowth
head(tg)
# summarySE provides the standard deviation, standard error of the mean, and a (default 95%) confidence interval
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
conf.interval=.95, .drop=TRUE) {
library(plyr)
# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# This does the summary. For each group's data frame, return a vector with
# N, mean, and sd
datac <- ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N = length2(xx[[col]], na.rm=na.rm),
mean = mean (xx[[col]], na.rm=na.rm),
sd = sd (xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# Rename the "mean" column
datac <- rename(datac, c("mean" = measurevar))
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult
return(datac)
}
tgc <- summarySE(tg, measurevar="len", groupvars=c("supp","dose"))
tgc
ggplot(tgc, aes(x=dose, y=len, colour=supp)) +
geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1) +
geom_line() +
geom_point()
2.2、折線圖誤差棒防重疊
pd <- position_dodge(0.1) # move them .05 to the left and right
ggplot(tgc, aes(x=dose, y=len, colour=supp)) +
geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1, position=pd) +
geom_line(position=pd) +
geom_point(position=pd)
2.3、按照95%置信區間添加折線圖誤差棒
# Use 95% confidence interval instead of SEM
ggplot(tgc, aes(x=dose, y=len, colour=supp)) +
geom_errorbar(aes(ymin=len-ci, ymax=len+ci), width=.1, position=pd) +
geom_line(position=pd) +
geom_point(position=pd)
2.4、修改折線圖誤差棒顔色
ggplot(tgc, aes(x=dose, y=len, colour=supp, group=supp)) +
geom_errorbar(aes(ymin=len-ci, ymax=len+ci), colour="black", width=.1, position=pd) +
geom_line(position=pd) +
geom_point(position=pd, size=3)
2.5、完美的誤差棒折線圖
ggplot(tgc, aes(x=dose, y=len, colour=supp, group=supp)) +
geom_errorbar(aes(ymin=len-se, ymax=len+se), colour="black", width=.1, position=pd) +
geom_line(position=pd) +
geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle
xlab("Dose (mg)") +
ylab("Tooth length") +
scale_colour_hue(name="Supplement type", # Legend label, use darker colors
breaks=c("OJ", "VC"),
labels=c("Orange juice", "Ascorbic acid"),
l=40) + # Use darker colors, lightness=40
ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs") +
expand_limits(y=0) + # Expand y range
scale_y_continuous(breaks=0:20*4) + # Set tick every 4
theme_bw() +
theme(legend.justification=c(1,0),
legend.position=c(1,0)) # Position legend in bottom right
2.6、柱狀圖添加誤差棒
tgc2 <- tgc
tgc2$dose <- factor(tgc2$dose)
# Error bars represent standard error of the mean
ggplot(tgc2, aes(x=dose, y=len, fill=supp)) +
geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=len-se, ymax=len+se),
width=.2, # Width of the error bars
position=position_dodge(.9))
2.7、按照95%置信區間添加柱狀圖誤差棒
ggplot(tgc2, aes(x=dose, y=len, fill=supp)) +
geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=len-ci, ymax=len+ci),
width=.2, # Width of the error bars
position=position_dodge(.9))
2.8、完美的帶誤差棒的條形圖
ggplot(tgc2, aes(x=dose, y=len, fill=supp)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black", # Use black outlines,
size=.3) + # Thinner lines
geom_errorbar(aes(ymin=len-se, ymax=len+se),
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9)) +
xlab("Dose (mg)") +
ylab("Tooth length") +
scale_fill_hue(name="Supplement type", # Legend label, use darker colors
breaks=c("OJ", "VC"),
labels=c("Orange juice", "Ascorbic acid")) +
ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs") +
scale_y_continuous(breaks=0:20*4) +
theme_bw()
3、分布圖
3.1、簡單直方圖
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200),rnorm(200, mean=.8)))
# View first few rows
head(dat)
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5)
3.2、輪廓直方圖
# Draw with black outline, white fill
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white")
3.3、核密度圖
# Density curve
ggplot(dat, aes(x=rating)) + geom_density()
3.4、核密度圖結合直方圖
# Histogram overlaid with kernel density curve
ggplot(dat, aes(x=rating)) +
geom_histogram(aes(y=..density..), # Histogram with density instead of count on y-axis
binwidth=.5,
colour="black", fill="white") +
geom_density(alpha=.2, fill="#FF6666") # Overlay with transparent density plot
3.5、直方圖添加均值線
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white") +
geom_vline(aes(xintercept=mean(rating, na.rm=T)), # Ignore NA values for mean
color="red", linetype="dashed", size=1)
3.6、多組資料預設直方圖
# Overlaid histograms
ggplot(dat, aes(x=rating, fill=cond)) +
geom_histogram(binwidth=.5, alpha=.5, position="identity")
3.9、多組資料連接配接直方圖
# Interleaved histograms
ggplot(dat, aes(x=rating, fill=cond)) +
geom_histogram(binwidth=.5, position="dodge")
3.10、多組資料核密度圖
# Density plots
ggplot(dat, aes(x=rating, colour=cond)) + geom_density()
# Density plots with semi-transparent fill
ggplot(dat, aes(x=rating, fill=cond)) + geom_density(alpha=.3)
3.11、多組資料直方圖添加均值線
# Find the mean of each group
library(plyr)
cdat <- ddply(dat, "cond", summarise, rating.mean=mean(rating))
cdat
#> cond rating.mean
#> 1 A -0.05775928
#> 2 B 0.87324927
# Overlaid histograms with means
ggplot(dat, aes(x=rating, fill=cond)) +
geom_histogram(binwidth=.5, alpha=.5, position="identity") +
geom_vline(data=cdat, aes(xintercept=rating.mean, colour=cond),
linetype="dashed", size=1)
3.12、多組資料直方圖添加均值線
# Density plots with means
ggplot(dat, aes(x=rating, colour=cond)) +
geom_density() +
geom_vline(data=cdat, aes(xintercept=rating.mean, colour=cond),
linetype="dashed", size=1)
3.13、分面多組資料直方圖
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white") +
facet_grid(cond ~ .)
3.14、分面多組資料直方圖添加均值線
# With mean lines, using cdat from above
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white") +
facet_grid(cond ~ .) +
geom_vline(data=cdat, aes(xintercept=rating.mean),
linetype="dashed", size=1, colour="red")
3.15、基礎箱圖
# A basic box plot
ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot()
3.16、不同變量填充不同色箱圖、圖例
ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()
3.17、不同變量填充不同色箱圖、無圖例
ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() +
guides(fill=FALSE)
3.18、水準箱圖
ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() +
guides(fill=FALSE) + coord_flip()
3.19、箱圖添加均值點
ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=5, size=4)
4、ggplot2繪制散點圖
4.1、基礎散點圖
set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
head(dat)
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) # Use hollow circles
4.2、散點圖添加趨勢線
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # Use hollow circles
geom_smooth(method=lm, # Add linear regression line
se=FALSE) # Don't add shaded confidence region
4.3、散點圖添加置信區間區域
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # Use hollow circles
geom_smooth(method=lm) # Add linear regression line
# (by default includes 95% confidence region)
4.4、散點圖不同變量添加不同色
ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1)
4.5、散點圖不同變量添加不同色、添加趨勢線
ggplot(dat, aes(x=xvar, y=yvar, color=cond)) +
geom_point(shape=1) +
scale_colour_hue(l=50) + # Use a slightly darker palette than normal
geom_smooth(method=lm, # Add linear regression lines
se=FALSE) # Don't add shaded confidence region
4.6、散點圖不同變量設定不同标記
ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point()
5、ggplot2設定标題
5.1、預設标題格式
bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
bp + ggtitle("Plant growth")
5.2、長标題分行顯示
bp + ggtitle("Plant growth with\ndifferent treatments")
5.3、标題字型設定
bp + ggtitle("Plant growth with\ndifferent treatments") +
theme(plot.title = element_text(lineheight=.8, ))
6、坐标軸|刻度值|刻度設定
6.1、調整變量順序
library(ggplot2)
bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot()
bp + scale_x_discrete(limits=c("trt1","trt2","ctrl"))#設定順序
6.2、設定刻度标簽名稱
bp + scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"),
labels=c("Control", "Treat 1", "Treat 2"))
6.3、隐藏網格線、刻度标簽、短标記線
# Hide x tick marks, labels, and grid lines
bp + scale_x_discrete(breaks=NULL)
6.4、設定刻度範圍
# 刻度包含0
bp + expand_limits(y=0)
# 刻度包含0和8
bp + expand_limits(y=c(0,8))
#刻度範圍為5到7.5
bp + ylim(5, 7.5)
# Using coord_cartesian "zooms" into the area
bp + coord_cartesian(ylim=c(5, 7.5))
# 刻度步長設定
bp + coord_cartesian(ylim=c(5, 7.5)) +
scale_y_continuous(breaks=seq(0, 10, 0.25)) # Ticks from 0-10, every .25
6.5、tick markers隐藏
bp + scale_y_continuous(breaks=seq(1,10,1/4))
# The breaks can be spaced unevenly
bp + scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6,8))
# Suppress ticks and gridlines
bp + scale_y_continuous(breaks=NULL)
# Hide tick marks and labels (on Y axis), but keep the gridlines
bp + theme(axis.ticks = element_blank(), axis.text.y = element_blank())
6.6、刻度機關轉換為對數, 指數、開方等
set.seed(201)
n <- 100
dat <- data.frame(
xval = (1:n+rnorm(n,sd=5))/20,
yval = 2*2^((1:n+rnorm(n,sd=5))/20)
)
# A scatterplot with regular (linear) axis scaling
sp <- ggplot(dat, aes(xval, yval)) + geom_point()
library(scales)
sp + scale_y_continuous(trans = log2_trans(),
breaks = trans_breaks("log2", function(x) 2^x),
labels = trans_format("log2", math_format(2^.x)))
set.seed(205)
n <- 100
dat10 <- data.frame(
xval = (1:n+rnorm(n,sd=5))/20,
yval = 10*10^((1:n+rnorm(n,sd=5))/20)
)
sp10 <- ggplot(dat10, aes(xval, yval)) + geom_point()
# log10
sp10 + scale_y_log10()
6.7、刻度字型等屬性設定
# Change font options:
# X-axis label: bold, red, and 20 points
# X-axis tick marks: rotate 90 degrees CCW, move to the left a bit (using vjust,
# since the labels are rotated), and 16 points
bp + theme(axis.title.x = element_text(, colour="#990000", size=20),
axis.text.x = element_text(angle=90, vjust=0.5, size=16))
library(scales) # Need the scales package
bp + scale_y_continuous(labels=percent) +
scale_x_discrete(labels=abbreviate) # In this particular case, it has no effect
7、ggplot2圖例設定
7.1、預設圖例
library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
bp
7.2、三種方法不顯示圖例
# Remove legend for a particular aesthetic (fill)
bp + guides(fill=FALSE)
# It can also be done when specifying the scale
bp + scale_fill_discrete(guide=FALSE)
# This removes all legends
bp + theme(legend.position="none")
7.3、修改圖例顯示順序
bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))
7.4、去掉圖例标題
bp + theme(legend.title=element_blank())
7.5、圖例标題屬性修改(legend titles and labels)
bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"),
name="Experimental\nCondition",
breaks=c("ctrl", "trt1", "trt2"),
labels=c("Control", "Treatment 1", "Treatment 2"))
7.6、多特征圖例
df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
lp1 <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line() + geom_point()
# Here's what happens if you just specify colour
lp1 + scale_colour_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man"))
7.7、圖例标題字号、顔色設定
# Title appearance
bp + theme(legend.title = element_text(colour="blue", size=16, ))
# Label appearance
bp + theme(legend.text = element_text(colour="blue", size = 16, face = "bold"))
7.8、圖例添加背景色
bp + theme(legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"))
7.9、圖例位置
bp + theme(legend.position="top")
# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(.5, .5))
# Set the "anchoring point" of the legend (bottom-left is 0,0; top-right is 1,1)
# Put bottom-left corner of legend box in bottom-left corner of graph
bp + theme(legend.justification=c(0,0), legend.position=c(0,0))
# Put bottom-right corner of legend box in bottom-right corner of graph
bp + theme(legend.justification=c(1,0), legend.position=c(1,0))
8、ggplot2添加輔助線
8.1、所有柱子添加水準直線
# Some sample data
dat <- read.table(header=TRUE, text='
cond result
control 10
treatment 11.5
')
library(ggplot2)
bp <- ggplot(dat, aes(x=cond, y=result)) +
geom_bar(position=position_dodge(), stat="identity")
# Make the line red and dashed
bp + geom_hline(aes(yintercept=12), colour="#990000", linetype="dashed")
dat <- read.table(header=TRUE, text='
cond group result hline
control A 10 9
treatment A 11.5 12
control B 12 9
treatment B 14 12
')
# Define basic bar plot
bp <- ggplot(dat, aes(x=cond, y=result, fill=group)) +
geom_bar(position=position_dodge(), stat="identity")
# The error bars get plotted over one another -- there are four but it looks
# like two
bp + geom_errorbar(aes(ymax=hline, ymin=hline), linetype="dashed")
8.2、每個柱子各自添加輔助線
# Draw separate hlines for each bar. First add another column to dat
dat$hline <- c(9,12)
# Can get the same result, even if we get the hline values from a second data frame
# Define data frame with hline
dat_hlines <- data.frame(cond=c("control","treatment"), hline=c(9,12))
d
# The bars are from dat, but the lines are from dat_hlines
bp + geom_errorbar(data=dat_hlines, aes(y=NULL, ymax=hline, ymin=hline), colour="#AA0000")
dat <- read.table(header=TRUE, text='
cond group result hline
control A 10 11
treatment A 11.5 12
control B 12 12.5
treatment B 14 15
')
# Define basic bar plot
bp <- ggplot(dat, aes(x=cond, y=result, fill=group)) +
geom_bar(position=position_dodge(), stat="identity")
bp
bp + geom_errorbar(aes(ymax=hline, ymin=hline), linetype="dashed",
position=position_dodge())
8.3、分面圖中添加輔助線
dat <- read.table(header=TRUE, text='
cond xval yval
control 11.5 10.8
control 9.3 12.9
control 8.0 9.9
control 11.5 10.1
control 8.6 8.3
control 9.9 9.5
control 8.8 8.7
control 11.7 10.1
control 9.7 9.3
control 9.8 12.0
treatment 10.4 10.6
treatment 12.1 8.6
treatment 11.2 11.0
treatment 10.0 8.8
treatment 12.9 9.5
treatment 9.1 10.0
treatment 13.4 9.6
treatment 11.6 9.8
treatment 11.5 9.8
treatment 12.0 10.6
')
library(ggplot2)
sp <- ggplot(dat, aes(x=xval, y=yval, colour=cond)) + geom_point()
library(dplyr)
lines <- dat %>%
group_by(cond) %>%
summarise(
x = mean(xval),
ymin = min(yval),
ymax = max(yval)
)
dat_vlines <- data.frame(cond=levels(dat$cond), xval=c(10,11.5))
spf <- sp + facet_grid(. ~ cond)
spf + geom_hline(aes(yintercept=10)) +
geom_linerange(aes(x=x, y=NULL, ymin=ymin, ymax=ymax), data=lines)
9、ggplot2圖形分面
9.1、y軸方向分面
library(reshape2)
library(ggplot2)
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
sp + facet_grid(sex ~ .)
9.2、x軸方向分面
sp + facet_grid(. ~ sex)
9.3、x軸y軸方向同時分面
sp + facet_grid(sex ~ day)
9.4、分面為指定的行數和列數
sp + facet_wrap( ~ day, ncol=2)
9.5、分面标簽個性化設定(背景色、字型、字号等)
sp + facet_grid(sex ~ day) +
theme(strip.text.x = element_text(size=8, angle=75),
strip.text.y = element_text(size=12, ),
strip.background = element_rect(colour="red", fill="#CCCCFF"))
9.6、各分面圖軸刻度個性化
hp <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")
hp + facet_grid(sex ~ smoker, scales="free", space="free")
10、ggplot2拼圖
#現在已經有很多拼圖包了,寫這個書的時時間比較早了,這裡整的有點麻煩
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
library(ggplot2)
# This example uses the ChickWeight dataset, which comes with ggplot2
# First plot
p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")
# Second plot
p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Fitted growth curve per diet")
# Third plot
p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
geom_density() +
ggtitle("Final weight, by diet")
# Fourth plot
p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
geom_histogram(colour="black", binwidth=50) +
facet_grid(Diet ~ .) +
ggtitle("Final weight, by diet") +
theme(legend.position="none") # No legend (redundant in this graph)
multiplot(p1, p2, p3, p4, cols=2)
11、ggplot2圖形儲存
11.1、R預設儲存方式
# 6x3 inches
pdf("plots.pdf", width=6, height=3)
# 10x6 cm
pdf("plots.pdf", width=10/2.54, height=6/2.54)
plot(...)
plot(...)
dev.off()#dev.off() command to tell R that you are finished plotting; otherwise your graph will not show up.
#eg
pdf("plots.pdf", width=6, height=3)
ggplot(dat, aes(x=rating, fill=cond)) + geom_density(alpha=.3)
dev.off()
11.2、ggsave儲存圖
ggsave("plot2.pdf", width=4, height=4)
ggplot(dat, aes(x=rating, fill=cond)) + geom_density(alpha=.3)
dev.off()
12、字型設定
12.1、字型用法
dat <- data.frame(
y = 1:3,
text = c("This is text", "Text with\nmultiple lines", "Some more text")
)
library(ggplot2)
p <- ggplot(dat, aes(x=1, y=y)) +
scale_y_continuous(limits=c(0.5, 3.5), breaks=NULL) +
scale_x_continuous(breaks=NULL)
p + geom_text(aes(label=text), family="Times", font, lineheight=.8) +
annotate(geom="text", x=1, y=1.5, label="Annotation text", colour="red",
size=7, family="Courier", font, angle=30)
12.2、可用字型·
fonttable <- read.table(header=TRUE, sep=",", stringsAsFactors=FALSE,
text='
Short,Canonical
mono,Courier
sans,Helvetica
serif,Times
,AvantGarde
,Bookman
,Helvetica-Narrow
,NewCenturySchoolbook
,Palatino
,URWGothic
,URWBookman
,NimbusMon
URWHelvetica,NimbusSan
,NimbusSanCond
,CenturySch
,URWPalladio
URWTimes,NimbusRom
')
fonttable$pos <- 1:nrow(fonttable)
library(reshape2)
fonttable <- melt(fonttable, id.vars="pos", measure.vars=c("Short","Canonical"),
variable.name="NameType", value.name="Font")
# Make a table of faces. Make sure factors are ordered correctly
facetable <- data.frame(Face = factor(c("plain","bold","italic","bold.italic"),
levels = c("plain","bold","italic","bold.italic")))
fullfonts <- merge(fonttable, facetable)
library(ggplot2)
pf <- ggplot(fullfonts, aes(x=NameType, y=pos)) +
geom_text(aes(label=Font, family=Font, fontface=Face)) +
facet_wrap(~ Face, ncol=2)
pf