使用R語言繪制棒棒糖圖火柴桿圖教程

 使用原生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語言繪制棒棒糖圖火柴桿圖的資料請關註WalkonNet其它相關文章!

推薦閱讀: