前言
ggplot2是一个强大的数据可视化R包。它的作者是Hadley Wickham。gg
在ggplot2中的意思是Grammar of Graphics,作者的意思是使用规范的语言来描述图像。我们可以想象,当我们想要画一幅图时,我们的思维方式是先从总体上掌握自己想要画一个什么类型的图形,这个图形会由哪几部分组成,是否需要在图像上加载统计信息,而后是图形的细节,比如坐标轴,背景,图例等。ggplot2的思维正是这样,它将绘图分成了3个部分: Plot == data + Aesthetics + Geometry + …
- data 就是数据,它必须是一个data frame.
- Aesthetics 美学,通常指x,y坐标系,颜色,大小,点线面的形状细节等。
- Geometry 图形,它对应是图应该画成什么类型,是histogram, boxplot, lineplot, density plot, dot plot还是其它。
- … 其它,主要是指统计信息。
在ggplot2中,将这些一条条指示如何画图的语句集合起来的符号就是加号“+”。举个例子,如果想在图上画一个histogram,将会是这样一个样子:
这里是数据,x是数据中的A列,y是数据中的B列 + 坐标是2维直角坐标系 + 画成柱形图
这就是ggplot2的思维方式。
ggplot2提供两个主要的函数:
qplot()
, 快速画图, quick plotggplot()
, 使用语法画图。
我们为什么要学习ggplot2呢?我们已经有了R中很多的画图工具,比如graphics以及grid中的各种工具。但是它们就好比windows系统自带的画图板工具,简单,你可以使用它带出非常繁杂漂亮的图像,但是需要你有很深的功力。如果你只是想方便地实现漂亮的繁杂图像,那么美图秀秀可能是你想要的。虽然ggplot2并不是简单的可以用美国秀秀来类比的,但是可以很轻松地让你从素颜图飞越至精妆图这一点上是一致的。
为了激发您的学习兴趣,下面是一组ggplot2画的效果图。
本文的重点并不太于让大家从零开始入门与提高,本文的目的是让大家能够照猫画虎,毕竟图像只是数据显示的手段,并非目的。糗世界的观点是如何让数据简洁,美观,重点突出才是最上选的数据用图。本文对于在高影响力的杂志投稿将会有很大帮助。
快速入门
本小节让读者快速理解ggplot2,并且能够画出简单的图像。
## 安装ggplot2
install.packages("ggplot2")
## 加载ggplot2
library(ggplot2)
data(mtcars)
head(mtcars[, c("mpg", "wt")])
## mpg wt
## Mazda RX4 21.0 2.620
## Mazda RX4 Wag 21.0 2.875
## Datsun 710 22.8 2.320
## Hornet 4 Drive 21.4 3.215
## Hornet Sportabout 18.7 3.440
## Valiant 18.1 3.460
qplot(x=wt, y=mpg, data=mtcars)
在函数qplot中,data参数需要的是一个data.frame。x及y参数直接以data.frame的列名来指定数据,这很象with(mtcars, …)这种写法。
通过上面的步骤,我们就可以画出点图了。但是这和极简绘图还差得很远。比如它有讨厌的背景灰。在ggplot绘图体系中,可以很轻松的使用主题(theme)来改变图像的显示状态。我们使用极简主题试试。
qplot(x=wt, y=mpg, data=mtcars) + theme_minimal()
好的,现在我们想把图中的点画大一点。
qplot(x=wt, y=mpg, data=mtcars) + geom_point(size=5) + theme_minimal()
把点的颜色改变一下。
qplot(x=wt, y=mpg, data=mtcars) +
geom_point(size=5, pch=21, color="gray30", fill="cyan") +
theme_minimal()
再加上趋势线。
qplot(x=wt, y=mpg, data=mtcars) +
geom_point(size=5, pch=21, color="gray30", fill="cyan") +
geom_smooth(method=lm, se=TRUE, fullrange=TRUE, color="red", fill="gray80") +
theme_minimal()
从上面的步骤中,我们可以很清晰地看到ggplot2是如何一层一层地加上我们想要的效果的。理解了这一语法,接下来需要做的就是了解ggplot2画图过程。
ggplot2的绘图过程我将其分为三步,
- 数据处理,也就是所谓的统计方法,
- 数据转换,将数据转换成二维指定的坐标系,将映射至颜色,大小,线型等属性数据转换成属性值。
- 调整坐标,图标,背景等,绘图输出。
我们来看一个简单的例子。
data("ToothGrowth")
head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
## ToothGrowth中的dose和supp应该是离散的,所以我用factor来保存它们
ToothGrowth$dose <- factor(ToothGrowth$dose)
## 使用ToothGrowth构建一个ggplot对象
a <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp))
names(a) ##这里就牵出了ggplot2的几个重要概念。
## [1] "data" "layers" "scales" "mapping" "theme"
## [6] "coordinates" "facet" "plot_env" "labels"
head(a$data)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
str(a$mapping)
## List of 3
## $ x : symbol dose
## $ y : symbol len
## $ fill: symbol supp
a$coordinates
## <ggproto object: Class CoordCartesian, Coord>
## aspect: function
## distance: function
## expand: TRUE
## is_linear: function
## labels: function
## limits: list
## range: function
## render_axis_h: function
## render_axis_v: function
## render_bg: function
## render_fg: function
## train: function
## transform: function
## super: <ggproto object: Class CoordCartesian, Coord>
## 我们想对a中的数据做一个violin plot,使用统计方法geom_violin
b <- a + geom_violin(trim=FALSE, position=position_dodge(1))
## position_dodge(width=1)的意思是在整个区域内分散,
## 也可以试试position_dodge(width=.5)
head(b$data) ## 与a一致
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
sapply(names(a), function(.ele) identical(a[[.ele]], b[[.ele]]))
## data layers scales mapping theme coordinates
## TRUE FALSE FALSE TRUE TRUE TRUE
## facet plot_env labels
## TRUE TRUE TRUE
## 我们可以看到,有两项发生了变化
a$layers ## layer 长度为0
## list()
b$layers ## layer 长度为1
## [[1]]
## geom_violin: draw_quantiles = NULL, na.rm = FALSE
## stat_ydensity: trim = FALSE, scale = area, na.rm = FALSE
## position_dodge
a ## 输出a的时候,因为会调用强制打印,所以它会启动绘图动作,但是因为layers长度为0,会报错。
b ## layers长度为1,就会画出一个violin的图了。
a$scales
## <ggproto object: Class ScalesList>
## add: function
## clone: function
## find: function
## get_scales: function
## has_scale: function
## input: function
## n: function
## non_position_scales: function
## scales: NULL
## super: <ggproto object: Class ScalesList>
b$scales ## b$scales有了长度为零的一个list
## <ggproto object: Class ScalesList>
## add: function
## clone: function
## find: function
## get_scales: function
## has_scale: function
## input: function
## n: function
## non_position_scales: function
## scales: list
## super: <ggproto object: Class ScalesList>
## violin plot虽然可以很好的体现数据分布,但是它失去了类似boxplot中的中值等信息
## 我们来加一个boxplot
b + geom_boxplot(width=0.1, fill="white")
## 直接使用boxplot并不是我们想要的,因为它只是针对的一组数据,
## 现在每个浓度的数据都按照supp分成了两组,所以我们需要写出定制的boxplot的函数
data_summary <- function(x){
f <- fivenum(x)
##对于fun.data,它的输入是每个x对应的y值。 ...
##它的输出是可以定制。一般来说,要按照你指定的geom可以接受的变量来输出
##因为我们接下来使用的geom是crossbar, 它可以定制的变量是x, y, ymax, ymin, alpha等
##所以我们在控制输出时,输出了y, min, ymax.
return(c(y=f[3], ymin=f[2], ymax=f[4]))
}
b + stat_summary(fun.data=data_summary,
position=position_dodge(1), ## 位置需要和之前的一样抖开
width=.2, alpha=.3, ## box的宽度不能太宽,太宽了不好看
geom="crossbar")
##问题是,我们想让box的颜色和背景不同,怎么做?其实不容易
box.data.source <- ToothGrowth
colnames(box.data.source)[2] <- "boxcolor"
levels(box.data.source$boxcolor) <- c("OJ_BOX", "VC_BOX") ## 增加fill的数据
c <- b + stat_summary(aes(x=dose, y=len, fill=boxcolor),
data=box.data.source,
fun.data=data_summary,
position=position_dodge(1),
width=.2, alpha=.8,
geom="crossbar")
b$layers
## [[1]]
## geom_violin: draw_quantiles = NULL, na.rm = FALSE
## stat_ydensity: trim = FALSE, scale = area, na.rm = FALSE
## position_dodge
c$layers ## 我们可以看到,在绘图区域,有了两层图像
## [[1]]
## geom_violin: draw_quantiles = NULL, na.rm = FALSE
## stat_ydensity: trim = FALSE, scale = area, na.rm = FALSE
## position_dodge
##
## [[2]]
## mapping: x = dose, y = len, fill = boxcolor
## geom_crossbar: na.rm = FALSE, width = 0.2
## stat_summary: fun.data = function (x)
## {
## f <- fivenum(x)
## return(c(y = f[3], ymin = f[2], ymax = f[4]))
## }, fun.y = NULL, fun.ymax = NULL, fun.ymin = NULL, fun.args = list(), na.rm = FALSE
## position_dodge
c
##模拟ggplot2的默认颜色
gg_color_hue <- function(n) {
hues = seq(15, 375, length=n+1)
hcl(h=hues, l=65, c=100)[1:n]
}
cols <- gg_color_hue(2) ## 因为原本的full颜色只有两个,
## 我们加入了两个levels,原本的levels就从 OJ, VC变成了OJ, OJ_BOX, VC, VC_BOX
## 颜色的赋值应该保持相同的顺序,当我们希望把box的颜色变成白色时,
## 颜色值就应该是cols[1], "white", cols[2], "white"
d <- c + scale_fill_manual(name="suppl",
values=c(cols[1], "white",
cols[2], "white"),
breaks=c("OJ", "VC"), ## 决定不显示OJ_BOX和VC_BOX
labels=c("OJ", "VC"))
c$scales
## <ggproto object: Class ScalesList>
## add: function
## clone: function
## find: function
## get_scales: function
## has_scale: function
## input: function
## n: function
## non_position_scales: function
## scales: list
## super: <ggproto object: Class ScalesList>
d$scales
## <ggproto object: Class ScalesList>
## add: function
## clone: function
## find: function
## get_scales: function
## has_scale: function
## input: function
## n: function
## non_position_scales: function
## scales: list
## super: <ggproto object: Class ScalesList>
f <- d + theme_bw()
identical(f$theme, d$theme) ## theme 被改变
## [1] FALSE
f
当一个data.frame转递给ggplot时,ggplot就会成一个ggplot对象,用来保存data等参数。 每一个ggplot对象都会有data, layers, scales, mapping, theme, coordinates, facet, plot_env, 以及labels。 当我们每增加一个绘图语句时,ggplot2就在相关的参数上进行修改。当最后print的时候,再按照这些参数来输出图像。
ggplot2 统计方法
ggplot2的统计方法其实就是对数据的再运算,然后将生成的结果追加到图上。一个简单的例子:
a <- ggplot(mpg, aes(x=hwy))
a + stat_bin(aes(fill=..count.., color=-1*..ndensity..), binwidth = 1)
统计方法有输入,有输出。通常输入为x和y。输出值会以例的形式追加到当前操作的数据拷贝中。比如上例中的stat_bin函数,就会生成四列新数据,分别为count, density, ncount以及ndensity。在访问这些新列的时候,使用..name..的方式。
ggplot2的色彩空间
ggplot2的scales不单单可以修改色彩空间,它还可以修改坐标等。但是我这里只想提一两句色彩的问题。当映射颜色至color时,就使用scale_color_xxxx这样的函数。当是fill时,就使用scale_fill_xxxx这样的函数。 其中,scale_color_brewer()
函数中有些默认的颜色组,我们怎么了解其效果呢?
library(RColorBrewer)
display.brewer.all()
从上图中,我们可以了解到有哪些颜色组可以使用,我们不至于会问“Accent”, “Dark2”这些颜色名字是从哪里来的这类的问题了。
代码实践
这一节,将一一重现文章开始出现的每一幅的代码。大家可以参考这些代码,依据自己的需要修改。有任何疑问,请自行查阅说明文档或者网上资源。
data("ToothGrowth")
ToothGrowth$dose <- factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, fill=dose)) +
geom_boxplot(outlier.colour = NULL, outlier.size=0) +
stat_summary(geom = "crossbar", width=0.65, fatten=0,
color="white", fun.data = function(x){
return(c(y=median(x), ymin=median(x), ymax=median(x))) }) +
labs(title="box plot") + scale_fill_hue(guide=FALSE) +
scale_color_hue(guide=FALSE) + theme_bw() +
theme(panel.grid.major.x=element_blank(), axis.text.x=element_blank(),
axis.ticks.x = element_blank(), axis.line.x = element_blank(),
axis.title.x=element_blank(), axis.line.y = element_blank(),
axis.title.y=element_blank(), axis.text.y = element_text(color="gray"),
axis.ticks.y= element_line(color="gray"))
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2) +
stat_summary(fun.data=mean_sdl, mult=1, geom="pointrange", color="red") +
scale_fill_brewer(palette="Blues", guide=FALSE) +
theme_bw() + labs(title="dot plot")
ggplot(ToothGrowth, aes(x=dose, y=len, shape=dose, color=dose)) +
geom_jitter(position=position_jitter(.2), size=5) +
stat_summary(fun.data=mean_sdl, mult=1, geom="pointrange", color="red") +
scale_color_brewer(palette="Dark2", guide=FALSE) + scale_shape(guide=FALSE) +
theme_bw() + labs(title="strip plot")
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_violin(trim=FALSE) + geom_boxplot(width=0.1, fill="white") +
scale_fill_brewer(palette="Blues", guide=FALSE) +
theme_bw() + labs(title="violin plot")
data(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=5) + geom_smooth(method=lm, se=FALSE, fullrange=TRUE) +
scale_color_brewer(palette="Paired") + theme_bw() + labs(title="scatter plot")
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=5, pch=21, color="gray30", fill="cyan") +
geom_smooth(method=lm, se=TRUE, fullrange=TRUE, color="red", fill="gray80") +
theme_bw() + labs(title="regression line")
ToothGrowth.s <- melt(tapply(ToothGrowth$len, ToothGrowth[, c("dose", "supp")], mean),
value.name = "len")
ggplot(ToothGrowth.s, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp, color=supp)) +
geom_point(aes(color=supp), size=5) +
scale_color_brewer(palette="Dark2") +
theme_bw() + labs(title="line plot")
ToothGrowth.s$dose <- factor(ToothGrowth.s$dose)
ToothGrowth.s$supp <- factor(ToothGrowth.s$supp)
ToothGrowth.s$sd <- melt(tapply(ToothGrowth$len, ToothGrowth[, c("dose", "supp")], sd))$value
ggplot(ToothGrowth.s, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2, position=position_dodge(.9)) +
scale_fill_brewer(palette="Reds") + theme_minimal() + labs(title="bar plot")
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5)))
)
mu <- melt(tapply(df$weight, df$sex, mean), varnames="sex", value.name="grp.mean")
ggplot(df, aes(x=weight, color=sex)) + geom_density() +
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex), linetype="dashed") +
scale_color_brewer(palette="Accent") +
theme_minimal() + labs(title="density curve")
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density..), alpha=0.5, position="identity", fill="white", binwidth=1) +
geom_density(alpha=.2) + scale_color_brewer(palette="Accent") +
scale_fill_brewer(palette="Accent") + theme_minimal() + labs(title="histogram")
ggplot(df, aes(x=weight, fill=sex)) + geom_area(stat ="bin", alpha=0.4, binwidth=1) +
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex), linetype="dashed") +
scale_fill_brewer(palette="Accent") + scale_color_brewer(palette="Accent") +
theme_minimal() + labs(title="area plot")
x <- c(1/3, 1/9, 1/9, 1/9, 1/12, 1/12, 1/12, 1/12)
df <- data.frame(x=cumsum(x)-x/2, id=factor(paste0(letters[1:8], 1:8)), value=c(60, 50, 100, 67, 90, 70, 44, 88), width=x)
ggplot(df, aes(x=x, y=value, width=width)) +
geom_bar(aes(fill=id), stat = "identity", position="identity") +
scale_fill_manual(values = c('#2EB400BB', '#2BC8C999', '#2BC8C966',
'#2BC8C933', "#666666BB", "#66666699",
"#66666666", "#66666633"), breaks=c()) +
coord_polar(theta = "x", start = 0) + theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
panel.border = element_blank(), axis.text.x = element_blank()) +
geom_text(aes(y=value+10, label=id)) + labs(title="radar plot")
继续阅读
http://www.sthda.com/english/wiki/ggplot2-introduction
http://www.cookbook-r.com/Graphs/
如果您有好的资源,留言后可能被加到这里。
附录
ggplot2的基本图形
在ggplot2中一维数据图形有:
old_theme <- theme_get()
## <U+8BBE><U+7F6E><U+9ED8><U+8BA4><U+7684><U+4E3B><U+9898><U+4E3A><U+9ED1><U+767D><U+4E3B><U+9898>
theme <- theme_set(theme_bw())
## <U+8FDE><U+7EED><U+6570><U+636E>
a <- ggplot(mpg, aes(x=hwy))
a + geom_area(stat="bin") ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size
a + geom_density() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size, weight
a + geom_dotplot() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill
a + geom_histogram(binwidth=5) ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size, weight
## <U+79BB><U+6563><U+6570><U+636E>
b <- ggplot(mpg, aes(x=fl))
b + geom_bar() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, alpha, color, fill, linetype, size, weight
二维数据图形有:
d <- ggplot(economics, aes(x=date, y=unemploy))
d + geom_path() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, linetype, size
d + geom_ribbon(aes(ymin=unemploy-900, ymax=unemploy+900)) ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, ymax, ymin, alpha, color, fill, linetype, size
d + geom_area() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size
d + geom_line() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, linetype, size
d + geom_step() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, linetype, size
f <- ggplot(mpg, aes(x=cty, y=hwy))
f + geom_jitter() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, shape, size
f + geom_point() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, shape, size
f + geom_quantile() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, linetype, size, weight
f + geom_rug() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>alpha, color, linetype, size
f + geom_smooth() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size, weight
f + geom_text(aes(label=cty)) ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, label, alpha, color, family, fontface, hjust, vjust, lineheight, size
## x<U+79BB><U+6563><U+FF0C> y<U+8FDE><U+7EED>
g <- ggplot(mpg, aes(x=class, y=hwy))
g + geom_bar(stat="identity") ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size, weight
g + geom_boxplot() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>lower, middle, upper, x, ymax, ymin, alpha, color, fill, linetype, shape, size, weight
g + geom_dotplot() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill
g + geom_violin() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, linetype, size, weight
## x<U+79BB><U+6563><U+FF0C>y<U+79BB><U+6563>
h <- ggplot(diamonds, aes(x=cut, y=color))
h + geom_jitter() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, shape, size
## <U+8FDE><U+7EED><U+4E8C><U+5143><U+5206><U+5E03>
i <- ggplot(movies, aes(x=year, y=rating))
i + geom_bin2d() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size, weight
i + geom_density2d() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, linetype, size
i + geom_hex() ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, color, fill, size
## Error in ggplot(movies, aes(x = year, y = rating)): object 'movies' not found
## Error in i + geom_bin2d(): non-numeric argument to binary operator
## Error in i + geom_density2d(): non-numeric argument to binary operator
## Error in i + geom_hex(): non-numeric argument to binary operator
三维数据图形有:
seals$z <- with(seals, sqrt(delta_long^2+delta_lat^2))
m <- ggplot(seals, aes(x=long, y=lat))
m + geom_contour(aes(z=z)) ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, z, alpha, color, linetype, size, weight
m + geom_raster(aes(fill=z), interpolate=FALSE) ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, alpha, fill
m + geom_tile(aes(fill=z)) ## <U+53EF><U+63A7><U+53D8><U+91CF><U+FF1A>x, y, z, alpha, color, fill, linetype, size
R cookbook, 也是一个不错的在线资源;
http://www.cookbook-r.com/Graphs/
谢谢分享。
请问您是UMASS MED的老师吗?我从您这里学了很多,一直很景仰您!
谢谢您的留言。我在UMASS MED工作而已。
大哥,可以分享一下如何输出各种统计结果到csv上吗?
没什么直接的办法,但是可以通过一些特殊手段。你可以体会一下下面的代码:
qplot(x=wt, y=mpg, data=mtcars) +
geom_point(size=5, pch=21, color="gray30", fill="cyan") +
geom_smooth(aes(output=global_var_name<<-list(y=..y.., ymin=..ymin.., ymax=..ymax.., se=..se..)), method=lm, se=TRUE, fullrange=TRUE, color="red", fill="gray80") + theme_minimal() global_var_name
其中<<-的意思是将结果输出给全局变量。..variable..是smooth的结果变量的取值方式。