I am relatively new to R and I am trying to do the following.
I am looking for a way to generate single plots defined by group (based on the ID).
My goal: Generally I am looking for a way to always plot row based points in the following pattern
- starting point: REFERENCE_YEAR/REFERENCE_YEAR_VALUE to end point: SURVEY_YEAR/SURVEY_YEAR_VALUE and connect these points by a line
My sample data.frame looks like this:
In numbers the example for the group plot with ID 87 would be as follows
How I tried to solve this problem:
I tried to reorganise the dataframe by writing REFERENCE_YEAR and SURVEY_YEAR one below the other and reorganised the data into two columns YEAR and VALUE in order to create an xy plot but this didn't result in correct plots as described above.
After that I am splitting it by group (defined by ID) and I'm plotting it into separate graphs.
Does anyone have an idea how I could solve the above mentioned problem maybe also using lines() and points()? I am looking for a solution in Base R. Any help would be kindly appreciated since I am relatively new to R and I am stuck.
Thank you,
redshoe
I am looking for a way to generate single plots defined by group (based on the ID).
My goal: Generally I am looking for a way to always plot row based points in the following pattern
- starting point: REFERENCE_YEAR/REFERENCE_YEAR_VALUE to end point: SURVEY_YEAR/SURVEY_YEAR_VALUE and connect these points by a line
My sample data.frame looks like this:
Code:
xy <- data.frame(NAME=c("NAME1","NAME1","NAME1","NAME2","NAME2","NAME2"),ID=c(87,87,87,199,199,199), SURVEY_YEAR=c(1986,1994,1999,1909,1924,1927), REFERENCE_YEAR=c(1984,1986,1984,1899,1909,1924), VALUE_SURVEY_YEAR=c(20,50,-15,-70,-80,-100),VALUE_REFERENCE_YEAR=c(75,25,-90,-8,-55,-10))
xy
NAME ID SURVEY_YEAR REFERENCE_YEAR VALUE_SURVEY_YEAR VALUE_REFERENCE_YEAR
1 NAME1 87 1986 1984 20 75
2 NAME1 87 1994 1986 50 25
3 NAME1 87 1999 1984 -15 -90
4 NAME2 199 1909 1899 -70 -8
5 NAME2 199 1924 1909 -80 -55
6 NAME2 199 1927 1924 -100 -10
- For row 1 plot a starting point 1984/75 and end point 1986/20 and these two points should be connected by a line
- Then move on to row 2 and plot starting point 1986/25 and end point 1994/50 and connect the points with a line
- Then move on to row 3 and plot a starting point 1984/-90 and end point 1999/-15 and connect the points with a line.
How I tried to solve this problem:
I tried to reorganise the dataframe by writing REFERENCE_YEAR and SURVEY_YEAR one below the other and reorganised the data into two columns YEAR and VALUE in order to create an xy plot but this didn't result in correct plots as described above.
Code:
xy <- reshape(xy, direction="long",
varying = list(c("SURVEY_YEAR","REFERENCE_YEAR"), c("VALUE_SURVEY_YEAR","VALUE_REFERENCE_YEAR")),
v.names=c("YEAR","VALUE"), times = c("SURVEY_YEAR", "REFERENCE_YEAR"))
xy <- xy[with(xy, order(NAME, ID, YEAR,time, id)), ]
Code:
ind <- split(x = xy,f = xy[,'ID'])
plot1 <- function(x) {
fname <- paste0(x[1, 'ID'], '.png')
png(fname, width=1679, height=1165, res=150)
par(mar=c(6,8,6,5))
plot(x = c(1946, 2014),
y = range(x$VALUE, na.rm=TRUE),
type='n',
main=x[1, 'NAME'])
points(x[,c('YEAR','VALUE')], type="l", lwd=2)
points(x[,c('YEAR','VALUE')], type="p", lwd=1, cex=0.7, pch=21, bg='white')
dev.off()
}
plot2 <- function(x) {
fname <- paste0(x[1, 'ID'], '.png')
png(fname, width=1679, height=1165, res=150)
par(mar=c(6,8,6,5))
plot(x[,c('YEAR','VALUE')],
type='n',
main=x[1, 'NAME'])
points(x[,c('YEAR','VALUE')], type="l", lwd=2)
points(x[,c('YEAR','VALUE')], type="p", lwd=1, cex=0.7, pch=21, bg='white')
dev.off()
}
lapply(ind, function(x) ifelse(any(x$YEAR < 1946 & x$YEAR < 2014), plot2(x), plot1(x)))
Thank you,
redshoe