1.Dot plots
1.1 語(yǔ)法
geom_dotplot( mapping = NULL, data = NULL, position = "identity", ..., binwidth = NULL, binaxis = "x", method = "dotdensity", binpositions = "bygroup", stackdir = "up", stackratio = 1, dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE, width = 0.9, drop = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
# 將數(shù)值型變量轉(zhuǎn)換為因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# 基礎(chǔ)點(diǎn)圖
p1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
# 更改點(diǎn)尺寸和堆疊率
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center',
stackratio=1.5, dotsize=1.2)
# 旋轉(zhuǎn)
p3 <- p1 + coord_flip()
# 選擇展示數(shù)據(jù)
p4 <- p1 + scale_x_discrete(limits=c("0.5", "2"))
ggarrange(p1,p2,p3,p4,nrow = 1)
1.2 在點(diǎn)圖上添加摘要統(tǒng)計(jì)信息 stat_summary()
# 在點(diǎn)圖上添加摘要統(tǒng)計(jì)信息 stat_summary()
# 添加平均值和中位數(shù)
p5 <- p1 + stat_summary(fun.y=mean, geom="point", shape=18,
size=3, color="red")
p6<- p1 + stat_summary(fun.y=median, geom="point", shape=18,
size=3, color="red")
1.3 點(diǎn)狀圖與箱形圖和小提琴圖
# 點(diǎn)狀圖與箱形圖和小提琴圖
# 添加基礎(chǔ)箱線圖
p7 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_dotplot(binaxis='y', stackdir='center')
# 添加缺刻箱線圖
p8 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(notch = TRUE)+
geom_dotplot(binaxis='y', stackdir='center')
# 添加小提琴圖
p9 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim = FALSE)+
geom_dotplot(binaxis='y', stackdir='center')
ggarrange(p7,p8,p9,nrow = 1)
1.4 添加平均值和標(biāo)準(zhǔn)偏差
# 添加平均值和標(biāo)準(zhǔn)偏差
p10 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
p11 <- p10 + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="crossbar", width=0.5)
p12 <- p10 + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")
# 產(chǎn)生匯總統(tǒng)計(jì)的函數(shù) (mean and +/- sd)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
p13 <- p10 + stat_summary(fun.data=data_summary, color="blue")
ggarrange(p10,p11,p12,p13,nrow = 1)
1.5 更改填充色
# 按組更改點(diǎn)圖顏色
# 單色填充
p14 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center', fill="#FFAAD4")
# 按組填充
p15 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')
ggarrange(p14,p15)
# 自定義調(diào)色板
p16 <- p15 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer調(diào)色板
p17 <- p15 + scale_fill_brewer(palette="Dark2")
# 灰色
p18 <- p15 + scale_fill_grey() + theme_classic()
ggarrange(p16,p17,p18,nrow = 1)
1.6 更改圖例位置
# 更改圖例位置
p19 <- p15 + theme(legend.position="top")
p20 <- p15 + theme(legend.position="bottom")
p21 <- p15 + theme(legend.position="none") # Remove legend
ggarrange(p19,p20,p21,nrow = 1)
1.7 更改圖例中的項(xiàng)目順序
# 更改圖例中的項(xiàng)目順序
p22 <- p15 + scale_x_discrete(limits=c("2", "0.5", "1"))
p22
ggarrange(p15,p22)
1.8 多組點(diǎn)圖
# 多組點(diǎn)圖
# 按組更改顏色
p23 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_dotplot(binaxis='y', stackdir='center')
p24 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8))
ggarrange(p23,p24)
# 更改顏色
p25 <-p24 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 添加箱線圖
p26 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(fill="white")+
geom_dotplot(binaxis='y', stackdir='center')
# 改變圖例
p27 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8))
ggarrange(p25,p26,p27,nrow = 1)
1.9 自定義點(diǎn)圖
# 定制點(diǎn)圖
# 基礎(chǔ)點(diǎn)圖
dp1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_dotplot(binaxis='y', stackdir='center')+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+
theme_classic()
# 按組修改顏色
dp2 <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
dp3 <- dp2 + theme_classic()
# 連續(xù)性顏色
dp4 <- dp2 + scale_fill_brewer(palette="Blues") + theme_classic()
# 離散型顏色
dp5 <- dp2 + scale_fill_brewer(palette="Dark2") + theme_minimal()
# 漸變色
dp6 <- dp2 + scale_fill_brewer(palette="RdBu") + theme_minimal()
ggarrange(dp1,dp2,dp3,nrow = 1)
ggarrange(dp4,dp5,dp6,nrow = 1)
2. Scatter plots
2.1 語(yǔ)法
geom_point( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
2.2 基礎(chǔ)散點(diǎn)圖
# 將數(shù)值型變量轉(zhuǎn)換為因子
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)
library(ggplot2)
# 基礎(chǔ)散點(diǎn)圖
p1 <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# 點(diǎn)的大小及形狀
p2 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23)
p3 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(size=qsec))
ggarrange(p1,p2,p3,nrow = 1)
2.3添加文本geom_text()
# 在散點(diǎn)圖中標(biāo)記點(diǎn) geom_text()
p4 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text(label=rownames(mtcars))
p4
2.4 添加回歸線geom_smooth(), stat_smooth(),geom_abline()
# 添加回歸線geom_smooth(), stat_smooth(),geom_abline()
# 添加回歸線
p5 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm)
# 刪除置信區(qū)間
p6 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm, se=FALSE)
# Loess method
p7 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth()
ggarrange(p5,pp6,p7,nrow = 1)
2.5 更改點(diǎn)和線的外觀
# 更改點(diǎn)和線的外觀
# 點(diǎn)的顏色及形狀
# 線型及顏色
p8 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape=18, color="blue")+
geom_smooth(method=lm, se=FALSE, linetype="dashed",
color="darkred")
# 更改置信區(qū)間填充顏色
p9 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape=18, color="blue")+
geom_smooth(method=lm, linetype="dashed",
color="darkred", fill="blue")
ggarrange(p8,p9)
2.6 多組散點(diǎn)圖
# 自動(dòng)更改點(diǎn)的顏色/形狀/大小
# --------------------------------------------------------
# 通過cyl因子水平修改點(diǎn)的形狀
# mtcars$cyl <- as.factor(mtcars$cyl)
p10 <- ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl)) +
geom_point()
p10
# 修改點(diǎn)的形狀及顏色
p11 <- ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl)) +
geom_point()
p11
# 點(diǎn)的形狀,大小及顏色
p12 <- ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl, size=cyl)) +
geom_point()
p12
ggarrange(p10,p11,p12,nrow = 1)
# 添加回歸線
p13 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm)
# 移除置信區(qū)間
# 延長(zhǎng)回歸線
p14 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
# 置信帶的填充顏色可以如下更改:
p15 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, aes(fill=cyl))
ggarrange(p13,p14,p15,nrow = 1)
# 手動(dòng)更改點(diǎn)的顏色/形狀/大小
# ----------------------------------------------------------------
# 手動(dòng)更改點(diǎn)的顏色及形狀
p16 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
# 手動(dòng)更改點(diǎn)的大小
p17 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl))+
geom_point(aes(size=cyl)) +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
scale_size_manual(values=c(2,3,4))+
theme(legend.position="top")
p18 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
theme_classic()
# brewer調(diào)色板
p19 <- p18 + scale_color_brewer(palette="Dark2")
# 灰度
p20 <- p18 + scale_color_grey()
ggarrange(p16,p17,p18,p19,p20,nrow = 2,ncol = 3)
2.7 添加邊緣地毯
# 添加邊緣地毯
# geom_rug(sides ="bl"),sides=“trbl”, for top, right, bottom, and left.
p21 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() + geom_rug()
# 更改顏色
p22 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point() + geom_rug()
# Add marginal rugs using faithful data
p23 <- ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point() + geom_rug()
ggarrange(p21,p22,p23,nrow = 1)
2.8 二維密度估計(jì)的散點(diǎn)圖
# 二維密度估計(jì)的散點(diǎn)圖 geom_density_2d(),stat_density_2d()
sp1 <- ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point()
sp2 <- sp1 + geom_density_2d()
# 漸變色
sp3 <- sp1 + stat_density_2d(aes(fill = ..level..), geom="polygon")
# 更改漸變顏色
sp4 <- sp1 + stat_density_2d(aes(fill = ..level..), geom="polygon")+
scale_fill_gradient(low="blue", high="red")
ggarrange(sp1,sp2,sp3,sp4,nrow = 1)
2.9 橢圓散點(diǎn)圖
# 橢圓散點(diǎn)圖 stat_ellipse()
# 圍繞所有點(diǎn)的一個(gè)橢圓
p24 <- ggplot(faithful, aes(waiting, eruptions))+
geom_point()+
stat_ellipse()
# 分組橢圓
p25 <- ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3))+
geom_point()
p26 <- p25 + stat_ellipse()
# 更改橢圓的類型:可能的值為 "t", "norm", "euclid"
p27 <- p25 + stat_ellipse(type = "norm")
ggarrange(p24,p25,p26,p27,nrow = 1)
2.10 帶有矩形箱的散點(diǎn)圖
# geom_bin2d() 用于添加二維箱計(jì)數(shù)的熱圖
# stat_bin_2d() 用于計(jì)數(shù)矩形箱中的觀測(cè)次數(shù)
# stat_summary_2d() 將功能應(yīng)用于二維矩形箱
# plot + geom_bin2d(...)
# plot+stat_bin_2d(geom=NULL, bins=30)
# plot + stat_summary_2d(geom = NULL, bins = 30, fun = mean)
# geom:顯示數(shù)據(jù)的幾何對(duì)象
# bins:垂直和水平方向上的垃圾箱數(shù)。 默認(rèn)值為30
# fun:匯總功能
head(diamonds)
# Plot
p28 <- ggplot(diamonds, aes(carat, price))
p29 <- p28 + geom_bin2d()
# 更改bins
p30 <- p29 + geom_bin2d(bins=10)
# 或指定bins的寬度
p31 <- p29 + geom_bin2d(binwidth=c(1, 1000))
ggarrange(p28,p29,p30,p31,nrow = 1)
2.11 具有邊際密度分布圖的散點(diǎn)圖
# 建立數(shù)據(jù)
set.seed(1234)
x <- c(rnorm(500, mean = -1), rnorm(500, mean = 1.5))
y <- c(rnorm(500, mean = 1), rnorm(500, mean = 1.7))
group <- as.factor(rep(c(1,2), each=500))
df <- data.frame(x, y, group)
head(df)
# 創(chuàng)建散點(diǎn)圖辨图,按組分色
scatterPlot <- ggplot(df,aes(x, y, color=group)) +
geom_point() +
scale_color_manual(values = c('#999999','#E69F00')) +
theme(legend.position=c(0,1), legend.justification=c(0,1))
scatterPlot
# x的邊際密度圖(上圖)
xdensity <- ggplot(df, aes(x, fill=group)) +
geom_density(alpha=.5) +
scale_fill_manual(values = c('#999999','#E69F00')) +
theme(legend.position = "none")
xdensity
# y的邊際密度圖(右圖)
ydensity <- ggplot(df, aes(y, fill=group)) +
geom_density(alpha=.5) +
scale_fill_manual(values = c('#999999','#E69F00')) +
theme(legend.position = "none")
ydensity
# 創(chuàng)建一個(gè)空白的占位符圖:
blankPlot <- ggplot()+geom_blank(aes(1,1))+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank()
)
# 可以使用gridExtra軟件包將多個(gè)圖放在同一頁(yè)面上
install.packages("gridExtra")
library("gridExtra")
grid.arrange(xdensity, blankPlot, scatterPlot, ydensity,
ncol=2, nrow=2, widths=c(4, 1.4), heights=c(1.4, 4))
2.12 自定義散點(diǎn)圖
# 基礎(chǔ)散點(diǎn)圖
p32 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm, color="black")+
labs(title="Miles per gallon \n according to the weight",
x="Weight (lb/1000)", y = "Miles/(US) gallon")+
theme_classic()
# 按組更改顏色及形狀班套,移除置信區(qū)間
p33 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point()+
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
labs(title="Miles per gallon \n according to the weight",
x="Weight (lb/1000)", y = "Miles/(US) gallon")
p34 <- p33 + theme_classic()
# 連續(xù)型顏色
p35 <- p33 + scale_color_brewer(palette="Paired") + theme_classic()
# 離散型顏色
p36 <- p33 + scale_color_brewer(palette="Dark2") + theme_minimal()
# 漸變色
p37 <- p33 + scale_color_brewer(palette="Accent") + theme_minimal()
ggarrange(p32,p33,p34,p35,p36,p37,nrow = 2,ncol = 3)
3.Stripcharts
3.1 語(yǔ)法
geom_jitter( mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
3.2 基本帶狀圖
# 將可變劑量從數(shù)字轉(zhuǎn)換為因子變量
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
# 基本帶狀圖
library(ggplot2)
p1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter()
# 改變位置
# 0.2:x方向的抖動(dòng)度
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2))
# 旋轉(zhuǎn)帶狀圖
p3 <- p2 + coord_flip()
# 選擇展示數(shù)據(jù)
p4 <- p2 + scale_x_discrete(limits=c("0.5", "2"))
ggarrange(p1,p2,p3,p4)
# 變更點(diǎn)大小
p5 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2), cex=1.2)
# 改變形狀
p6 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2), shape=17)
ggarrange(p5,p6)
3.3 在活動(dòng)圖表上添加摘要統(tǒng)計(jì)信息
# 添加均值和中位數(shù)
p7 <- p2 + stat_summary(fun.y=mean, geom="point", shape=18,
size=3, color="red")
p8 <- p3 + stat_summary(fun.y=median, geom="point", shape=18,
size=3, color="red")
ggarrange(p7,p8)
# 帶箱線圖和小提琴圖的帶狀圖
# Add basic box plot
p9 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_jitter(position=position_jitter(0.2))
# Add notched box plot
p10 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(notch = TRUE)+
geom_jitter(position=position_jitter(0.2))
# Add violin plot
p11 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim = FALSE)+
geom_jitter(position=position_jitter(0.2))
ggarrange(p9,p10,p11,nrow = 1)
# 添加平均值和標(biāo)準(zhǔn)偏差
p12 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2))
p13 <- p12 + stat_summary(fun.data="mean_sdl", mult=1,
geom="crossbar", width=0.5)
p14 <- p12 + stat_summary(fun.data=mean_sdl, mult=1,
geom="pointrange", color="red")
ggarrange(p12,p13,p14,nrow = 1)
# 統(tǒng)計(jì)公式 (mean and +/- sd)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
p15 <- p12 + stat_summary(fun.data=data_summary, color="blue")
p15
3.4 按組帶狀圖
# 按組更改點(diǎn)形狀
p16 <- ggplot(ToothGrowth, aes(x=dose, y=len, shape=dose)) +
geom_jitter(position=position_jitter(0.2))
# 手動(dòng)更改點(diǎn)形狀
p17 <- p16 + scale_shape_manual(values=c(1,17,19))
ggarrange(p16,p17)
# 按組更改帶狀圖的顏色
# 單色
p18 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2), color="red")
# 按組更改帶狀圖的顏色
p19 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
geom_jitter(position=position_jitter(0.2))
# 自定義調(diào)色板
p20 <- p19 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer調(diào)色板
p21 <- p19 + scale_color_brewer(palette="Dark2")
# 灰度
p22 <- p19 + scale_color_grey() + theme_classic()
ggarrange(p18,p19,p20,p21,p22,nrow=2,ncol = 3)
3.5 修改圖例
# 更改圖例位置
p23 <- p19 + theme(legend.position="top")
p24 <- p19 + theme(legend.position="bottom")
p25 <- p19 + theme(legend.position="none")# Remove legend
# 更改圖例中的項(xiàng)目順序
p26 <- p19 + scale_x_discrete(limits=c("2", "0.5", "1"))
ggarrange(p23,p24,p25,p26)
3.6 帶多個(gè)組的帶狀圖
# 帶多個(gè)組的帶狀圖
# 按組更改帶狀圖的顏色
p27 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) +
geom_jitter(position=position_jitter(0.2))
# 更改位置:同一組的圖表之間的間隔
p28 <-ggplot(ToothGrowth, aes(x=dose, y=len, color=supp, shape=supp)) +
geom_jitter(position=position_dodge(0.8))
ggarrange(p27,p28)
3.7 帶狀圖添加箱形圖
# 更改帶狀圖的顏色并添加箱形圖:
# 改變顏色
p29 <- p28 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 添加箱線圖
p30 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) +
geom_boxplot(color="black")+
geom_jitter(position=position_jitter(0.2))
# 修改位置
p31 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) +
geom_boxplot(position=position_dodge(0.8))+
geom_jitter(position=position_dodge(0.8))
ggarrange(p29,p30,p31,nrow = 1)
3.8 自定義帶狀圖
# 自定義帶狀圖
# 基礎(chǔ)帶狀圖
p32 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_jitter(position=position_jitter(0.2))+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+
theme_classic()
# 按組更改顏色/形狀
p33 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) +
geom_jitter(position=position_jitter(0.2))+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
p34 <- p33 + theme_classic()
# 連續(xù)型顏色
p35 <- p33 + scale_color_brewer(palette="Blues") + theme_classic()
# 離散型顏色
p36 <- p33 + scale_color_brewer(palette="Dark2") + theme_minimal()
# 漸變色
p37 <- p33 + scale_color_brewer(palette="RdBu")
ggarrange(p32,p33,p34,p35,p36,p37,nrow = 2,ncol = 3)
Reference
1.ggplot2 dot plot : Quick start guide - R software and data visualization
2.ggplot2 scatter plots : Quick start guide - R software and data visualization
3.ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization