31. 棒棒糖圖繪制
清除當(dāng)前環(huán)境中的變量
rm(list=ls())
設(shè)置工作目錄
setwd("C:/Users/Dell/Desktop/R_Plots/31lollipop/")
使用ggplot2包繪制棒棒糖圖
library(ggplot2)
# 查看內(nèi)置示例數(shù)據(jù)
data("mtcars")
df <- mtcars
# 轉(zhuǎn)換為因子
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
# 繪制基礎(chǔ)棒棒糖圖
ggplot(df,aes(name,mpg)) +
# 添加散點(diǎn)
geom_point(size=5) +
# 添加輔助線段
geom_segment(aes(x=name,xend=name,y=0,yend=mpg))
# 更改點(diǎn)的大小蹋宦,形狀侄柔,顏色和透明度
ggplot(df,aes(name,mpg)) +
# 添加散點(diǎn)
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)) +
# 添加散點(diǎn)
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))
# 對(duì)點(diǎn)進(jìn)行排序,坐標(biāo)軸翻轉(zhuǎn)
# 根據(jù)mpg值從小到大排序
df <- df[order(df$mpg),]
# 設(shè)置因子進(jìn)行排序
df$name <- factor(df$name,levels = df$name)
ggplot(df,aes(name,mpg)) +
# 添加散點(diǎn)
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)
# 查看示例數(shù)據(jù)
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函數(shù)繪制棒棒糖圖
ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # 設(shè)置按照cyl填充顏色
size = 6, # 設(shè)置點(diǎn)的大小
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 修改顏色畫板
sorting = "ascending", # 設(shè)置升序排序
add = "segments", # 添加輔助線段
add.params = list(color = "lightgray", size = 1.5), # 設(shè)置輔助線段的大小和顏色
ggtheme = theme_pubr(), # 設(shè)置主題
)
# 自定義一些參數(shù)
ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # 設(shè)置按照cyl填充顏色
size = 8, # 設(shè)置點(diǎn)的大小
palette = "jco", # 修改顏色畫板
sorting = "descending", # 設(shè)置降序排序
add = "segments", # 添加輔助線段
add.params = list(color = "lightgray", size = 1.2), # 設(shè)置輔助線段的大小和顏色
rotate = TRUE, # 旋轉(zhuǎn)坐標(biāo)軸方向
group = "cyl", # 設(shè)置按照cyl進(jìn)行分組
label = "mpg", # 按mpg添加label標(biāo)簽
font.label = list(color = "white",
size = 7,
vjust = 0.5), # 設(shè)置label標(biāo)簽的字體顏色和大小
ggtheme = theme_pubclean(), # 設(shè)置主題
)
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 munsell_0.5.0 crayon_1.3.4