查看原文
其他

R可视化27|R Graphics Cookbook-ggplot-2(下篇)

pythonic生物人 pythonic生物人 2023-07-26

"pythonic生物人"的第163篇分享

很好的R可视化基础资料,长文搬运,建议「按目录选择性阅读」
续前篇:R可视化26|R Graphics Cookbook(上篇)

本文目录

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.8917.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.5316.8116.2417.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, face="bold"))

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(57.5)


# Using coord_cartesian "zooms" into the area
bp + coord_cartesian(ylim=c(57.5))

# 刻度步长设置
bp + coord_cartesian(ylim=c(57.5)) + 
    scale_y_continuous(breaks=seq(0100.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(44.254.556,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(face="bold", 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.5316.8116.2417.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, face="bold"))

# 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, face="bold"),
          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.53.5), breaks=NULL) +
       scale_x_continuous(breaks=NULL)
p + geom_text(aes(label=text), family="Times", fontface="italic", lineheight=.8) +
    annotate(geom="text", x=1, y=1.5, label="Annotation text", colour="red",
             size=7, family="Courier", fontface="bold", 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

reference@ http://www.cookbook-r.com/Graphs/

有用请“点赞”“在看”“分享”

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存