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).
# Explicitly add the default scalep + scale_colour_hue()
# Adjust parameters of the default, here changing the appearance # of the legendp + scale_colour_hue("What does\nit eat?", breaks = c("herbi", "carni", "omni", NA), labels = c("plants", "meat", "both", "don't know"))
# Use a different scalep + scale_colour_brewer(palette = "Set1")
p <- qplot(cty, hwy, data = mpg, colour = displ)
p
p + scale_x_continuous("City mpg")
p + xlab("City mpg")
p + ylab("Highway mpg")
p + labs(x = "City mpg", y = "Highway", colour = "Displacement")
p + xlab(expression(frac(miles, gallon)))
p <- qplot(cyl, wt, data = mtcars)p
p + scale_x_continuous(breaks = c(4, 8))
p + scale_x_continuous(limits = c(4, 8))
p <- qplot(wt, cyl, data = mtcars, colour = cyl)p
p + scale_colour_gradient(breaks = c(5.5, 6.5))
p + scale_colour_gradient(limits = c(5.5, 6.5))
# Khác nhau giữa log scale và log data: tên các trục khác nhauqplot(log10(carat), log10(price), data = diamonds)
qplot(carat, price, data = diamonds) + scale_x_log10() + scale_y_log10()
plot <- qplot(date, psavert, data = economics, geom = "line") + ylab("Personal savings rate") + geom_hline(xintercept = 0, colour = "grey50")plot
# Cần thêm thư viện 'scales'library(scales)plot + scale_x_date(breaks = date_breaks("10 years"))
plot + scale_x_date( limits = as.Date(c("2004-01-01", "2005-01-01")), labels = date_format("%Y-%m-%d"))
# gradient và gradient2f2d <- 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))
erupt + scale_fill_gradient(limits = c(0, 0.04), low = "white", high = "black")
erupt + scale_fill_gradient2(limits = c(-0.04, 0.04), midpoint = mean(df$density))
# gradientnlibrary(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)
erupt + fill_gradn(diverge_hcl)
erupt + fill_gradn(heat_hcl)
# 3 ColorBrewer palettespoint <- 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).
point + scale_colour_brewer(palette = "Set2")
point + scale_colour_brewer(palette = "Pastel1")
area + scale_fill_brewer(palette = "Set1")
area + scale_fill_brewer(palette = "Set2")
area + scale_fill_brewer(palette = "Pastel1")
# Màu tùy chỉnhplot <- qplot(brainwt, bodywt, data = msleep, log = "xy")plot + aes(colour = vore) + scale_colour_manual(values = c("red", "orange", "yellow", "green", "blue"))
colours <- c(carni = "red", "NA" = "orange", insecti = "yellow", herbi = "green", omni = "blue")plot + aes(colour = vore) + scale_colour_manual(values = colours)
# Hình dạng tùy chỉnhplot + aes(shape = vore) + scale_shape_manual(values = c(1, 2, 6, 0, 23))
## Warning: Removed 32 rows containing missing values (geom_point).
# Đa màu sắchuron <- 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
ggplot(huron, aes(year)) + geom_line(aes(y = level - 5, colour = "below")) + geom_line(aes(y = level + 5, colour = "above"))
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"))
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()
# Legends produced by geom: point, line, point and line, and bar.p <- ggplot(diamonds[1:100, ], aes(price, carat, colour = cut))p + geom_point()
p + geom_line()
p + geom_point() + geom_line()
p + geom_bar(binwidth = 100) + aes(fill = cut, y = ..count..)
# Colour legend, shape legend, colour + shape legend.p <- ggplot(diamonds[1:100, ], aes(price, carat)) + geom_point()p + aes(colour = cut)
p + aes(shape = cut)
p + aes(shape = cut, colour = cut)
[trang chủ] [liên hệ] [code]