📜  如何在 R 中使用 ggplot2 绘制数据帧的子集?

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

如何在 R 中使用 ggplot2 绘制数据帧的子集?

在本文中,我们将讨论如何使用 R 编程语言中的 ggplot2 绘制数据帧的子集。

使用中的数据框:

 AgeScoreEnrollNo
1177005
2188010
3177915
4197520
5188525
6199630
7179035
8197140
9188345

为了获得完整的图片,让我们首先绘制一个完整的数据框。

例子:

R
# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19",
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25",
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96",
           "90", "71", "83")
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Create a simple Scatter Plot
ggplot(data, aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)


R
# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19",
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25", 
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96",
           "90", "71", "83")
  
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Generate R Scatter Plot only where Age 
# variable has value "18"
ggplot(subset(data, Age %in% "18"), aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)


R
# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19", 
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25",
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96",
           "90", "71", "83")
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Create a Variable 'data_subset' 
# which has the values equal to 
# "19" values of Age object.
data_subset <- data[data$Age %in% "19", ]
  
# Generate R Scatter Plot only where
# Age variable has value "19"
ggplot(data_subset, aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)


R
# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19",
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25", 
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96", 
           "90", "71", "83")
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Generate R Scatter Plot only where
# Age variable has value "17"
ggplot(data[data$Age %in% "17", ], aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)


输出:

简单散点图

简单散点图

现在,我们将一一看到使用 DataFrame 的子集创建 R Plot 的所有方法。

方法一:使用subset()函数

在这里,我们使用subset()函数在ggplot()函数仅绘制 DataFrame 的子集,而不是数据DataFrame。所有其他的东西都是一样的。

例子:

电阻



# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19",
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25", 
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96",
           "90", "71", "83")
  
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Generate R Scatter Plot only where Age 
# variable has value "18"
ggplot(subset(data, Age %in% "18"), aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)

输出:

通过sunset() 函数仅使用DataFrame 子集的散点图

通过sunset()函数仅使用DataFrame 子集的散点图

方法 2:使用方括号

在这个方法中,我们没有使用 subset()函数,而是编写了用于检索方括号中 DataFrame 子集的逻辑表达式,并将其分配给子集变量。

例子:

电阻

# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19", 
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25",
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96",
           "90", "71", "83")
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Create a Variable 'data_subset' 
# which has the values equal to 
# "19" values of Age object.
data_subset <- data[data$Age %in% "19", ]
  
# Generate R Scatter Plot only where
# Age variable has value "19"
ggplot(data_subset, aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)

输出:

仅使用方括号的 DataFrame 子集的散点图

仅使用方括号的 DataFrame 子集的散点图

方法 3:在 ggplot()函数使用方括号

在这种方法中,我们使用方括号为 DataFrame 的子集编写表达式,但我们在 ggplot()函数中使用它来代替 dataframe 对象。

例子

电阻

# Load ggplot2 package
library(ggplot2)
  
# Create Data For plotting
Age <- c("17", "18", "17", "19", "18", "19",
         "17", "19", "18")
  
EnrollNo <- c("05", "10", "15", "20", "25", 
              "30", "35", "40", "45")
  
Score <- c("70", "80", "79", "75", "85", "96", 
           "90", "71", "83")
  
# Create a DataFrame from Data
data <- data.frame(Age, EnrollNo, Score)
  
# Generate R Scatter Plot only where
# Age variable has value "17"
ggplot(data[data$Age %in% "17", ], aes(Score, EnrollNo)) + 
  geom_point(color = "green", size = 3)

输出: