首页 » 软件编程 » R语言

使用R语言绘制棒棒糖图火柴杆图教程

R语言 2021-12-12

 使用原生ggplot方法

最容易也是最简单想到的方法是直接使用ggplot2包进行更新,这里需要使用ggplot本身的特性,通过图层叠加的方式,进行最终棒棒糖图的展现。(宽度极窄的柱状图配合散点图即可呈现)

1)生成数据

下面我们的展示均以此份数据为例:

library(ggplot2)

# Load data
data("mtcars")
dfm <- mtcars
# Convert the cyl variable to a factor
dfm$cyl <- as.factor(dfm$cyl)
# Add the name colums
dfm$name <- rownames(dfm)

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                      levels = c("low", "high"))
# Inspect the data
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])

2)绘制棒棒糖图

ggplot(dfm, aes(x = name, y = mpg)) +
  geom_hline(yintercept = 0, color = "grey", size = 1) + # 添加y=0的辅助线
  geom_point(aes(color = cyl), size = 2) +         # 将点的size设置大一些比较好看
  geom_bar(aes(fill = cyl), stat = "identity", width = 0.2) + # 注意将width宽度设小
  theme_bw(base_family = "Times") +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),      # 消除竖条的背景线
        axis.text.x = element_text(angle = 90),
        legend.position = "None",
        panel.border = element_blank(),
        # text = element_text(family = "STHeiti"), # Mac 电脑上绘图展现中文需要此行命令
        plot.title = element_text(hjust = 0.5)) +  # 标题居中,若无标题可不加
  labs(x = "name", y = "mpg",
       colour = "", linetype = "", fill = "")

结果如下:

使用R语言绘制棒棒糖图火柴杆图教程

下面我们介绍一种更简便且高级的棒棒糖图绘制方法:使用ggpubr包中的ggdotchart()函数。

使用ggpubr包中的ggdotchart()

这里我们直接看官方介绍的几个例子,来理解函数的使用方式,首先载入依赖包:

library(ggpubr)

1)

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "ascending",                        # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           ggtheme = theme_pubr()                        # ggplot2 theme
)

使用R语言绘制棒棒糖图火柴杆图教程

2)

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), 
           sorting = "asc", sort.by.groups = TRUE,                      
           add = "segments",                            
           add.params = list(color = "lightgray", size = 2), 
           group = "cyl",                                
           dot.size = 4,                                 
           ggtheme = theme_pubclean()
) + font("x.text", size = 8, vjust = 0.5)

使用R语言绘制棒棒糖图火柴杆图教程

3)

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           rotate = TRUE,                                # Rotate vertically
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9,
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
)

使用R语言绘制棒棒糖图火柴杆图教程

4)

ggdotchart(dfm, x = "name", y = "mpg_z",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           add.params = list(color = "lightgray", size = 2), # Change segment color and size
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg_z,1),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9,
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
) + geom_hline(yintercept = 0, linetype = 2, color = "lightgray")

使用R语言绘制棒棒糖图火柴杆图教程

参考

Articles - ggpubr: Publication Ready Plots

Articles - R Graphics Essentials

ggpubr: ‘ggplot2' Based Publication Ready Plots

以上就是使用R语言绘制棒棒糖图火柴杆图教程的详细内容,更多关于R语言绘制棒棒糖图火柴杆图的资料请关注www.580doc.com其它相关文章!


上一篇:使用R语言绘制散点图结合边际分布图教程下一篇:R语言使用ggplot绘制画中画细节放大的方法
程序园_程序员的世界 Copyright © 2020- www.580doc.com. Some Rights Reserved.