如何使用R语言绘制棒棒糖图火柴杆图好代码教程

使用原生ggplot方法 最容易也是最简单想到的方法是直接使用ggplot2包进行更新,这里需要使用ggplot本身的特性,通过图层叠加的方式,进行最终棒棒糖

 使用原生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 = "")

结果如下:

下面我们介绍一种更简便且高级的棒棒糖图绘制方法:使用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
)

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)

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
)

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")

参考

Articles - ggpubr: Publication Ready Plots

Articles - R Graphics Essentials

ggpubr: ‘ggplot2' Based Publication Ready Plots

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