最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年11月05日 14:10:59   作者:Kanny廣小隸  
本篇文章為大家介紹幾種利用R語言繪制棒棒糖圖(火柴桿圖)的方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

 使用原生ggplot方法

最容易也是最簡單想到的方法是直接使用ggplot2包進(jìn)行更新,這里需要使用ggplot本身的特性,通過圖層疊加的方式,進(jìn)行最終棒棒糖圖的展現(xiàn)。(寬度極窄的柱狀圖配合散點(diǎn)圖即可呈現(xiàn))

1)生成數(shù)據(jù)

下面我們的展示均以此份數(shù)據(jù)為例:

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) +         # 將點(diǎn)的size設(shè)置大一些比較好看
  geom_bar(aes(fill = cyl), stat = "identity", width = 0.2) + # 注意將width寬度設(shè)小
  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 電腦上繪圖展現(xiàn)中文需要此行命令
        plot.title = element_text(hjust = 0.5)) +  # 標(biāo)題居中,若無標(biāo)題可不加
  labs(x = "name", y = "mpg",
       colour = "", linetype = "", fill = "")

結(jié)果如下:

下面我們介紹一種更簡便且高級的棒棒糖圖繪制方法:使用ggpubr包中的ggdotchart()函數(shù)。

使用ggpubr包中的ggdotchart()

這里我們直接看官方介紹的幾個(gè)例子,來理解函數(shù)的使用方式,首先載入依賴包:

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語言繪制棒棒糖圖火柴桿圖教程的詳細(xì)內(nèi)容,更多關(guān)于R語言繪制棒棒糖圖火柴桿圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

仁怀市| 克东县| 定南县| 蓝田县| 深州市| 巴彦淖尔市| 霍城县| 丁青县| 大悟县| 柘城县| 卢湾区| 承德县| 山阳县| 金昌市| 湖南省| 会理县| 辉县市| 五原县| 镶黄旗| 织金县| 鄱阳县| 玛纳斯县| 泽库县| 文化| 竹山县| 桃园县| 厦门市| 兴仁县| 贡山| 仪征市| 丰顺县| 门源| 宁海县| 武汉市| 赤水市| 万全县| 五寨县| 嘉荫县| 邳州市| 德兴市| 盐亭县|