Monday 19 January 2015

Using the R ggplot2 package to make a multiple line plot

Here's how I made a multiple line plot using the lovely ggplot2 package:
[note to self: need to do 'ssh -Y' to the farm, to be able to see plots]

> library(ggplot2) # load library
# enter my data, and make a data frame
> var1 <- c(4.5,2.3,2.4,2.1,2.2)
> var2 <- c(33,22,13,23,14)
> var3 <- c(234,234,23,23,1)
> myvalues <- c(var1,var2,var3)
> myx <- rep(c(50,40,30,20,10),3) # the x axis labels
> myvarname <- rep(c("my var1", "my var2", "my var3"),each=5)
> mydata <- data.frame(myx, myvalues, myvarname)

# plot the data:

> myplot <- ggplot(data = mydata, aes(x=myx, y=myvalues)) + geom_line(aes(colour=myvarname),size=2) # size=2 makes a thicker line
> myplot + ylab("Average number") + xlab("Length threshold (kb)")




















Adding a vertical line:
Here's how to add a dashed verticle line at x=40 to the plot:
> myplot <- ggplot(data = mydata, aes(x=myx, y=myvalues)) + geom_line(aes(colour=myvarname),size=2) + geom_vline(xintercept=40,linetype=2) # size=2 makes a thicker line
> myplot + ylab("Average number") + xlab("Length threshold (kb)")

No comments: