r - Multiple polygons in one axis using ggplot -
im new in r programming, want plot multiple triangles in 1 chart. when placed ggplot command inside loop, resets chart viewer.but want see plots in 1 plot simultaneously. here's code have been working on.
data<-read.csv("test.csv",sep=",",header=true) library("ggplot2") for(i in 1:5){ d=data.frame(x=c(data$x1[i],data$x2[i],data$x3[i]), y=c(data$y1[i],data$y2[i],data$y3[i])) print(ggplot()+ (geom_polygon(data=d, mapping=aes(x=x,y=y),col="blue"))) }
i hope can me.many thanks
we can use data.table package keep our reshaping 1 step allows specify patterns measure-columns.
first, create id each observation:
dat$id <- 1:nrow(dat)
then create our data in long format. best format ggplot: each observation (or point) on it's own row.
library(data.table) dat_m <- melt(setdt(dat),measure=patterns(c("^x","^y")),value.name=c("x","y"))
plotting easy:
p <- ggplot(dat_m, aes(x=x,y=y,group=id)) + geom_polygon() p
data used:
dat <- structure(list(x1 = c(1, 3, 5), x2 = c(2, 4, 6), x3 = c(1, 3, 5), y1 = c(1, 1, 1), y2 = c(1, 1, 1), y3 = c(2, 2, 2)), .names = c("x1", "x2", "x3", "y1", "y2", "y3"), row.names = c(na, -3l), class = "data.frame")
Comments
Post a Comment