ggplot2: qplot()


Dữ liệu

library(ggplot2)
head(diamonds)
##   carat       cut color clarity depth table price   x   y   z
## 1 0.23 Ideal E SI2 62 55 326 4.0 4.0 2.4
## 2 0.21 Premium E SI1 60 61 326 3.9 3.8 2.3
## 3 0.23 Good E VS1 57 65 327 4.0 4.1 2.3
## 4 0.29 Premium I VS2 62 58 334 4.2 4.2 2.6
## 5 0.31 Good J SI2 63 58 335 4.3 4.3 2.8
## 6 0.24 Very Good J VVS2 63 57 336 3.9 4.0 2.5
set.seed(1410)
dsmall <- diamonds[sample(nrow(diamonds), 100), ]

Lệnh cơ bản

# Basic use
qplot(carat, price, data = diamonds)

plot of chunk basic use

qplot(log(carat), log(price), data = diamonds)

plot of chunk basic use

qplot(carat, x * y * z, data = diamonds)

plot of chunk basic use

Các thuộc tính thẩm mỹ

# Color and shape
qplot(carat, price, data = dsmall, colour = color)

plot of chunk other aesthetic

qplot(carat, price, data = dsmall, shape = cut)

plot of chunk other aesthetic

# Alpha aesthetic
qplot(carat, price, data = diamonds, alpha = I(1/10))

plot of chunk other aesthetic

qplot(carat, price, data = diamonds, alpha = I(1/100))

plot of chunk other aesthetic

qplot(carat, price, data = diamonds, alpha = I(1/200))

plot of chunk other aesthetic

# Add smoother
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

plot of chunk other aesthetic

qplot(carat, price, data = diamonds, geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.

plot of chunk other aesthetic

Các đối tượng hình học

Thêm smoother

# method = "loess", default for n < 1000
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
span = 0.2)
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

plot of chunk smoother

qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
span = 1)
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

plot of chunk smoother

# method = "gam", default for n > 1000
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "gam", formula = y ~ s(x))

plot of chunk smoother

qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "gam", formula = y ~ s(x, bs = "cs"))

plot of chunk smoother

# method = "lm"
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm")

plot of chunk smoother

library(splines)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm", formula = y ~ ns(x,5))

plot of chunk smoother

# method = "rlm"
library(MASS)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "rlm")

plot of chunk smoother

Boxplots và jittered points

# Basic command
qplot(color, price / carat, data = diamonds, geom = "jitter")

plot of chunk boxplot and jitter

qplot(color, price / carat, data = diamonds, geom = "boxplot")

plot of chunk boxplot and jitter

# More colorful?
qplot(color, price / carat, data = diamonds, geom = "jitter",
color = color)

plot of chunk boxplot and jitter

qplot(color, price / carat, data = diamonds, geom = "boxplot",
fill = color)

plot of chunk boxplot and jitter

# Add semi-transparent
qplot(color, price / carat, data = diamonds, geom = "jitter",
alpha = I(1 / 5))

plot of chunk boxplot and jitter

qplot(color, price / carat, data = diamonds, geom = "jitter", 
alpha = I(1 / 50))

plot of chunk boxplot and jitter

qplot(color, price / carat, data = diamonds, geom = "jitter", 
alpha = I(1 / 200))

plot of chunk boxplot and jitter

Hoành đồ và mật độ

# basic
qplot(carat, data = diamonds, geom = "histogram")
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

plot of chunk histo & densi

qplot(carat, data = diamonds, geom = "density")

plot of chunk histo & densi

# add binwidth & xlim
qplot(carat, data = diamonds, geom = "histogram", binwidth = 1,
xlim = c(0,3))

plot of chunk histo & densi

qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1,
xlim = c(0,3))

plot of chunk histo & densi

qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01,
xlim = c(0,3))
## Warning: position_stack requires constant width: output may be incorrect

plot of chunk histo & densi

# more colorful?
qplot(carat, data = diamonds, geom = "density", colour = color)

plot of chunk histo & densi

qplot(carat, data = diamonds, geom = "histogram", fill = color)
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

plot of chunk histo & densi

Biểu đồ cột

# simple version
qplot(color, data = diamonds, geom = "bar")

plot of chunk bar chart

# weighted version (by carat)
qplot(color, data = diamonds, geom = "bar", weight = carat) +
scale_y_continuous("carat")

plot of chunk bar chart

# more colorful?
qplot(color, data = diamonds, geom = "bar", fill = color)

plot of chunk bar chart

Chuỗi thời gian

# with line
qplot(date, unemploy / pop, data = economics, geom = "line")

plot of chunk time series

qplot(date, uempmed, data = economics, geom = "line")

plot of chunk time series

# with path
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = economics,
geom = c("point", "path"))

plot of chunk time series

qplot(unemploy / pop, uempmed, data = economics,
geom = "path", color = year(date)) + scale_size_area()

plot of chunk time series

Faceting

# facets
qplot(carat, data = diamonds, facets = color ~ .,
geom = "histogram", binwidth = 0.1, xlim = c(0, 3))

plot of chunk faceting

# with ..density..
qplot(carat, ..density.., data = diamonds, facets = color ~ .,
geom = "histogram", binwidth = 0.1, xlim = c(0, 3))

plot of chunk faceting

Các tùy chọn khác

qplot(
carat, price, data = dsmall,
xlab = "Price ($)", ylab = "Weight (carats)",
main = "Price-weight relationship"
)

plot of chunk other opts

qplot(
carat, price/carat, data = dsmall,
ylab = expression(frac(price,carat)),
xlab = "Weight (carats)",
main="Small diamonds",
xlim = c(.2,1)
)
## Warning: Removed 35 rows containing missing values (geom_point).

plot of chunk other opts

qplot(carat, price, data = dsmall, log = "xy")

plot of chunk other opts