Saturday, July 26, 2014

Installing the package

Generally, install.packages("ggplot2") should do the trick.

Alpha and omega

bmiData revisited

library(ggplot2)
bmi.file <- "http://bit.ly/bmidata"
bmiData <- read.table(bmi.file, header = TRUE)

ggplot(bmiData, aes(x = weight, y = height, color = gender)) +
  geom_point(size = 3, alpha = 0.5) +
  geom_smooth(method = "lm", se = FALSE) +
  theme_bw()
# ggsave("test.pdf", width = 7, height = 4)

bmiData revisited

plot of chunk unnamed-chunk-2

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  theme_bw() +
  geom_point(alpha = 0.5)

plot of chunk unnamed-chunk-4

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, size = Petal.Width)) +
  theme_bw() +
  geom_point(alpha = 0.5)

plot of chunk unnamed-chunk-6

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, 
                 color = Species, size = Petal.Width)) +
  theme_bw() +
  ylab("Length of petals") +
  xlab("Length of sepals") +
  geom_point(alpha = 0.5) + 
  facet_wrap(~ Species)

plot of chunk unnamed-chunk-8

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, 
                 color = Species, size = Petal.Width)) +
  theme_bw() +
  ylab("Length of petals") +
  xlab("Length of sepals") +
  geom_smooth(method = "loess", se = FALSE, size = 2) +
  geom_point(alpha = 0.5)

plot of chunk unnamed-chunk-10

ggplot(Orange, aes(x = age, y = circumference)) +
  theme_bw() +
  geom_point() +
  facet_wrap(~ Tree)

plot of chunk unnamed-chunk-12

But Tree is not ordered from 1 to 5. How would one 'fix' this? Hint: how to reorder factor ggplot2 r

Orange$Tree <- factor(Orange$Tree, levels = unique(as.character(Orange$Tree)))

ggplot(Orange, aes(x = age, y = circumference)) +
  theme_bw() +
  geom_point() +
  facet_wrap(~ Tree)

Orange$Tree <- factor(Orange$Tree, levels = unique(as.character(Orange$Tree)))

ggplot(Orange, aes(x = age, y = circumference)) +
  theme_bw() +
  geom_point() +
  facet_wrap(~ Tree)

plot of chunk unnamed-chunk-14

ggplot(Orange, aes(x = age, y = circumference)) +
  theme_bw() +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(~ Tree)

plot of chunk unnamed-chunk-16

red <- rnorm(30, 10, 10)
green <- rnorm(100, 52, 8)
blue <- rnorm(182, 3, 21)
xy <- data.frame(values = c(red, green, blue))
xy$three.cols <- rep(c("red", "green", "blue"), times = c(30, 100, 182))

rm(red, green, blue)

ggplot(xy, aes(x = three.cols, y = values)) + 
  theme_bw() +
  geom_point()

plot of chunk unnamed-chunk-18

Our options are:

ggplot(xy, aes(x = three.cols, y = values, fill = three.cols)) + 
  theme_bw() +
  geom_boxplot()

plot of chunk unnamed-chunk-19

ggplot(xy, aes(x = three.cols, y = values)) + 
  theme_bw() +
  geom_boxplot() +
  geom_jitter(position = position_jitter(width = 0.25), alpha = 0.25)

plot of chunk unnamed-chunk-20

xy.mean <- aggregate(values ~ three.cols, data = xy, FUN = mean)
xy.sd <- aggregate(values ~ three.cols, data = xy, FUN = sd)
xy.n <- aggregate(values ~ three.cols, data = xy, FUN = length)
xy.se <- xy.sd[, -1]/sqrt(xy.n[, -1])
xy.stat <- xy.mean
xy.stat$se <- xy.se

names(xy.stat) <- c("three.cols", "mean", "se")

ggplot(xy.stat, aes(x = three.cols, y = mean)) +
  theme_bw() +
  geom_point() +
  geom_errorbar(aes(ymin = mean - 1.96 * se, ymax = mean + 1.96 * se))

plot of chunk unnamed-chunk-22

ggplot(xy.stat, aes(x = three.cols, y = mean)) +
  theme_bw() +
  geom_pointrange(aes(ymin = mean - 1.96 * se, ymax = mean + 1.96 * se))

plot of chunk unnamed-chunk-23

head(diamonds[, c("clarity", "cut")])

require(RColorBrewer)
display.brewer.all()

ggplot(diamonds, aes(clarity, fill = cut)) + 
  theme_bw() +
  scale_fill_brewer(palette = "Dark2") +
  geom_bar(position = "fill")

## Loading required package: RColorBrewer

plot of chunk unnamed-chunk-25

plot of chunk unnamed-chunk-26

plot of chunk unnamed-chunk-27

day.span <- seq(from = as.Date("26.7.2014", format = "%d.%m.%Y"), 
                to = as.Date("26.8.2014", format = "%d.%m.%Y"), length.out = 30)

per.day <- as.numeric(diff(range(day.span)))
many.days <- rep(day.span, each = per.day)

set.seed(357)
daily.mean <- sample(1:59, 30, replace= TRUE)

my.function <- function(bigX) {
  rnorm(n = per.day, mean = bigX, 
        sd = rnorm(1, mean = 10, sd = 5))
  }
daily.data <- sapply(daily.mean, FUN = my.function, simplify = FALSE)
daily.data <- do.call("c", daily.data)
final.data <- data.frame(day = many.days, value = daily.data)

ggplot(final.data, aes(x = day, y = value)) +
  theme_bw() +
  geom_boxplot(aes(group = day)) +
  geom_jitter(alpha = 0.1) +
  geom_smooth(method = loess, span = 0.5)