Data Aggregation (Grouping & Summarizing) in R

A data frame or table has several columns as conditions and the rest as results, now I want to summary the table by the same conditions. That means, the results columns of the rows with same conditions will be added together.

  1. Using tapply()
  2. Using plyr package

     

 

Reference:

http://stackoverflow.com/questions/9847054/how-to-get-summary-statistics-by-group

http://www.sr.bham.ac.uk/~ajrs/R/r-show_data.html

http://www.psychwire.co.uk/2011/04/aggregate-function-in-r-making-your-life-easier-one-mean-at-a-time/

http://www.psychwire.co.uk/2011/04/data-aggregation-in-r-plyr-sqldf-and-data-table/

R: How to change ggplot2 axis position

In ggplot2 it is not so easy to change the scale position, like showing the Y-axis in the right side or using dual axis. Kohske Takahashi is working on it and it is available in the dev version. (https://groups.google.com/forum/?fromgroups=#!topic/ggplot2-dev/JkJU5CLBkQw). As alternative he provided a solution with gtable http://rpubs.com/kohske/dual_axis_in_ggplot2. For simply change the Y-axis to the right side see http://stackoverflow.com/questions/15334494/how-to-change-positions-of-x-and-y-axis-in-ggplot2

Dual axis:
library(ggplot2)
library(gtable)
library(grid)
grid.newpage()
# two plots
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") + theme_bw()
p2 <- ggplot(mtcars, aes(mpg, drat)) + geom_line(colour = "red") + theme_bw() %+replace%
theme(panel.background = element_rect(fill = NA))
# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))
# overlap the panel of 2nd plot on that of 1st plot
pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t,
pp$l, pp$b, pp$l)
# axis tweaks
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
# draw it
grid.draw(g)

Y axis in right:
library(ggplot2)
library(gtable)
library(grid)
grid.newpage()
dat <- data.frame(x<-seq(0, 10, 0.1),y = sin(x * pi))
p <- ggplot(dat, aes(x, y)) + geom_line(colour = "blue") + theme_bw()
# extract gtable
g <- ggplot_gtable(ggplot_build(p))
# axis tweaks
ia <- which(g$layout$name == "axis-l")
ax <- g$grobs[[ia]]$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
pp <- c(subset(g$layout, name == "panel", select = t:r))
g <- gtable_add_cols(g, g$widths[g$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g$grobs[[ia]]$children[[2]] <- NULL
##############################
ia <- which(g$layout$name == "ylab")
ylab <- g$grobs[[ia]]
g <- gtable_add_cols(g, g$widths[g$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ylab, pp$t, length(g$widths) - 1, pp$b)
g$grobs[[ia]]$label = ''
grid.draw(g)

Using Git with RStudio

RStudio includes integrated support for Git. To use Git with RStudio, below are the steps to get it working:

  1. Setup a GitHub account here.
  2. Download and install Rstudio.
  3. Download and install the platform-specific version of Git (not GitHub), default options   work well.
  4. Configure Git with global commands. I have found this step necessary both times I     ran through this process. Open up the bash version of Git and type the following:
    git config –global user.name “your GitHub account name”
    git config –global user.email “GitHubEmail@something.com”
  5. Open Rstudio and set the path to Git executable. Go to Tools > Global Options > Git/SVN

rstudio_git

 

 

 

http://www.rstudio.com/ide/docs/version_control/overview

R graph with two y-​​axes

Sometimes it is required to use two Y-axes in the same graph. The method to achieve the goal is depended by the basic graphic package, such as ggplot2, lattice. Because ggplot2 is my first choice, so I’m trying to get a solution for ggplot2.

kohske’s solution is the only one I can find right now.

http://rpubs.com/kohske/dual_axis_in_ggplot2

http://stackoverflow.com/questions/18989001/how-can-i-put-a-transformed-scale-on-the-right-side-of-a-ggplot2

http://stackoverflow.com/questions/18509723/display-two-parallel-axes-on-a-ggplot-r/18511024#18511024

http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes

http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/

http://evolvingspaces.blogspot.de/2011/05/multiple-y-axis-in-r-plot.html

http://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes-in-r/

https://groups.google.com/forum/#!topic/ggplot2/eCBtE5ODW1M

http://stackoverflow.com/questions/17661052/force-x-axis-text-on-for-all-facets-of-a-facet-grid-plot/17661337#17661337