Question: Continuously add lines to ggplot with for loop
1
frcamacho • 190 wrote:
Hi there,
I have an object called gPlot and I want to continuously add geom_lines to the gPlot object.
df is a dataframe
sstart ssend
167 2637
552 8273
My following code is :
gPlot<-ggplot() + geom_line(aes(x=1:6549, y=1, colour="red")) + geom_line(aes(x=295:1218, y=.99, colour="blue")) + geom_line(aes(x=3084:4340, y=.99, colour="green"))+coord_cartesian(xlim = c(0, 6549), ylim = c(0,1)) + xlab("coordinates") + ylab("regions")
geomLine <- NULL
for(i in 1:nrow(df)){
rowDF<-df[i,]
dfstart <-rowDF$sstart
dfend<-rowDF$send
geomLine<-gPlot+geom_line(aes(x=dfstart:dfend, y=.95, colour="black"))
}
However, when I plot the new ranges from the df, the plot does not add all the ranges but, just puts the last number range. Any help would be appreciated.
Thanks!
ADD COMMENT
• link
•
modified 10 months ago
by
mmyousefies • 0
•
written
4.1 years ago by
frcamacho • 190
did you find the reason why this happen in the for loop?