ggplot2: Scales


Sử dụng scales

library(ggplot2)
plot <- qplot(cty, hwy, data = mpg)
## Error: ggplot2 doesn't know how to deal with data of class numeric
plot
## standardGeneric for "plot" defined from package "graphics"
##
## function (x, y, ...)
## standardGeneric("plot")
## <environment: 0x46d7a88>
## Methods may be defined for arguments: x, y
## Use showMethods("plot") for currently available ones.
plot + aes(x = drv)
## Error in plot + aes(x = drv): non-numeric argument to binary operator
p <- qplot(sleep_total, sleep_cycle, data = msleep, colour = vore)
p
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk modify scale

# Explicitly add the default scale
p + scale_colour_hue()
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk modify scale

# Adjust parameters of the default, here changing the appearance 
# of the legend
p + scale_colour_hue("What does\nit eat?",
breaks = c("herbi", "carni", "omni", NA),
labels = c("plants", "meat", "both", "don't know"))
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk modify scale

# Use a different scale
p + scale_colour_brewer(palette = "Set1")
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk modify scale

Các chi tiết

Tên

p <- qplot(cty, hwy, data = mpg, colour = displ)
## Error: ggplot2 doesn't know how to deal with data of class numeric
p
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk cm

p + scale_x_continuous("City mpg")
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk cm

p + xlab("City mpg")
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk cm

p + ylab("Highway mpg")
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk cm

p + labs(x = "City mpg", y = "Highway", colour = "Displacement")
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk cm

p + xlab(expression(frac(miles, gallon)))
## Warning: Removed 51 rows containing missing values (geom_point).

plot of chunk cm

breaks và limits

p <- qplot(cyl, wt, data = mtcars)
p

plot of chunk b&l

p + scale_x_continuous(breaks = c(4, 8))

plot of chunk b&l

p + scale_x_continuous(limits = c(4, 8))

plot of chunk b&l

p <- qplot(wt, cyl, data = mtcars, colour = cyl)
p

plot of chunk b&l

p + scale_colour_gradient(breaks = c(5.5, 6.5))

plot of chunk b&l

p + scale_colour_gradient(limits = c(5.5, 6.5))

plot of chunk b&l

Chuyển đổi

# Khác nhau giữa log scale và log data: tên các trục khác nhau
qplot(log10(carat), log10(price), data = diamonds)

plot of chunk scale x

qplot(carat, price, data = diamonds) + 
scale_x_log10() + scale_y_log10()

plot of chunk scale x

Thời gian và ngày tháng

plot <- qplot(date, psavert, data = economics, geom = "line") + 
ylab("Personal savings rate") +
geom_hline(xintercept = 0, colour = "grey50")
plot

plot of chunk date time

# Cần thêm thư viện 'scales'
library(scales)
plot + scale_x_date(breaks = date_breaks("10 years"))

plot of chunk date time

plot + scale_x_date(
limits = as.Date(c("2004-01-01", "2005-01-01")),
labels = date_format("%Y-%m-%d")
)

plot of chunk date time

Màu sắc

# gradient và gradient2
f2d <- with(faithful, MASS::kde2d(eruptions, waiting,
h = c(1, 10), n = 50))
df <- with(f2d, cbind(expand.grid(x, y), as.vector(z)))
names(df) <- c("eruptions", "waiting", "density")
erupt <- ggplot(df, aes(waiting, eruptions, fill = density)) +
geom_tile() +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))
erupt + scale_fill_gradient(limits = c(0, 0.04))

plot of chunk gradient

erupt + scale_fill_gradient(limits = c(0, 0.04), 
low = "white", high = "black")

plot of chunk gradient

erupt + scale_fill_gradient2(limits = c(-0.04, 0.04), 
midpoint = mean(df$density))

plot of chunk gradient

# gradientn
library(vcd)
## Error in library(vcd): there is no package called 'vcd'
fill_gradn <- function(pal) {
scale_fill_gradientn(colours = pal(7), limits = c(0, 0.04))
}
# Tải thêm thư viện `colorspace`
library(colorspace)
erupt + fill_gradn(rainbow_hcl)

plot of chunk gradient

erupt + fill_gradn(diverge_hcl)

plot of chunk gradient

erupt + fill_gradn(heat_hcl)

plot of chunk gradient

# 3 ColorBrewer palettes
point <- qplot(brainwt, bodywt, data = msleep, log = "xy",
colour = vore)
area <- qplot(log10(brainwt), data = msleep, fill = vore,
binwidth = 1)
# Chú ý 'pal=' không làm việc, phải dùng 'palette=', không hiểu
# tại sao.
point + scale_colour_brewer(palette = "Set1")
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk gradient

point + scale_colour_brewer(palette = "Set2")
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk gradient

point + scale_colour_brewer(palette = "Pastel1")
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk gradient

area + scale_fill_brewer(palette = "Set1")

plot of chunk gradient

area + scale_fill_brewer(palette = "Set2")

plot of chunk gradient

area + scale_fill_brewer(palette = "Pastel1")

plot of chunk gradient

Tùy chỉnh

# Màu tùy chỉnh
plot <- qplot(brainwt, bodywt, data = msleep, log = "xy")
plot + aes(colour = vore) +
scale_colour_manual(values = c("red", "orange", "yellow",
"green", "blue"))
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk manual

colours <- c(carni = "red", "NA" = "orange", insecti = "yellow", 
herbi = "green", omni = "blue")
plot + aes(colour = vore) + scale_colour_manual(values = colours)
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk manual

# Hình dạng tùy chỉnh
plot + aes(shape = vore) +
scale_shape_manual(values = c(1, 2, 6, 0, 23))
## Warning: Removed 32 rows containing missing values (geom_point).

plot of chunk manual

# Đa màu sắc
huron <- data.frame(year = 1875:1972, level = LakeHuron)
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5), colour = "blue") +
geom_line(aes(y = level + 5), colour = "red")
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous

plot of chunk manual

ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above"))
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous

plot of chunk manual

ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above")) +
scale_colour_manual("Direction",
values = c("below" = "blue", "above" = "red"))
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous

plot of chunk manual

Scale đồng nhất

x <- colors()
luv <- as.data.frame(convertColor(t(col2rgb(x)), "sRGB", "Luv"))
qplot(u, v, data=luv, colour = x, size = I(3)) + scale_colour_identity() +
coord_equal()

plot of chunk identity

Chú giải

# Legends produced by geom: point, line, point and line, and bar.
p <- ggplot(diamonds[1:100, ], aes(price, carat, colour = cut))
p + geom_point()

plot of chunk legend

p + geom_line()

plot of chunk legend

p + geom_point() + geom_line()

plot of chunk legend

p + geom_bar(binwidth = 100) + aes(fill = cut, y = ..count..)

plot of chunk legend

# Colour legend, shape legend, colour + shape legend.
p <- ggplot(diamonds[1:100, ], aes(price, carat)) +
geom_point()
p + aes(colour = cut)

plot of chunk legend

p + aes(shape = cut)

plot of chunk legend

p + aes(shape = cut, colour = cut)

plot of chunk legend