Tag: ggplot2

  • Multiple plots with different titles using ggplot lapply…

    annonce femme cherche homme drancy Multiple plots with different titles using ggplot, lapply, and do.call.

    parameters = data.frame(p1=letters[1:5], p2=round(rnorm(5),2)) l = replicate(5, data.frame(x=1:10, y=rnorm(10)), simplify=FALSE) names(l) = do.call(paste, c(parameters, sep=","))   plot_one = function(x) ggplot(data = l[[x]]) + geom_path(aes(x, y)) + opts(title = x)   plots = lapply(names(l), plot_one)   do.call(gridExtra::grid.arrange, plots)

    Created by Pretty R at inside-R.org

    http://stackoverflow.com/questions/10726470/ggplot-over-many-data-frames-changing-titles

  • Side by side comparison between ggplot2 and lattice…

    Side by side comparison between ggplot2 and lattice
    http://learnr.wordpress.com/tag/ggplot2/

    Code examples and plot output
    http://wiki.stdout.org/rcookbook/Graphs/

    And of course,
    http://had.co.nz/ggplot2/

  • Differences in zooming or scaling in ggplot Either…

    Differences in zooming or scaling in ggplot.

    Either scale_y_continuous() or coord_cartesian() can be used but they work in slightly different way.
    scale_y_continuous (or scale_x_continuous) will drop data which are out of the range, while coord_cartesian() won’t. It does not affect the plot if the plot is drawn only with the given data. However, if the plot includes processed data by ggplot, such as smoothing or histogram, the result may not the same.

    http://had.co.nz/ggplot2/coord_cartesian.html

  • Plot multi column data with ggplot ggplot is…

    Plot multi column data with ggplot

    ggplot is a great visualization tool for R. It draws beautiful plots but the difference from the native plotting system in R takes some time to get used to it.

    Here are two examples how to plot data in multiple columns.
    The original data have three columns with one x-variable and two y-variables. The data look like this.

    head(e.plot$nucl.di.sm)
    
         x       aatt       ggcc
    1  -71 0.10117730 0.05899822
    2  -70 0.09955112 0.05715069
    3  -69 0.09949577 0.05404929
    4  -68 0.09990107 0.05115649
    5  -67 0.09933432 0.04910463
    6  -66 0.09688013 0.04802868
    7  -65 0.09345548 0.04829135
    8  -64 0.09024192 0.04977192
    9  -63 0.08826623 0.05155951
    10 -62 0.08823234 0.05227631

    First, I can use separate geom for each column.

    e.plot$dinucl.plot <- ggplot(e.plot$nucl.di.sm)
    
    e.plot$dinucl.plot + geom_line(aes(x = sidx.plot.x, y = aatt, colour = 'AATT')) 
      + geom_line(aes(x = sidx.plot.x, y = ggcc, colour = 'GGCC')) 
    + scale_colour_discrete("Pattern")
     + xlab("Dist from dyad") + ylab("Dinucleotide frequency")
    

    This was the first approach and the result is below.

    Plotting was easy but I had to spend quite a bit of time to figure out how to change the color and put the legend. Check Hadley's answer for this. How to change the legend title

    Then I found there is another way of doing it. It involves reshaping the data using melt() came with reshape package.

    e.plot$test.melt <- melt(e.plot$nucl.di.sm, measure.vars=c('aatt', 'ggcc'))      
    

    After reshaping by melt, the data look like this.

    head(e.plot$test.melt)
    
         x variable      value
    1  -71     aatt 0.10117730
    2  -70     aatt 0.09955112
    3  -69     aatt 0.09949577
    4  -68     aatt 0.09990107
    5  -67     aatt 0.09933432
    6  -66     aatt 0.09688013
    7  -65     aatt 0.09345548
    8  -64     aatt 0.09024192
    9  -63     aatt 0.08826623
    10 -62     aatt 0.08823234
    

    Then the data can be plotted with one geom.

    e.plot$test.melt.ggplot <- ggplot(e.plot$test.melt)
     
    e.plot$test.melt.ggplot + geom_line(aes(colour = variable)) 
    + scale_colour_discrete("Pattern") 
    + xlab("Dist from dyad")
     + ylab("Dinucleotide frequency")
    

    With this method, ggplot took care of colors and legend automatically. Cool! Here is the result.

    As you can see the two results are almost identical except for the label in the legend. The label follows the column name of the data. I have a feeling, also from several comment online, that ggplot prefers long table to wide table or one column for y variable. If you want this approach, melt will be a invaluable tool and ggplot takes care of many formatting jobs so that user can save lots of time.

    http://stackoverflow.com/questions/1787578/problem-with-legend-while-plotting-data-from-two-data-frame
    http://stackoverflow.com/questions/1313954/plotting-two-vectors-of-data-on-a-ggplot2-scatter-plot-using-r

    Update: I found a way to change the labels for the legend.

    e.plot$test.melt.ggplot <- ggplot(e.plot$test.melt)
    e.plot$test.melt.ggplot + geom_line(aes(colour = factor(variable, labels = c("AATT", "GGCC")))) 
    + scale_colour_discrete("Pattern")
    + xlab("Dist from dyad") 
    + ylab("Dinucleotide frequency")
    

    http://stackoverflow.com/questions/2339953/how-to-add-custom-series-labels-to-a-legend-in-rs-ggplot

  • Using smooth spline in stat scale in ggplot2…

    Using smooth.spline in stat_scale in ggplot2

    smooth.spline2 <- function(formula, data, ...) { 
      mat <- model.frame(formula, data) 
      smooth.spline(mat[, 2], mat[, 1]) 
    } 
    
    predictdf.smooth.spline <- function(model, xseq, se, level) { 
      pred <- predict(model, xseq) 
      data.frame(x = xseq, y = pred$y) 
    } 
    
    qplot(mpg, wt, data = mtcars) + geom_smooth(method = "smooth.spline2", se= F)
    

    From: http://groups.google.com/group/ggplot2/browse_thread/thread/149dfa0891fe383a

  • Setting a scale in ggplot2 scale_y_cont …

    Setting a scale in ggplot2
    scale_y_continuous(limits = c(10, 200))

  • Save ggplot in pdf Use dev.copy2pdf(fil …

    Save ggplot in pdf
    Use dev.copy2pdf(file=”plot.pdf”)

    Example)
    plotA <- ggplot() + geom_line(data=nucleosome.mono.colsum, aes(seq, y=A, colour="A") ) plotT <- ggplot() + geom_line(data=nucleosome.mono.colsum, aes(seq, y=T, colour="T") ) arranged.plot <- grid.arrange(plotA, plotT, nrow=2) dev.copy2pdf(file="arranged.plot") dev.off()

  • Multi plots in a page gridExtra package …

    Multi plots in a page
    gridExtra package makes it easier to put multiple plots generated by ggplot2 in a page equivalent to par(mfrow= ).
    Example:
    http://code.google.com/p/gridextra/wiki/arrange
    came across through a conversation in StackOverflow
    http://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2-in-r