/man/translate_qplot_ggplot.Rd
Unknown | 86 lines | 72 code | 14 blank | 0 comment | 0 complexity | 8feeb91c8e5d9077dc5a37ed1808af46 MD5 | raw file
Possible License(s): GPL-2.0
1% Generated by roxygen2: do not edit by hand 2% Please edit documentation in R/translate-qplot-ggplot.r 3\name{translate_qplot_ggplot} 4\alias{translate_qplot_ggplot} 5\title{Translating between qplot and ggplot} 6\description{ 7Within ggplot2, there are two basic methods to create plots, with qplot() 8and ggplot(). qplot() is designed primarily for interactive use: it makes 9a number of assumptions that speed most cases, but when designing multilayered 10plots with different data sources it can get in the way. This section 11describes what those defaults are, and how they map to the fuller ggplot() 12syntax. 13} 14\examples{ 15 16# By default, qplot() assumes that you want a scatterplot, 17# i.e., you want to use geom_point() 18# qplot(x, y, data = data) 19# ggplot(data, aes(x, y)) + geom_point() 20 21# Using Aesthetics 22 23# If you map additional aesthetics, these will be added to the defaults. With 24# qplot() there is no way to use different aesthetic mappings (or data) in 25# different layers 26# qplot(x, y, data = data, shape = shape, colour = colour) 27# ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point() 28# 29# Aesthetic parameters in qplot() always try to map the aesthetic to a 30# variable. If the argument is not a variable but a value, effectively a new column 31# is added to the original dataset with that value. To set an aesthetic to a 32# value and override the default appearance, you surround the value with I() in 33# qplot(), or pass it as a parameter to the layer. 34# qplot(x, y, data = data, colour = I("red")) 35# ggplot(data, aes(x, y)) + geom_point(colour = "red") 36 37# Changing the geom parameter changes the geom added to the plot 38# qplot(x, y, data = data, geom = "line") 39# ggplot(data, aes(x, y)) + geom_line() 40 41# Not all geoms require both x and y, e.g., geom_bar() and geom_histogram(). 42# For these two geoms, if the y aesthetic is not supplied, both qplot and 43# ggplot commands default to "count" on the y-axis 44# ggplot(data, aes(x)) + geom_bar() 45# qplot(x, data = data, geom = "bar") 46 47# If a vector of multiple geom names is supplied to the geom argument, each 48# geom will be added in turn 49# qplot(x, y, data = data, geom = c("point", "smooth")) 50# ggplot(data, aes(x, y)) + geom_point() + geom_smooth() 51 52# Unlike the rest of ggplot2, stats and geoms are independent 53# qplot(x, y, data = data, stat = "bin") 54# ggplot(data, aes(x, y)) + geom_point(stat = "bin") 55# 56# Any layer parameters will be passed on to all layers. Most layers will ignore 57# parameters that they don't need 58# qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm") 59# ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm") 60 61# Scales and axes 62 63# You can control basic properties of the x and y scales with the xlim, ylim, 64# xlab and ylab arguments 65# qplot(x, y, data = data, xlim = c(1, 5), xlab = "my label") 66# ggplot(data, aes(x, y)) + geom_point() + 67# scale_x_continuous("my label", limits = c(1, 5)) 68 69# qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20)) 70# ggplot(data, aes(x, y)) + geom_point() + 71# scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20)) 72 73# Like plot(), qplot() has a convenient way of log transforming the axes. 74# qplot(x, y, data = data, log = "xy") 75# ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10() 76# There are many other possible transformations, but not all are 77# accessible from within qplot(), see ?scale_continuous for more 78 79# Plot options 80 81# qplot() recognises the same options as plot does, and converts them to their 82# ggplot2 equivalents. See ?theme for more on ggplot options 83# qplot(x, y, data = data, main="title", asp = 1) 84# ggplot(data, aes(x, y)) + geom_point() + labs(title = "title") + theme(aspect.ratio = 1) 85} 86