我的样本data.frame
包含正常数据'在这种情况下,按组生成2个图(这里的组由ID
分割)。您可以在下面找到我的代码:
# Normal Dataframe
xy <- data.frame(NAME=c("NAME1", "NAME1","NAME1","NAME1","NAME1", "NAME1","NAME1","NAME1","NAME2","NAME2","NAME2","NAME2","NAME2","NAME2","NAME2","NAME2","NAME2","NAME2"),ID=c(47,47,47,47,47,47,47,47,259,259,259,259,259,259,259,259,259,259),YEAR=c(1959,1960,1960,1961,1961,1963,1963,1965,2004,2007,2007,2009,2009,2010,2010,2011,2011,2014),VALUE=c(0,-6,-6,-16,-16,-31,-31,-54,0,-9,-9,-24,-24,-69,-69,-92,-92,-132))
xy$ID <-paste(xy$ID, ".png", sep = "") # Add .png Fileextension
ind <- split(x = xy,f = xy[,'ID']) # index split by ID
### PLOT
for(i in 1:length(ind)){
png(names(ind[i]), width=3358, height=2329, res=300)
par(mar=c(6,8,6,5))
plot(ind[[i]][,c('YEAR','VALUE')],
type='n',
main=ind[[i]][1,'NAME'],
xlab="Time [Years]",
ylab="Length change [m]")
# plot axis
axis(1, at = seq(1000,2030,10), cex.axis=1, labels=FALSE, tcl=-0.3)
# plot points and lines
points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1, pch=21, bg='white')
dev.off()
}
这很好用,但我也有定性数据,我想在图中显示。 (示例)data.frame
看起来像这样:
data_qual <- data.frame(NAME=c("NAME1","NAME1","NAME2"),ID=c(47,47,259),SURVEY_YEAR=c(1961,1964,2010), REFERENCE_YEAR=c(1960,1962,2006),VALUE_SURYEAR=c("-X","ST","+x"),VALUE_REFYEAR=c("-X","ST","+x"))
data_qual
NAME ID SURVEY_YEAR REFERENCE_YEAR VALUE_SURYEAR VALUE_REFYEAR
1 NAME1 47 1961 1960 -X -X
2 NAME1 47 1964 1962 ST ST
3 NAME2 259 2010 2006 +x +x
我如何绘制从REFERENCE_YEAR
到SURVEY_YEAR
以及定性VALUE_
中间的线上方的一条线(如果取自{{1}则无关紧要}或VALUE_SURYEAR
因为它们总是相同的)并将它合并到我的情节循环中?有谁知道我怎么能做到这一点?
这是我试图为第一个图做的图形近似:
我是否可以将其合并到我的情节代码中?谢谢你的帮助!
答案 0 :(得分:0)
这是一个解决方案。
它基本上是您的代码,但我为ID + png名称创建了一个新列ID2
。然后我创建了一个带有lines()
函数和text()
函数的子循环来添加行和标签。
xy <-data.frame( NAME = c("NAME1", "NAME1", "NAME1", "NAME1", "NAME1", "NAME1",
"NAME1", "NAME1", "NAME2", "NAME2", "NAME2", "NAME2", "NAME2", "NAME2", "NAME2", "NAME2", "NAME2", "NAME2"),
ID=c(47,47,47,47,47,47,47,47,259,259,259,259,259,259,259,259,259,259),
YEAR=c(1959,1960,1960,1961,1961,1963,1963,1965,2004,2007,2007,2009,2009,2010,2010,2011,2011,2014),
VALUE=c(0,-6,-6,-16,-16,-31,-31,-54,0,-9,-9,-24,-24,-69,-69,-92,-92,-132))
xy$ID2 <-paste(xy$ID, ".png", sep = "") # Add .png Fileextension
ind <- split(x = xy,f = xy[,'ID']) # index split by ID
data_qual <- data.frame( NAME= c("NAME1", "NAME1", "NAME2"),
ID=c(47,47,259), SURVEY_YEAR=c(1961,1964,2010),
REFERENCE_YEAR=c(1960,1962,2006),
VALUE_SURYEAR=c("-X","ST","+x"),VALUE_REFYEAR=c("-X","ST","+x"))
### PLOT
for(i in 1:length(ind)){
png(names(ind[i]), width=3358, height=2329, res=300)
par(mar=c(6,8,6,5))
plot(ind[[i]][,c('YEAR','VALUE')],
type='n',
main=ind[[i]][1,'NAME'],
xlab="Time [Years]",
ylab="Length change [m]")
# plot axis
axis(1, at = seq(1000,2030,10), cex.axis=1, labels=FALSE, tcl=-0.3)
# plot points and lines
points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1, pch=21, bg='white')
for(j in 1 : nrow(data_qual[data_qual$ID==names(ind[i]),])) {
lines(
x=c(data_qual[data_qual$ID==names(ind[i]),]$REFERENCE_YEAR[j], data_qual[data_qual$ID==names(ind[i]),]$SURVEY_YEAR[j] ),
y=c(min(ind[[i]][,"VALUE"]), min(ind[[i]][,"VALUE"]) )
)
text(
x=mean(c(data_qual[data_qual$ID==names(ind[i]),]$REFERENCE_YEAR[j], data_qual[data_qual$ID==names(ind[i]),]$SURVEY_YEAR[j])),
y=min(ind[[i]][,"VALUE"]+3),
labels=as.character(data_qual[data_qual$ID==names(ind[i]),]$VALUE_SURYEAR[j])
)
}
dev.off()
}
}