Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutannihilation/3c2057849e2be19d533d to your computer and use it in GitHub Desktop.
Save yutannihilation/3c2057849e2be19d533d to your computer and use it in GitHub Desktop.
options of ggplot2: fixing range and colors
library(ggplot2)
library(animation)
library(dplyr)
set.seed(100)
data <- data.frame(value = rnorm(1000, mean = 5, sd = sqrt(5)))
### continuous-scale axis ###
# BAD EXAMPLE: Ranges are changing too fast...
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data, 1:i), aes(x = value)) + geom_bar()
print(p)
}
}, movie.name = "animation_bad1.gif", interval=c(rep(0.02, nrow(data) - 1), 5) )
# GOOD EXAMPLE: specify `limits` and `binwidth`
xlimits <- range(data)
xrangewidth <- diff(limits)
ylimits <- c(0,100)
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data, 1:i), aes(x = value)) + geom_bar(binwidth = xrangewidth/30) +
scale_x_continuous(limits = xlimits) +
scale_y_continuous(limits = ylimits)
print(p)
}
}, movie.name = "animation1.gif", interval=c(rep(0.02, nrow(data) - 1), 5) )
# But, when you use scale_*_log10, limit must be larger than 0, from the definition of logarithm.
ylimits_for_log <- c(1,100)
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data, 1:i), aes(x = value)) + geom_bar(binwidth = xrangewidth/30) +
scale_x_continuous(limits = xlimits) +
scale_y_log10(limits = ylimits_for_log)
print(p)
}
}, interval=c(rep(0.02, nrow(data) - 1), 5) )
#### discrete-scale axis ####
breaks <- floor(limits[1]):ceiling(limits[2])
labels <- sapply(1:(length(breaks)-1), function(i) paste(breaks[i], "~", breaks[i+1]))
data %>%
mutate(valuerange = cut(value,
breaks = breaks,
labels = labels)
) -> data2
ylimits <- c(0, max(table(data2$valuerange)))
# BAD EXAMPLE:
# you cannot use `scale_x_continuous()` since x is categorical value.
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data2, 1:i), aes(x = valuerange)) + geom_bar() +
scale_y_continuous(limits = ylimits)
print(p)
}
}, movie.name = "animation_bad2.gif", interval=c(rep(0.02, nrow(data) - 1), 5) )
# GOOD EXAMPLE: use `drop = FALSE`
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data2, 1:i), aes(x = valuerange)) + geom_bar() +
scale_x_discrete(drop = FALSE) +
scale_y_continuous(limits = ylimits)
print(p)
}
}, movie.name = "animation2.gif", interval=c(rep(0.02, nrow(data) - 1), 5) )
# You can specify order by `limits` option
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data2, 1:i), aes(x = valuerange)) + geom_bar() +
scale_x_discrete(drop = FALSE, limits = rev(labels)) +
scale_y_continuous(limits = ylimits)
print(p)
}
}, interval=c(rep(0.02, nrow(data) - 1), 5) )
#### colour and fill ####
# BAD EXAMPLE: color arrangement is changing like Chameleon...
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data2, 1:i), aes(x = valuerange, fill = valuerange)) + geom_bar() +
scale_x_discrete(drop = FALSE) +
scale_y_continuous(limits = ylimits) +
print(p)
}
}, movie.name = "animation_bad3.gif", interval=c(rep(0.02, nrow(data) - 1), 5) )
# GOOD EXAMPLE: use `scale_fill_discrete()`
# (For colour, use `scale_colour_discrete()` instead)
saveGIF({
for (i in 1:nrow(data)) {
p <- ggplot(slice(data2, 1:i), aes(x = valuerange, fill = valuerange)) + geom_bar() +
scale_x_discrete(drop = FALSE) +
scale_y_continuous(limits = ylimits) +
scale_fill_discrete(drop = FALSE)
print(p)
}
}, movie.name = "animation3.gif", interval=c(rep(0.02, nrow(data) - 1), 5) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment