如何在 R 中绘制特定的行和列
How to plot specific rows and columns in R
我的数据框包含 2019 年 1 月至 2020 年 10 月期间在英格兰 9 个不同地区报告的 5 种犯罪类型(加上它们各自的发生次数)的列表。
我想为每个区域分别绘制一个以犯罪事件为 Y 轴、以日期为 X 轴的图表,而不是多面图。
为此,我必须挑出我想要绘制的区域;我试过
df$region == "Region_name"
但是没有成功。
如有任何建议,我们将不胜感激!
这是我使用的数据框示例:
structure(list(
Region = c("Yorkshire and The Humber", "East of England",
"East Midlands", "North East", "South East", "South East", "South East",
"East Midlands", "East Midlands", "London", "North West", "South West",
"East of England", "East Midlands", "East Midlands", "South East",
"Yorkshire and The Humber", "East of England", "East of England",
"East of England"),
Date = structure(c(18109, 18383, 17928, 18293,
18414, 18536, 18170, 17987, 18322, 18140, 18383, 18475, 18231,
18048, 18353, 18322, 18475, 18170, 18109, 17897), class = "Date"),
Crime = c("Violence and sexual offences", "Robbery", "Violence and sexual offences",
"Robbery", "Burglary", "Violence and sexual offences", "Robbery",
"Burglary", "Violence and sexual offences", "Robbery", "Robbery",
"Burglary", "Robbery", "Theft", "Violence and sexual offences",
"Burglary", "Burglary", "Robbery", "Anti-social behaviour",
"Theft"),
Crime_occurrencies = c(21001L, 177L, 6033L, 82L,
2635L, 24096L, 590L, 1536L, 7388L, 3205L, 163L, 981L, 232L,
5339L, 6367L, 3375L, 3238L, 257L, 10982L, 7906L)),
class = "data.frame", row.names = c(NA, -20L))
您可以像这样为每个区域创建一个线图:
library(ggplot2)
#Code
ggplot(df,aes(x=Date,y=Crime_occurrencies,color=Region))+
geom_line()+
geom_point()
输出:
更新:接下来的代码将为每个区域创建一个列表:
#Unique regions
uni <- unique(df$Region)
#Plot func
myplot <- function(x)
{
G <- ggplot(subset(df,Region==x),aes(x=Date,y=Crime_occurrencies,color=Crime))+
geom_line()+
geom_point()+
ggtitle(x)
G
}
#Apply
List <- lapply(uni, myplot)
只要在控制台输入List
,所有的图就会出现。
一些输出:
我的数据框包含 2019 年 1 月至 2020 年 10 月期间在英格兰 9 个不同地区报告的 5 种犯罪类型(加上它们各自的发生次数)的列表。
我想为每个区域分别绘制一个以犯罪事件为 Y 轴、以日期为 X 轴的图表,而不是多面图。
为此,我必须挑出我想要绘制的区域;我试过
df$region == "Region_name"
但是没有成功。 如有任何建议,我们将不胜感激!
这是我使用的数据框示例:
structure(list(
Region = c("Yorkshire and The Humber", "East of England",
"East Midlands", "North East", "South East", "South East", "South East",
"East Midlands", "East Midlands", "London", "North West", "South West",
"East of England", "East Midlands", "East Midlands", "South East",
"Yorkshire and The Humber", "East of England", "East of England",
"East of England"),
Date = structure(c(18109, 18383, 17928, 18293,
18414, 18536, 18170, 17987, 18322, 18140, 18383, 18475, 18231,
18048, 18353, 18322, 18475, 18170, 18109, 17897), class = "Date"),
Crime = c("Violence and sexual offences", "Robbery", "Violence and sexual offences",
"Robbery", "Burglary", "Violence and sexual offences", "Robbery",
"Burglary", "Violence and sexual offences", "Robbery", "Robbery",
"Burglary", "Robbery", "Theft", "Violence and sexual offences",
"Burglary", "Burglary", "Robbery", "Anti-social behaviour",
"Theft"),
Crime_occurrencies = c(21001L, 177L, 6033L, 82L,
2635L, 24096L, 590L, 1536L, 7388L, 3205L, 163L, 981L, 232L,
5339L, 6367L, 3375L, 3238L, 257L, 10982L, 7906L)),
class = "data.frame", row.names = c(NA, -20L))
您可以像这样为每个区域创建一个线图:
library(ggplot2)
#Code
ggplot(df,aes(x=Date,y=Crime_occurrencies,color=Region))+
geom_line()+
geom_point()
输出:
更新:接下来的代码将为每个区域创建一个列表:
#Unique regions
uni <- unique(df$Region)
#Plot func
myplot <- function(x)
{
G <- ggplot(subset(df,Region==x),aes(x=Date,y=Crime_occurrencies,color=Crime))+
geom_line()+
geom_point()+
ggtitle(x)
G
}
#Apply
List <- lapply(uni, myplot)
只要在控制台输入List
,所有的图就会出现。
一些输出: