0

I'd like to create a function that will conveniently accept a data frame, then, for each vector in the data frame, plot a histogram. For each additional vector (beyond the first), my function should also print possibly several scatterplots: the additional vector on each other vector in the list of vector arguments.

The result should be a triangular array of plots, ideally faceted.

This is stumping me. So far, I have is the following:

library(ggplot2)
library(reshape2)

scatterHistoChart <- function(data) {

    require(ggplot2)
    require(reshape2)

    #data <- data.frame(...)
    data <- melt(data)

    graph <- ggplot(data,aes(x=value)) + 
             geom_histogram() +
            # geom_point(aes(x=value)) +
             facet_wrap(~variable,scales = "free_x")

    return(graph)

}


scatterHistoChart(diamonds)

Edit

Where * is a histogram, and + is a scatter plot, the following is an illustration of intended result:

1-vec| *
2-vec| * +
3-vec| * + +
4-vec| * + + +
5-vec| * + + + +

etc. Note that ordering of plots does not matter; the *'s and +'s can be intermixed.

Jefftopia
  • 2,105
  • 1
  • 26
  • 45
  • 1
    This is both too broad and unclear. You're asking too much. Start smaller. Try writing a function that takes two vectors and then plots two faceted histograms. Even better, don't try putting it in a function. Just string some commands together, and then put _that_ in a function. – joran Feb 20 '14 at 16:48
  • maybe `d = stack(list(x1=x1,x2=x2,x3=x3)); qplot(values, data=d, facets=~ind)` helps? – baptiste Feb 20 '14 at 17:04
  • @Joran, why is my question too broad? I feel it is rather clear, so if clarification is needed, can you help me diagnose what needs clarifying? In response to starting smaller: I have done this already and have other plotting functions. – Jefftopia Feb 20 '14 at 17:06
  • Where to begin? Creating a triangular array of plots with ggplot will be quite complicated, and is not an appropriate thing to just ask us to write for you. Similarly, you're just asking us to write the piece that takes multiple vectors and puts them in a data frame in the right format. You need to make an attempt first and ask for help with that... – joran Feb 20 '14 at 17:12
  • 1
    As for clarity...should all the histograms/scatterplots be in the same triangular grid? How should they be arranged? In what order? Should they be on separate plots? Do you want scatterplots of every single pair, or just some pairs? There is _a lot_ that needs fleshing out. Like I said, show us an actual attempt that doesn't work, perhaps on a smaller version of your task. – joran Feb 20 '14 at 17:14
  • Thanks Joran. I added some notes and updated my script. Agreed, the task at hand seems complicated, which is why I'm seeking input from this excellent community. As my writing states, separate scatter plots for every pair is desirable. My current example prints only histograms; the commented portion fails to generate any scatterplots. – Jefftopia Feb 20 '14 at 17:40
  • 2
    This sounds like a scatterplot matrix with histograms in the diagonals. Look at the documentation for `ggpairs(...)` in the `GGally` package. – jlhoward Feb 20 '14 at 19:15
  • As of a few minutes ago, I reached the same conclusion as jlhoward. the `GGally` package appears to have an implementation of what I'd like to accomplish. I will close my question if you'd like to make your comment an answer. – Jefftopia Feb 20 '14 at 19:30

0 Answers0