ggplot2:不同语法间的转换

ggplot2:不同语法间的转换

本文介绍了ggplot2语法和其它绘图语法之间的转换。

在qplot和ggplot间转换

图形属性

1
2
qplot(x, y, data = data, shape = shape, colour = colour)
ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point()
1
2
qplot(x, y, data = data, colour = I("red")
ggplot(data, aes(x, y)) + geom_point(colour = "red")

图层

1
2
qplot(x, y, data = data, geom = "line")
ggplot(data, aes(x, y)) + geom_line()
1
2
qplot(x, y, data = data, geom = c("point", "smooth"))
ggplot(data, aes(x, y)) + geom_point() + geom_smooth()
1
2
qplot(x, y, data = data, stat = "bin")
ggplot(data, aes(x, y)) + geom_point(stat = "bin")

任何图层的参数都会传递给所有图层,大部分图层都会忽略对其无用的参数:

1
2
qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm")
ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm")

标度和坐标轴

1
2
qplot(x, y, data = data, xlim = c(1, 5),  xlab = "my label")
ggplot(data, aes(x, y)) + geom_point() + scale_x_continuous("my label", limits = c(1, 5))
1
2
qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20))
ggplot(data, aes(x, y)) + scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20))
1
2
qplot(x, y, data = data, log = "xy")
ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10()

绘图选项

1
2
qplot(x, y, data = data, main = "title", asp = 1)
ggplot(data, aes(x, y)) + geom_point() + labs(title = "title") + coord_fixed(ratio = 1)

基础图形系统

基础图形系统有两类绘图函数,一种是绘制完整的图形,即高级绘图,另一种是在现有的图形上添加元素,即低级绘图;

高级绘图

1
2
plot(x, y); dotchart(x, y); stripchart(x, y)
qplot(x, y)
1
2
plot(x, y, type = "l")
qplot(x, y) + geom_line()
1
2
plot(x, y, type = "s")
qplot(x, y, geom = "step")
1
2
plot(x, y, type = "b")
qplot(x, y, geom = c("point", "line"))
1
2
boxplot(x, y)
qplot(x, y, geom = "boxplot")
1
2
hist(x)
qplot(x, geom = "histogram")
1
2
cdplot(x, y)
qplot(x, fill = y, geom = "density", position = "fill")
1
2
coplot(y ~ x | a + b)
qplot(x, y, facets = a ~ b)

但是需要注意的是,这些几何对象的参数和基础图形是不同的,例如,hist()函数的参数是箱子的个数,而geom_histogram()的参数是组距:

1
2
hist(x, bins = 100)
qplot(x, geom = "histogram", binwidth = 1)

barplot()适用于汇总后的数据,而geom_bar()适用于没有汇总的数据。

1
2
barplot(table(x))
qplot(x, geom = "bar")

1
2
barplot(x)
qplot(names(x), x, geom = "bar", stat = "identity")

geom_tile()和geom_contour()要求数据框形式的参数,而image()和contour()则需要矩阵形式的参数。

1
2
image(x)
qplot(X1, X2, data = melt(x), geom = "tile", fill = value)

1
2
contour(x)
qplot(X1, X2, data = melt(x), geom = "contour", fill = value)

通常情况下,基础绘图函数使用独立的向量,而不像ggplot2那样使用整合好的数据框。当数据未被指定时,qplot()会尝试构建一个数据框,但不能保证总能实现。

1
2
with(df, plot(x, y))
qplot(x, y, data = df)

默认时,qplot()将以某一标度将数值映射到图形属性上,若需要自行设置图形属性并覆盖默认值时,需要使用I():

1
2
plot(x, y)
qplot(x, y colour = I("red"), size = I(1))

低级绘图

基础图形中的低级绘图函数和ggplot2()中图册的对比:

基础函数 ggplot2中的图层
curve() geom_curve()
hline() geom_hline()
lines() geom_line()
points() geom_point()
polygon() geom_polygon()
rect() geom_rect()
rug() geom_rug()
segment() geom_segment()
text() geom_text()
vline() geom_vline()
abline(lm(y ~ x)) geom_smooth(method = “lm”)
lines(density(x)) geom_density()
lines(loess(x, y)) geom_smooth()

调色板

1
2
palette(rainbow(5))
plot(1:5, 1:5, col = 1:5, pch = 19, cex = 4)


1
qplot(1:5, 1:5, col = factor(1:5), size = I(4)) + scale_colour_manual(values = rainbow(5))

在ggplot2中,你还可以使用连续色彩的调色板,其中的颜色是由线性插值得到的。

1
qplot(0:100, 0:100, col = 0:100, size = I(4)) + scale_colour_gradientn(colours = rainbow(7))


1
last_plot() + scale_colour_gradientn(colours = terrain.colors(7))

lattice图形设备

lattice和ggplot2的主要区别是其图形公式是基于公式的。而ggplot2并非如此:

1
2
3
library(ggplot2movies)
library(lattice)
xyplot(rating ~ year, data = movies)


1
qplot(year, rating, data = movies, colour = I("lightblue"), alpha = I(1/5))


1
xyplot(rating ~ year | Comedy + Action, data = movies)


1
qplot(year, rating, data = movies, facets = Comedy ~ Action)


1
2
stripplot(~ rating, data = movies, jitter.data = T)
qplot(rating, 1, data = movies, geom = "jitter")



1
2
histogram(~ rating, data = movies)
qplot(rating, data = movies, geom = "histogram")



1
2
bwplot(Comedy ~ rating, data = movies)
qplot(factor(Comedy), rating, data = movies, geom = "boxplot")



1
2
xyplot(wt ~ mpg, mtcars, type = c("p", "smooth"))
qplot(mpg, wt, data = mtcars, geom = c("point", "smooth"))



1
2
xyplot(wt ~ mpg, mtcars, type = c("p", "r"))
qplot(mpg, wt, data = mtcars, geom = c("point", "smooth"), method = "lm")


ggplot2和lattice的标度处理方式相似:

1
2
xyplot(wt ~ mpg | cyl, mtcars, scale = list(y = list(relation = "free")))
qplot(mpg, wt, data = mtcars) + facet_wrap(~cyl, scales = "free")



1
2
3
4
5
xyplot(wt ~ mpg | cyl, mtcars, scales = list(log = 10))
qplot(mpg, wt, data = mtcars, log = "xy") + facet_wrap(~cyl)

xyplot(wt ~ mpg | cyl, mtcars, scales = list(log = 2))
qplot(mpg, wt, data = mtcars) + scale_x_continuous(trans = "log2") + scale_y_continuous(trans = "log2") + facet_wrap(~cyl)

1
2
xyplot(wt ~ mpg, mtcars, group = cyl, auto.key = T)
qplot(mpg, wt, data = mtcars, colour = factor(cyl))



1
2
xyplot(wt ~ mpg, mtcars, xlim = c(20, 30))
qplot(mpg, wt, data = mtcars, xlim = c(20, 30))

lattice和ggplot2中都有类似的选项来控制图形中的标签:

1
2
3
4
5
6
xyplot(wt ~ mpg, mtcars, xlab = "Miles per gallon",
ylab = "Weight",
main = "Weight-efficiency tradeoff")
qplot(mpg, wt, data = mtcars, xlab = "Miles per gallon",
ylab = "Weight",
main = "Weight-efficiency tradeoff")

1
2
xyplot(wt ~ mpg, mtcars, aspect = 1)
qplot(mpg, wt, data = mtcars, asp = 1)
# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×