Excersice with graphs
plotting two variables in one graph with ggplot
# load data set
data(iris)
# add some bogus treatment
iris$treat <- "none"
iris$treat[iris$Sepal.Width < 3] <- "small"
iris$treat[iris$Sepal.Width >= 3] <- "large"
iris$treat <- as.factor(iris$treat)
# load ggplot library
library(ggplot2)
# basic graph
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))
p <- p + geom_point()
p
# two variables
ggplot(iris, aes(x = Species)) +
stat_summary(aes(y = Sepal.Width), fun.data = "mean_sdl", mult = 1) +
stat_summary(aes(y = Sepal.Length), fun.data = "mean_sdl", mult = 1) +
labs(y = "Length")
In this graph it is difficult to adjust the position of Sepal.Width and Sepal.Length
Better to get all variables into one format and plot them all in one ggplot layer.
# load the reshape package
library(reshape)
## Loading required package: plyr
##
## Attaching package: 'plyr'
##
## The following objects are masked from 'package:Hmisc':
##
## is.discrete, summarize
##
##
## Attaching package: 'reshape'
##
## The following objects are masked from 'package:plyr':
##
## rename, round_any
Re-organise (melt) the data into a common format. This puts all variables in one common column, and all corresponding values in another
new.iris <- melt(iris, id = c("Species", "treat"))
# inspect the re-organised data
head(new.iris)
## Species treat variable value
## 1 setosa large Sepal.Length 5.1
## 2 setosa large Sepal.Length 4.9
## 3 setosa large Sepal.Length 4.7
## 4 setosa large Sepal.Length 4.6
## 5 setosa large Sepal.Length 5.0
## 6 setosa large Sepal.Length 5.4
# plot several variables on one axis y-axis, each should have its own colour
# customise the look of the plot
p <- ggplot(new.iris, aes(x = Species, y = value))
p <- p + theme_bw()
p <- p + stat_summary(aes(colour = variable),
fun.data = "mean_sdl", mult = 1,
position = position_dodge(width = 0.75))
p <- p + labs(y = "Centimeters [cm]")
# example on how to change text elements
p <- p + theme(axis.title.y = element_text(size = rel(2),
colour = "blue",
face = "bold"))
# remove the grid lines
p <- p + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
p
# select a subgroup of parameters to be plotted
p <- ggplot(new.iris[new.iris$variable == "Sepal.Length" | new.iris$variable == "Sepal.Width", ],
aes(x = Species, y = value))
p <- p + stat_summary(aes(colour = variable),
fun.data = "mean_sdl", mult = 1,
position = position_dodge(width = 0.75))
p <- p + labs(y = "My stuff")
p <- p + theme(axis.title.y = element_text(size = rel(2),
colour = "blue",
face = "bold"))
p