其他
R可视乎|棒棒糖图
The following article is from bioinfomics Author Davey1220
31. 棒棒糖图绘制
清除当前环境中的变量
rm(list=ls())
设置工作目录
setwd("C:/Users/Dell/Desktop/R_Plots/31lollipop/")
使用ggplot2包绘制棒棒糖图
library(ggplot2)
# 查看内置示例数据
data("mtcars")
df <- mtcars
# 转换为因子
df$cyl <- as.factor(df$cyl)
df$name <- rownames(df)
head(df)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
## name
## Mazda RX4 Mazda RX4
## Mazda RX4 Wag Mazda RX4 Wag
## Datsun 710 Datsun 710
## Hornet 4 Drive Hornet 4 Drive
## Hornet Sportabout Hornet Sportabout
## Valiant Valiant
# 绘制基础棒棒糖图
ggplot(df,aes(name,mpg)) +
# 添加散点
geom_point(size=5) +
# 添加辅助线段
geom_segment(aes(x=name,xend=name,y=0,yend=mpg))
# 更改点的大小,形状,颜色和透明度
ggplot(df,aes(name,mpg)) +
# 添加散点
geom_point(size=5, color="red", fill=alpha("orange", 0.3),
alpha=0.7, shape=21, stroke=3) +
# 添加辅助线段
geom_segment(aes(x=name,xend=name,y=0,yend=mpg)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45,hjust = 1),
panel.grid = element_blank())
# 更改辅助线段的大小,颜色和类型
ggplot(df,aes(name,mpg)) +
# 添加散点
geom_point(aes(size=cyl,color=cyl)) +
# 添加辅助线段
geom_segment(aes(x=name,xend=name,y=0,yend=mpg),
size=1, color="blue", linetype="dotdash") +
theme_classic() +
theme(axis.text.x = element_text(angle = 45,hjust = 1),
panel.grid = element_blank()) +
scale_y_continuous(expand = c(0,0))
# 对点进行排序,坐标轴翻转
# 根据mpg值从小到大排序
df <- df[order(df$mpg),]
# 设置因子进行排序
df$name <- factor(df$name,levels = df$name)
ggplot(df,aes(name,mpg)) +
# 添加散点
geom_point(aes(color=cyl),size=8) +
# 添加辅助线段
geom_segment(aes(x=name,xend=name,y=0,yend=mpg),
size=1, color="gray") +
theme_minimal() +
theme(
panel.grid.major.y = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank()
) +
coord_flip()
使用ggpubr包绘制棒棒糖图
library(ggpubr)
# 查看示例数据
head(df)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
## name
## Cadillac Fleetwood Cadillac Fleetwood
## Lincoln Continental Lincoln Continental
## Camaro Z28 Camaro Z28
## Duster 360 Duster 360
## Chrysler Imperial Chrysler Imperial
## Maserati Bora Maserati Bora
# 使用ggdotchart函数绘制棒棒糖图
ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # 设置按照cyl填充颜色
size = 6, # 设置点的大小
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 修改颜色画板
sorting = "ascending", # 设置升序排序
add = "segments", # 添加辅助线段
add.params = list(color = "lightgray", size = 1.5), # 设置辅助线段的大小和颜色
ggtheme = theme_pubr(), # 设置主题
)
# 自定义一些参数
ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # 设置按照cyl填充颜色
size = 8, # 设置点的大小
palette = "jco", # 修改颜色画板
sorting = "descending", # 设置降序排序
add = "segments", # 添加辅助线段
add.params = list(color = "lightgray", size = 1.2), # 设置辅助线段的大小和颜色
rotate = TRUE, # 旋转坐标轴方向
group = "cyl", # 设置按照cyl进行分组
label = "mpg", # 按mpg添加label标签
font.label = list(color = "white",
size = 7,
vjust = 0.5), # 设置label标签的字体颜色和大小
ggtheme = theme_pubclean(), # 设置主题
)
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggpubr_0.2.1 magrittr_1.5 ggplot2_3.2.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 pillar_1.4.2 compiler_3.6.0 tools_3.6.0
## [5] digest_0.6.20 evaluate_0.14 tibble_2.1.3 lifecycle_0.2.0
## [9] gtable_0.3.0 pkgconfig_2.0.2 rlang_0.4.7 ggsci_2.9
## [13] yaml_2.2.0 xfun_0.8 withr_2.1.2 dplyr_1.0.2
## [17] stringr_1.4.0 knitr_1.23 generics_0.0.2 vctrs_0.3.2
## [21] grid_3.6.0 tidyselect_1.1.0 glue_1.4.2 R6_2.4.0
## [25] rmarkdown_1.13 purrr_0.3.2 scales_1.0.0 htmltools_0.3.6
## [29] ellipsis_0.2.0.1 colorspace_1.4-1 ggsignif_0.5.0 labeling_0.3
## [33] stringi_1.4.3 lazyeval_0.2.2
END
既往专辑