## R seminar series - part 7

Last update on March 19, 2015.

ggplot2 and dplyr

The seventh R brownbag seminar was by Dr. Tor G. Vagen (Senior Scientist, ICRAF) who continued from the previous week and demonstrated more advanced use of ggplot2. The dplyr package, which will be the topic of several future seminars was also introduced.

```
# Tor-G Vågen
# R brownbag seminar march 19th, 2015

# For those who do not have dplyr installed... (requires a recent R version)
install.packages("dplyr")

#####
# We will continue with the same dataset as last week...

# Recap a little from last week
library(ggplot2)
ggplot(tree) + geom_point(aes(x=Clay, y=Carbon, size=avTreeDen, colour=Site))

# Graphics produced using ggplot2 can be stored as objects...
p1 <- ggplot(tree) + geom_point(aes(x=Clay, y=Carbon, size=avTreeDen)) #Note the size argument!

print(p1)

# And then used to overlay or combine with other graphs
# for example a smoother
p1 + stat_smooth(aes(x=avTreeDen, y=Carbon))

## Theme the plot/graphic and store as pbject p2
p2 <- p1 + stat_smooth(aes(x=Clay, y=Carbon)) + theme_minimal(base_size=16)

p2

#####
# Use dplyr to create a simple summary of the dataset
library(dplyr)
tree.summary <- tree %>%
group_by(Site) %>%
summarise(count = n(),
meanC = mean(Carbon),
meanClay = mean(Clay))

# And then overlay the means calculated above on p2, coloured by site
p2 + geom_point(data = tree.summary, aes(x=meanClay, y=meanC, colour=Site), size=20, alpha=0.5)
#Note how we are setting the size manually and creating some transparency with alpha
```

Note: For those using older versions of R, the dplyr package requires at least R version 3.0.2. We recommend version 3.1.2 or above.

### Pingbacks

Pingbacks are closed.