1

Any idea why I am getting multiple lines on other columns in ggplot when I m trying to plot only one vertical line on col4 in ggplot?

library(tidyr) library(dplyr) library(ggplot2) Col0 <- c("AA", "BB", "CC", "DD","EE","FF") Col1 <- c(2,2,2,6,1,1) Col2 <- c(2,2,2,1,3,4) Col3 <- c(2,2,3,4,6,6) Col4 <- c(2,2,3,1,2,1) Col5 <- c(2,1,1,1,1,4) Col6 <- c(2,4,2,5,4,4) Col7 <- c(2,4,2,5,4,4) Col8 <- c(2,2,3,4,5,4) Col9 <- c(1,3,3,2,2,2) df<-data.frame(Col0,Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9) plotDat <- df %>% gather(Col, Val, -Col0)%>% split(Col0) pdf("plots.pdf") lapply(names(plotDat), function(i){ ggplot(plotDat[[i]], aes(Col, Val, group = Col0, col = Col0)) + geom_line() + geom_vline(xintercept = Col4) + geom_vline(xintercept = Col4, linetype="dotted", color = "blue", size=1.5)+ ggtitle(paste("Plot", i)) }) dev.off() 

enter image description here

1 Answer 1

1

If you want to display a vline on the 4th break on the (discrete) x-axis, set xintercept = 4

So, replace Col4 with 4 in both geom_vline-lines

pdf("plots.pdf") lapply(names(plotDat), function(i){ ggplot(plotDat[[i]], aes(Col, Val, group = Col0, col = Col0)) + geom_line() + geom_vline(xintercept = 4) + geom_vline(xintercept = 4, linetype="dotted", color = "blue", size=1.5)+ ggtitle(paste("Plot", i)) }) dev.off() 

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway to pass column name(x axis) for intercept?
@SamsungNeo, probably.., make it a factor, and try to refer to the correct level

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.