📌  相关文章
📜  如何使用 ggplot2 在 R 中制作哑铃图?

📅  最后修改于: 2022-05-13 01:55:23.605000             🧑  作者: Mango

如何使用 ggplot2 在 R 中制作哑铃图?

哑铃图显示了我们数据集中两点之间的变化。因其呈哑铃状而得名。它帮助我们分类地理解数据的跨度。为了使用 ggplot2 在 R 中制作哑铃图,我们使用 geom_dumbbell()函数。

使用 ggplot2,可以使用 geom_dumbbell()函数构建哑铃图。必须至少向 aes() 提供三个变量,即 y、x 和 xend。

创建基本的哑铃图

这是使用 ggplot2 包的基本哑铃图。



R
# create data sets
ylabel <- c("first","second","third")
x1 <- c(1,2,3)
x2 <- c(4,3,5)
datamain <- data.frame(ylabel,x1,x2)
  
# import ggplot2, ggalt and tidyverse
library(ggplot2) 
library(ggalt)   
library(tidyverse)
  
# Draw dumbbell plot
ggplot() + geom_dumbbell(data = datamain, 
                         aes(y = ylabel,
                             x = x1, 
                             xend = x2),
                         size = 1.5)


R
# create data sets
ylabel <- c("first","second","third")
x1 <- c(1,2,3)
x2 <- c(4,3,5)
datamain <- data.frame(ylabel,x1,x2)
  
# import ggplot2, ggalt and tidyverse
library(ggplot2) 
library(ggalt)   
library(tidyverse)
  
# Draw dumbbell plot
ggplot() + geom_dumbbell(data = datamain,
                         aes(y = ylabel,
                             x = x1,
                             xend = x2),
                         size = 1.5,
                         size_x = 5, 
                         size_xend = 9)


R
# create data sets
ylabel <- c("first","second","third",
            "fourth","fifth","Sixth")
x1 <- c(1,2,4,5,3,2)
x2 <- c(4,3,6,7,5,4)
  
datamain <- data.frame(ylabel,x1,x2)
  
# import ggplot2, ggalt and tidyverse
library(ggplot2)  
library(ggalt)    
library(tidyverse)
  
# Draw dumbbell plot
ggplot() +
 geom_dumbbell(data = datamain, aes(y = ylabel,
                                    x = x1, 
                                    xend = x2),
               size = 1.5, color = "blue", size_x = 7,
               size_xend = 7, colour_x = "green",
               colour_xend = "yellow")


输出:

在哑铃图中自定义尺寸:

我们可以通过改变哑铃图中圆的半径或中间线段的宽度来自定义哑铃图。

电阻

# create data sets
ylabel <- c("first","second","third")
x1 <- c(1,2,3)
x2 <- c(4,3,5)
datamain <- data.frame(ylabel,x1,x2)
  
# import ggplot2, ggalt and tidyverse
library(ggplot2) 
library(ggalt)   
library(tidyverse)
  
# Draw dumbbell plot
ggplot() + geom_dumbbell(data = datamain,
                         aes(y = ylabel,
                             x = x1,
                             xend = x2),
                         size = 1.5,
                         size_x = 5, 
                         size_xend = 9)

输出:

在哑铃图中自定义颜色:

我们可以使用 color_x 和 color_xend 属性在哑铃图中自定义颜色。

电阻

# create data sets
ylabel <- c("first","second","third",
            "fourth","fifth","Sixth")
x1 <- c(1,2,4,5,3,2)
x2 <- c(4,3,6,7,5,4)
  
datamain <- data.frame(ylabel,x1,x2)
  
# import ggplot2, ggalt and tidyverse
library(ggplot2)  
library(ggalt)    
library(tidyverse)
  
# Draw dumbbell plot
ggplot() +
 geom_dumbbell(data = datamain, aes(y = ylabel,
                                    x = x1, 
                                    xend = x2),
               size = 1.5, color = "blue", size_x = 7,
               size_xend = 7, colour_x = "green",
               colour_xend = "yellow")

输出: