Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a line from another data.frame to qplot

Tags:

r

ggplot2

Is it possible to add a line (such as an additional time series) to an already existing plot? I know how to add horizontal or vertical lines, but how can I add from other data.frames?

 q_myplot<-qplot(datefield,myvalue,data=mydf,geom=c("line"),colour=category) +opts(axis.title.x = theme_blank()) + scale_x_date(major="2 years")

is my basic plot, showing three different time series grouped by category. Is there a way to add another line to the plot for example by using layers? Of course I could add this additional data to mydf using another category, but I wonder if there's a better idea out there.

like image 984
Matt Bannert Avatar asked Mar 16 '11 09:03

Matt Bannert


People also ask

What is the difference between Ggplot and Qplot?

The function qplot() [in ggplot2] is very similar to the basic plot() function from the R base package. It can be used to create and combine easily different types of plots. However, it remains less flexible than the function ggplot(). This chapter provides a brief introduction to qplot(), which stands for quick plot.

Does Ggplot only work with data frames?

ggplot only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this “long” form with the melt function in the library reshape2 .

Is Qplot part of ggplot2?

The ggplot2 package includes two main functions, quickplot qplot() for fast graphs and the ggplot() function for more detailed, customizable graphs.

What does Qplot do in R?

qplot makes it easy to produce complex plots, often requiring several lines of code using other plotting systems, in one line. qplot() can do this because it's based on the grammar of graphics, which allows you to create a simple, yet expressive, description of the plot.


1 Answers

by using ggplot() instead of qplot(), you can have more flexibility. here is a minimal example using two datasets:

d1 <- data.frame(x1=rep(1:10,3), y1=rnorm(10*3), g1=gl(3,10,labels=letters[1:3]))
d2 <- data.frame(x2=rep(1:10,3), y2=rnorm(10*3), g2=gl(3,10, labels=letters[4:6]))

ggplot() + 
  geom_line(aes(x1, y1, colour=g1), d1) +  
  geom_line(aes(x2, y2, colour=g2), d2)
like image 103
kohske Avatar answered Sep 21 '22 13:09

kohske