ggplot2 007 點(diǎn)圖介蛉,散點(diǎn)圖爆阶,帶狀圖

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)
image.png
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")
image.png
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)
image.png
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)
image.png
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)
image.png
# 自定義調(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)
image.png
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)
image.png
1.7 更改圖例中的項(xiàng)目順序
# 更改圖例中的項(xiàng)目順序
p22 <-  p15 + scale_x_discrete(limits=c("2", "0.5", "1"))
p22
ggarrange(p15,p22)
image.png
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)

image.png
# 更改顏色
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)
image.png
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)
dp1,dp2,dp3
dp4,dp5,dp6

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)
image.png
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
image.png
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)
image.png
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)
image.png
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)

p10,p11,p12

p13,p14,p15
# 手動(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)
image.png
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)
image.png
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)
image.png
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)
image.png
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)
image.png
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))
image.png
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)
image.png

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)
image.png
# 變更點(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)
image.png
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)
image.png
# 帶箱線圖和小提琴圖的帶狀圖
# 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)
image.png
# 添加平均值和標(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)
image.png
# 統(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
image.png
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)
image.png
# 按組更改帶狀圖的顏色
# 單色
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)
image.png
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)
image.png
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)
image.png
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)
image.png
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)
image.png

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市故河,隨后出現(xiàn)的幾起案子吱韭,更是在濱河造成了極大的恐慌,老刑警劉巖鱼的,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件理盆,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡凑阶,警方通過查閱死者的電腦和手機(jī)猿规,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宙橱,“玉大人姨俩,你說我怎么就攤上這事∈χ#” “怎么了环葵?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)宝冕。 經(jīng)常有香客問我张遭,道長(zhǎng),這世上最難降的妖魔是什么地梨? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任菊卷,我火速辦了婚禮,結(jié)果婚禮上湿刽,老公的妹妹穿的比我還像新娘的烁。我一直安慰自己,他們只是感情好诈闺,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布渴庆。 她就那樣靜靜地躺著,像睡著了一般雅镊。 火紅的嫁衣襯著肌膚如雪襟雷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天仁烹,我揣著相機(jī)與錄音耸弄,去河邊找鬼。 笑死卓缰,一個(gè)胖子當(dāng)著我的面吹牛计呈,可吹牛的內(nèi)容都是我干的砰诵。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捌显,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼茁彭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起扶歪,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤理肺,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后善镰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妹萨,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年炫欺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了乎完。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡竣稽,死狀恐怖囱怕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情毫别,我是刑警寧澤娃弓,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站岛宦,受9級(jí)特大地震影響台丛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜砾肺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一挽霉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧变汪,春花似錦侠坎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至番官,卻和暖如春庐完,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背徘熔。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工门躯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人酷师。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親肝匆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355