glow
glow

Reputation: 35

Error in strsplit(filename, "\\.") : non-character argument in ggplot

I am trying to make the plot with horizontal lines where the data2 and data3 points should be within data1 range. This will give an overlapping lines in different colors but I am getting an error which says:

Error in strsplit(filename, "\\.") : non-character argument

Here is the data and code. Please give me some suggestion.

 data1 <- data.frame(Start=c(10),End=c(19))

 data2 <- data.frame(Start=c(5),End=c(15))

 data3 <- data.frame(Start=c(6),End=c(18))

 filter_data2 <- data2[data2$Start >= (data1$Start-(data1$Start/2)) & data2$End <= (data1$End+(data1$End/2)), ]

 filter_data3 <- data3[data3$Start >= (data1$Start-(data1$Start/2)) & data3$End <= (data1$End+(data1$End/2)), ]

 data1 <- data.frame(rep(1,nrow(data1)),data1)

 colnames(data1) <- c("ID","start","end")   

 data2 <- data.frame(rep(2,nrow(filter_data2)),filter_data2)

 colnames(data2) <- c("ID","start","end")

 data3 <- data.frame(rep(3,nrow(filter_data3)),filter_data3)

 colnames(data3) <- c("ID","start","end")

 dat1 <- rbind(data1,data2,data3)

 pdf("overlap.pdf")

 p <- ggplot(dat1, aes(x=(max(start)-max(start)/2), y = ID, colour=ID)) 
 p <- p + geom_segment(aes(xend =(max(end)+max(end)/2), ystart = ID, yend = ID))
 p <- p + scale_colour_brewer(palette = "Set1")

 ggsave(p)

Upvotes: 0

Views: 2372

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98599

There are two problems in your code. If you want to use scale_colour_brewer() then ID values should be set as factor

p <- ggplot(dat1, aes(x=(max(start)-max(start)/2), y = ID, colour=as.factor(ID)))

Next, to save the ggplot2 plot you have two possibilities.

Using ggsave() function you should provide file name and format. In this case function pdf() is unnecessary.

 ggsave(plot=p,file="plot.pdf")

Using function pdf(), you should add print(p) and then dev.off(). In this case you don't need ggsave() function.

pdf("overlap.pdf")
print(p)
dev.off()

Upvotes: 0

Related Questions