Base R: add points() in a loop defined by group -
i looking way generate different plots in base r adding points , lines manually plot.
xy <- data.frame(name=c("name1","name1","name2","name2"), x_start_year=c(1984,1986,1899,1903), y_start_value=c(75,25,-90,-8),x_end_year=c(1986,1994,1909,1924),y_end_value=c(20,50,-15,-70)) xy name x_start_year y_start_value x_end_year y_end_value 1 name1 1984 75 1986 20 2 name1 1986 25 1994 50 3 name2 1899 -90 1909 -15 4 name2 1903 -8 1924 -70
is possible add points()
plot values first row (starting point x_start_year/y_start value
end point: x_end_year/y_end_value
) , plot again in same plot in same scheme values second row.
after plot has been generated new group defined name
starts , new plot should generated rows 3 , 4 in same scheme above.
unlike in sample data, have 40000 rows , 1300 groups in end , looking way correctly plot data in base r. appreciated!
you can split data name , , plot each group :
layout(matrix(c(1,2))) ## don't use layout in case (dat in split(xy,xy$name)){ xx = unlist(dat[,grep('x_',colnames(dat))]) yy = unlist(dat[,grep('y_',colnames(dat))]) plot(xx,yy,main=unique(dat[,1]),pch=20) dat <- dat[,-1] segments(dat[,1],dat[,2],dat[,3],dat[,4]) }
this result in plot each group. of course if save result other thing console , should define device plot before.
Comments
Post a Comment