如何在 R 中使用 ggplot2 绘制数据帧的子集?
在本文中,我们将讨论如何使用 R 编程语言中的 ggplot2 绘制数据帧的子集。
使用中的数据框: Age Score EnrollNo 1 17 70 05 2 18 80 10 3 17 79 15 4 19 75 20 5 18 85 25 6 19 96 30 7 17 90 35 8 19 71 40 9 18 83 45
为了获得完整的图片,让我们首先绘制一个完整的数据框。
例子:
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。所有其他的东西都是一样的。
Syntax: subset(obj, …)
Parameters:
- It takes data object to be subsetted as it’s first parameter.
- subset() function can have many other parameters. only obj is necessary to take. here we only use logical expression indicating rows as a second argument.
Return : subset() function returns subset of 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)
输出:
方法 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)
输出:
方法 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)
输出: