ggplot2

绘图基础参数概览

ggplot() - -用于创建一个新的ggplot

aes() - - 构建美学映射

+() %+% - - 将组件添加到绘图中

ggsave() - -使用合理的默认值保存ggplot

qplot() quickplot() - - 快速绘图(不是ggplot2的精髓就不做具体探究了)

首先来看下 ggplot

ggplot() initializes a ggplot object. It can be used to declare the input data frame for a graphic and to specify the set of plot aesthetics intended to be common throughout all subsequent layers unless specifically overridden.
library(ggplot2)
## ggplot(data = NULL, mapping = aes(), ...,
## environment = parent.frame())

参数

data 用于绘图的默认数据框。如果还没有data.frame,将用fortify()转换为一个。如果未指定,则必须在添加到图中的每个图层中提供。
mapping 用于绘图的美学映射的默认列表。如果未指定,则必须在添加到图中的每个图层中提供。
传递给方法的其他参数。目前尚未使用。
environment DEPRECATED. Used prior to tidy evaluation.

细节

ggplot()用于构造初始绘图对象,之后都会用”+“向绘图添加组件。有三种常见的调用ggplot的方式。
ggplot(df, aes(x, y, other aesthetics))
ggplot(df)
ggplot()
如果所有图层使用相同的数据和相同的美学集,则建议使用第一种方法,尽管此方法也可用于使用来自另一个数据框的数据添加图层。请参见下面的第一个示例。第二种方法指定用于绘图的默认数据框,但不预先定义美学。当一个数据框主要用于添加图层时,这很有用,但美观可能因层而异。第三种方法初始化一个骨架ggplot对象,该对象在添加图层时会被充实。当多个数据帧用于生成不同的层时,此方法很有用,复杂图形中通常就是这种情况。

例子

# Generate some sample data, then compute mean(平均值) and standard deviation(标准偏差)in each group
df <- data.frame(
  gp = factor(rep(letters[1:3], each = 10)),
  y = rnorm(30)
)
df
##    gp           y
## 1   a -0.56566919
## 2   a  1.09347683
## 3   a  0.01264174
## 4   a  0.20764106
## 5   a -0.60425924
## 6   a -0.35413787
## 7   a -0.18665297
## 8   a  1.37631400
## 9   a -0.89224314
## 10  a  0.24115135
## 11  b  1.80992161
## 12  b  0.36841443
## 13  b -0.45313796
## 14  b  0.11995836
## 15  b -1.10622638
## 16  b  0.34465298
## 17  b  1.43860088
## 18  b -0.24907443
## 19  b -0.28134003
## 20  b  0.10261990
## 21  c  0.89342802
## 22  c  0.82346631
## 23  c -0.96738942
## 24  c  0.18361697
## 25  c  0.99188354
## 26  c  0.61532523
## 27  c  0.21182604
## 28  c  2.20025891
## 29  c  0.87446878
## 30  c  0.84004546
ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))#要使用某个包里的函数,通常做法是先加载(library)包,再调用函数。最新加载的包的namespace会成为最新的enviroment,某些情况下可能影响函数的结果。而package name::functionname的用法,一是可以在需要用某个函数时临时直接加载包,不用事先library。另一点更重要的是尽可能减少library带来的附带作用,这一点在开发R包时影响较大。而这种写法的副作用,是会稍微慢上那么几毫秒,在需要反复循环使用一个函数时对效率有影响,其他时候除了写起来麻烦一点,基本没有显见的副作用。以上供参考
ds
##   gp       mean        sd
## 1  a 0.03282626 0.7319719
## 2  b 0.20943894 0.8664384
## 3  c 0.66669298 0.7972113
The summary data frame ds is used to plot larger red points on top of the raw data. Note that we don’t need to supply data or mapping in each layer because the defaults from ggplot() are used.
library(ggplot2)
ggplot(df, aes(gp, y)) +
  geom_point() +
  geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)

#与上图相同,仅声明ggplot()中的数据框。请注意现在必须在每个geom_point()图层中声明x和y美学。
ggplot(df) +
  geom_point(aes(gp, y)) +
  geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3)

#或者我们可以完全指定每一层的图。这在这里没用,但在处理复杂的多数据集图形时可以更清楚
ggplot() +
  geom_point(data = df, aes(gp, y)) +
  geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) +
  geom_errorbar(
    data = ds,
    aes(gp, mean, ymin = mean - sd, ymax = mean + sd),
    colour = 'red',
    width = 0.4
  )
## Warning: Ignoring unknown aesthetics: y

可以看出这里加了个errorbar,后期实验作图,完全可以用ggplot添加errorbar

下一篇讲解aes()