📜  如何使用 R 中的数据点制作 Violinplot?

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

如何使用 R 中的数据点制作 Violinplot?

在本文中,我们将讨论如何使用 R 编程语言中的数据点制作 violinplot。

小提琴图是连续分布的紧凑显示。 R 中的 geom_violin() 方法用于在工作空间中构建小提琴图,该图了解各种美学映射,如 alpha、颜色或填充。

句法:

geom_violin()

要构建常规小提琴图,只需在可视化后调用 geom_violin()函数。

示例:常规小提琴图

R
library("ggplot2")
  
# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                        col2=c( sample(2:5, 10 , replace=T) ,
                                sample(4:10, 12 , replace=T),
                                sample(1:7, 18 , replace=T))
)
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  
 # adding violin plot
 geom_violin()


R
# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                         col2=c( sample(2:5, 10 , replace=T) , 
                                sample(4:10, 12 , replace=T), 
                                sample(1:7, 18 , replace=T))
                         )
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  
# adding violin plot
  geom_violin(alpha = 0.5) +
  geom_dotplot(binaxis = "y",
               stackdir = "center",
               dotsize = 0.5)


R
library("ggplot2")
  
# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                         col2=c( sample(2:5, 10 , replace=T) , 
                                 sample(4:10, 12 , replace=T), 
                                 sample(1:7, 18 , replace=T))
)
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  
  # adding violin plot
  geom_violin() +
  geom_jitter()


R
library("ggplot2")
  
# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                         col2=c( sample(2:5, 10 , replace=T) , 
                                 sample(4:10, 12 , replace=T), 
                                 sample(1:7, 18 , replace=T))
)
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  # adding violin plot
  geom_violin() +
  geom_jitter(width=0.15, alpha=0.5)


输出:

将数据点添加到 violinplot

在点图的情况下,点的宽度对应于 bin 宽度。接下来是点堆叠的情况,其中每个点代表一个观察。要添加数据点,我们在创建小提琴图后使用 geom_dotplot()。

示例:将数据点添加到 violinplot

R

# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                         col2=c( sample(2:5, 10 , replace=T) , 
                                sample(4:10, 12 , replace=T), 
                                sample(1:7, 18 , replace=T))
                         )
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  
# adding violin plot
  geom_violin(alpha = 0.5) +
  geom_dotplot(binaxis = "y",
               stackdir = "center",
               dotsize = 0.5)

输出:

为小提琴图添加抖动

可以通过使用随机噪声将 violinplot 与 x 轴上的实际数据点绘制数据点来改进 geom_plot()。这些数据点被称为“抖动”。 R 中的 geom_jitter() 方法用于为每个点的位置添加少量随机变化。

示例:向小提琴图添加抖动

R

library("ggplot2")
  
# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                         col2=c( sample(2:5, 10 , replace=T) , 
                                 sample(4:10, 12 , replace=T), 
                                 sample(1:7, 18 , replace=T))
)
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  
  # adding violin plot
  geom_violin() +
  geom_jitter()

输出:

在小提琴图中指定抖动宽度

小提琴图中数据点的透明度以及宽度可以通过在 R 中的 geom_jitter 方法中指定参数、宽度和 alpha 来临时调整。

示例:在小提琴图中指定抖动宽度

R

library("ggplot2")
  
# defining the columns of the data frame
data_frame <- data.frame(col1=c(rep("A", 10) , rep("B", 12) , rep("C", 18)),
                         col2=c( sample(2:5, 10 , replace=T) , 
                                 sample(4:10, 12 , replace=T), 
                                 sample(1:7, 18 , replace=T))
)
  
# plotting the data frame
ggplot(data_frame, aes(x = col1, y = col2, fill = col1)) +
  # adding violin plot
  geom_violin() +
  geom_jitter(width=0.15, alpha=0.5)

输出: