Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I avoid using data frames in ggplot2?

Tags:

I'm running a monte-carlo simulation and the output is in the form:

> d = data.frame(iter=seq(1, 2), k1 = c(0.2, 0.6), k2=c(0.3, 0.4)) > d iter  k1   k2 1     0.2  0.3 2     0.6  0.4 

The plots I want to generate are:

plot(d$iter, d$k1) plot(density(d$k1)) 

I know how to do equivalent plots using ggplot2, convert to data frame

new_d = data.frame(iter=rep(d$iter, 2),                     k = c(d$k1, d$k2),                     label = rep(c('k1', 'k2'), each=2)) 

then plotting is easy. However the number of iterations can be very large and the number of k's can also be large. This means messing about with a very large data frame.

Is there anyway I can avoid creating this new data frame?

Thanks

like image 803
csgillespie Avatar asked Jan 14 '10 11:01

csgillespie


People also ask

Can you use Ggplot without a DataFrame?

Let's return to our first matrix example. 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 Ggplot and ggplot2 the same?

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

What data type does Ggplot expect?

In ggplot2 syntax, we say that they use different geoms. A geom is the geometrical object that a plot uses to represent data. People often describe plots by the type of geom that the plot uses. For example, bar charts use bar geoms, line charts use line geoms, boxplots use boxplot geoms, and so on.


1 Answers

Short answer is "no," you can't avoid creating a data frame. ggplot requires the data to be in a data frame. If you use qplot, you can give it separate vectors for x and y, but internally, it's still creating a data frame out of the parameters you pass in.

I agree with juba's suggestion -- learn to use the reshape function, or better yet the reshape package with melt/cast functions. Once you get fast with putting your data in long format, creating amazing ggplot graphs becomes one step closer!

like image 90
Harlan Avatar answered Oct 13 '22 01:10

Harlan