📜  如何在 R 中创建堆叠点图?

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

如何在 R 中创建堆叠点图?

堆叠点图是一种使用点堆叠显示频率的图。主要有两种方法来制作堆叠点图,本文讨论了这两种方法。

方法一:使用stripchart()

所以,使用第一种方法,带状图方法,来创建我们的堆积点图。例如,如果我们没有一套价值观,我们甚至可以让它们为我们的目的服务。我们将创建一组包含 0 到 30 范围内的数字的值,也包括这两个范围,即,该组值还将包括 0 和 30。创建一组值后,我们将绘制基于堆叠的点图在这些价值观上。我们将使用函数set.seed() 来重现特定的“随机”数字序列。 stripchart 生成给定数据的一维散点图(或点图)。

例子:

R
# sets the starting number used 
# to generate a sequence of random 
# numbers
set.seed(0)
  
# shows 100 such randomly generated
# numbers from 0 to 20
data <- sample(0:30, 500, replace = TRUE)
  
# creates the stacked data plot
stripchart(data, method = "stack")


R
# sets the starting number which is used
# to generate a sequence of random numbers
set.seed(0)
  
# shows 100 such randomly generated 
# numbers from 0 to 20
data <- sample(0:30, 500, replace = TRUE)
  
# creats the stacked dotplot, given 
# some more parameters to make the
# stacked dotplot look more attractive
stripchart(data, method = "stack", at = 0, 
           pch = 16, col = "darkgreen",
           main = "Stacked Dot Plot", 
           xlab = "X-Axis Values", 
           ylab = "Y-Axis Values")


R
# loads required package
require(ggplot2)
  
# sets the starting number used 
# to generate a sequence of random 
# numbers
set.seed(0)
  
# shows 100 such randomly generated
# numbers from 10 to 50
data <- data.frame(x = sample(10:50, 100, replace = TRUE))
  
# creates the stacked dot plot
ggplot(data, aes(x = x)) + geom_dotplot()


R
# load ggplot2
library(ggplot2)
  
set.seed(0)
data <- data.frame(x = sample(0:20, 100, replace = TRUE))
  
# create customized stacked dot plot
ggplot(data, aes(x = x)) +
  geom_dotplot(dotsize = 1.5, stackratio = 1, 
               fill = "darkgreen", color = "green") + 
labs(title = "Stacked Dot Plot", x = "X-Axis", y = "Y-Axis")


输出:

但是,我们制作的点图并不那么令人愉快,就像整个堆叠的点图都在 X 轴上方,所以现在我们要对其进行一些编辑,使其看起来更有趣。

例子:

电阻

# sets the starting number which is used
# to generate a sequence of random numbers
set.seed(0)
  
# shows 100 such randomly generated 
# numbers from 0 to 20
data <- sample(0:30, 500, replace = TRUE)
  
# creats the stacked dotplot, given 
# some more parameters to make the
# stacked dotplot look more attractive
stripchart(data, method = "stack", at = 0, 
           pch = 16, col = "darkgreen",
           main = "Stacked Dot Plot", 
           xlab = "X-Axis Values", 
           ylab = "Y-Axis Values")

输出:

方法 2:使用 geom_dotplot()

在点图中,点的宽度对应于 bin 宽度(或最大宽度,取决于 binning 算法),并且点是堆叠的,每个点代表一个观察值。



例子:

电阻

# loads required package
require(ggplot2)
  
# sets the starting number used 
# to generate a sequence of random 
# numbers
set.seed(0)
  
# shows 100 such randomly generated
# numbers from 10 to 50
data <- data.frame(x = sample(10:50, 100, replace = TRUE))
  
# creates the stacked dot plot
ggplot(data, aes(x = x)) + geom_dotplot()

输出:

同样,对于这个,我们可以让它更有趣一些,添加更多参数。

例子:

电阻

# load ggplot2
library(ggplot2)
  
set.seed(0)
data <- data.frame(x = sample(0:20, 100, replace = TRUE))
  
# create customized stacked dot plot
ggplot(data, aes(x = x)) +
  geom_dotplot(dotsize = 1.5, stackratio = 1, 
               fill = "darkgreen", color = "green") + 
labs(title = "Stacked Dot Plot", x = "X-Axis", y = "Y-Axis")

输出: