R语言可视化PPT第十二章实战

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point() + stat_smooth()
ggplot(quakes, aes(x=depth)) + geom_bar() ggplot(quakes, aes(x=depth)) +
old.par <- par(mfrow=c(1, 2)) plot(faithful, main="Faithful eruptions") plot(large.islands, main="Islands", ylab="Area") par(old.par)
ggplot2 实战
本小节我们使用ggolot2来进行一个实战展示。我们使用同样的数据集faithful, 首先我们先来绘制基本图形。使用Geoms 和 Stats,来定义数据如何使用,以及 把数据映射到plot函数里面
bwplot(factor(score) ~ gcsescore | gender, Chem97)
bwplot(gcsescore ~ gender | factor(score), Chem97, layout = c(6, 1))
ggplot(longley, aes(x=Year, y=Employed)) + geom_point()
ggplot(longley, aes(x=Year, y=Employed)) + geom_point() + stat_smooth()
ggplot(longley, aes(x=Year, y=Employed)) + geom_point() + stat_smooth(method="lm")
plot(LakeHuron, type="l", main='type="l"')
x <- seq(0.5, 1.5, 0.25) y <- rep(1, length(x)) plot(x, y, type="n") points(x, y)
with(mtcars, plot(mpg, disp))
第十二章实战
本章我们将通过3个具体的实战例子来综合展示如何使用R语言来进行数据的可视 化。第一个例子主要使用了基本库函数,第二个例子主要使用了ggplot2函数,第 三个例子主要使用了lattice函数。也就是本书主要讨论的3个图形库。
基本库实战
plot(faithful)
short.eruptions <- with(faithful, faithful[eruptions < 3, ])
densityplot(~ gcsescore | factor(score), Chem97, groups = gender, plot.points = FALSE, auto.key = TRUE)
qq(gender ~ gcsescore | factor(score), Chem97, f.value = ppoints(100), type = c("p", "g"), aspect = 1)
ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point()
ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point() +
stat_smooth(method="lm") + facet_grid(~cyl)
lattice 实战
数据集记录化学考试的学生信息。我们只对以下变量感兴趣 Score分数: 学生的总体分数 Gcse分数:学生的GCSE分数 gender: 学生的性别7)
histogram(~ gcsescore | factor(score), data = Chem97)
这一小节主要展示lattice的综合使用,我们使用来自mlmRev的 Chem97数据集。
data(Chem97, package = "mlmRev") head(Chem97)
lea school student score gender age gcsescore gcsecnt 1 1 1 1 4 F 3 6.625 0.3393157 2 1 1 2 10 F -3 7.625 1.3393157 3 1 1 3 10 F -4 7.250 0.9643157 4 1 1 4 10 F -2 7.500 1.2143157 5 1 1 5 8 F -1 6.444 0.1583157 6 1 1 6 10 F 4 7.750 1.4643157
plot(faithful) points(short.eruptions, col="red", pch=19)
head(colors(), 10) fit <- lm(waiting~eruptions, data=faithful) plot(faithful) lines(faithful$eruptions, fitted(fit), col="blue") abline(v=3, col="purple") abline(h=mean(faithful$waiting)) abline(a=coef(fit)[1], b=coef(fit)[2]) abline(fit, col = "red")
geom_bar(binwidth=50)
quakes.agg <- aggregate(mag ~ round(depth, 1), data=quakes, FUN=length)
names(quakes.agg) <- c("depth", "mag")
ggplot(quakes.agg, aes(x=depth, y=mag)) + geom_bar(stat="identity")
plot(faithful, main = "Eruptions of Old Faithful", xlab = "Eruption time (min)", ylab = "Waiting time to next eruption (min)")
x <- seq(0.5, 1.5, 0.25) y <- rep(1, length(x)) plot(x, y, main="Effect of cex on text size") text(x, y+0.1, labels=x, cex=x)
ggplot(quakes, aes(x=long, y=lat)) + geom_point()
ggplot(longley, aes(x=Year, y=Unemployed)) + geom_line()
ggplot(quakes, aes(x=depth)) + geom_bar(binwidth=50) ggplot(quakes, aes(x=depth)) + stat_bin(binwidth=50)
相关文档
最新文档