R 对于数据帧列表中的循环绘图
我想画多个情节 所以我分割了数据帧,并尝试绘制R 对于数据帧列表中的循环绘图,r,dataframe,ggplot2,split,R,Dataframe,Ggplot2,Split,我想画多个情节 所以我分割了数据帧,并尝试绘制 linkid <- unique (orgin_dat$stdLinkId) temp <- split(orgin_dat, orgin_dat$stdLinkId) p1 <- ggplot(temp$`1550007100`, aes(x=datetime, y=linkSpeed)) + geom_line() + ggtitle(linkid[i]) ## it works p1 <- ggplot
linkid <- unique (orgin_dat$stdLinkId)
temp <- split(orgin_dat, orgin_dat$stdLinkId)
p1 <- ggplot(temp$`1550007100`, aes(x=datetime, y=linkSpeed)) +
geom_line() +
ggtitle(linkid[i])
## it works
p1 <- ggplot(temp$linkid[1], aes(x=datetime, y=linkSpeed)) +
geom_line() +
ggtitle(linkid[i])
## it doesn't work
谢谢。如果没有一个可重复的例子,很难得到一个明确的答案。但是,您可以尝试以下解决方案,该解决方案首先不需要拆分数据:
for (i in unique(orgin_dat$stdLinkId)){
assign(paste0("p", i),
ggplot(filter(orgin_dat, stdLinkId == i),
aes(x=datetime, y=linkSpeed)) +
geom_line() +
ggtitle(paste0(i))
}
注意:您需要
library(dplyr)
才能使用filter()
的temp$linkid[1]
是否产生了您所期望的效果?您可能需要类似于temp[linkid[1]]
的东西,但如果不知道temp
&linkid
是什么样子,就很难解决确切的问题。似乎您正在尝试将$
和[
组合起来以对列表进行子集设置?请尝试temp[[i]]
而不是temp$linkid[i]
。我建议使用列表
来存储绘图,而不是使用分配
。如果您发布一个可复制的示例,我可以尝试提供更多帮助(可能使用mtcars
内置数据?)。
for (i in unique(orgin_dat$stdLinkId)){
assign(paste0("p", i),
ggplot(filter(orgin_dat, stdLinkId == i),
aes(x=datetime, y=linkSpeed)) +
geom_line() +
ggtitle(paste0(i))
}