IceCreamToucan
IceCreamToucan

Reputation: 28695

Change line type on specified interval in R

Using the plot() function, is it possible to change the line type over a certain interval (e.g. from x=1 to x=2) and leave the rest of the plot as another line type?

I know I could use lines() multiple times for the same effect, but I'm wondering if there's an easier way.

Upvotes: 1

Views: 523

Answers (1)

prateek1592
prateek1592

Reputation: 547

How about using ggplot instead?

data <- data.frame(matrix(rnorm(20),20))
names(data) <- "series"

library(reshape2)
library(dplyr)

data <- data.frame(cbind(Index=1:nrow(data),data))
data$Col <- data$Index < 8 & data$Index > 3
ggplot(data, aes(x=Index,y=series,color=factor(Col))) + 
  geom_line(aes(group=1),size=1) +
  guides(colour=F)

enter image description here

Upvotes: 1

Related Questions