R手册(Visualise)--ggplot2
文章目錄
- Overview
- Geoms
- 基本圖形
- 單變量
- 雙變量
- 三變量
- 文本
- 誤差可視化
- 地圖
- Stats
- Scales
- 常用標(biāo)尺格式
- 坐標(biāo)軸標(biāo)尺
- Color and fill scales
- Shape and size scales
- Coordinate Systems
- Position Adjustments
- Themes
- Faceting
- Annotations and Labels
- Legends
- Vector helpers
- 混合圖
- [ggplot2 extensions](https://blog.csdn.net/qq_41518277/article/details/80516938)
Overview
ggplot2 基于the grammar of graphics思想,通過數(shù)據(jù)集、幾何對(duì)象和坐標(biāo)系統(tǒng)建立圖形。
所有的ggplot2繪圖都以ggplot()開始, 默認(rèn)由aes()將數(shù)據(jù)集映射至幾何對(duì)象。在行尾+添加圖層:幾何,比例尺,坐標(biāo)和面等。要將繪圖保存,請(qǐng)使用ggsave()
完整的ggplot2圖形包括:
- ggplot(data,aes(...)): Create a new ggplot (required)
- geom_<FUNC>(aes(...),data,stat,position): Geometric object (required)
or stat_<FUNC>(aes(...),data,geom,position): Statistical transformation (required) - coord_<FUNC>(...): Coordinate systems
- facet_<FUNC>(...): Facetting
- scale_<FUNC>(...): Scales
- theme_<FUNC>(...): Themes
data, aes參數(shù)可以在ggplot, geom_<FUNC>, stat_<FUNC>任一函數(shù)中加載
aes() 控制數(shù)據(jù)中的變量映射到幾何圖形。aes()映射可以在ggplot()和geom圖層中設(shè)置。常用參數(shù):alpha, color, group, linetype, size
ggsave(filename, plot = last_plot(),path=NULL,width, height, units= c("in", "cm", "mm"))保存ggplot2圖形,默認(rèn)保存最后一個(gè)圖
qplot(),quickplot(): Quick plot
Geoms
用geom函數(shù)表現(xiàn)圖形,geom中的aes參數(shù)映射數(shù)據(jù),每一個(gè)geom函數(shù)添加一個(gè)圖層。
| color/fill | color/fill=NA 消除線條或填充 |
| alpha | 0 <= alpha <= 1 |
| linetype | 線條形式(1:6) |
| size | 點(diǎn)的尺寸和線的寬度 |
| shape | 點(diǎn)的形狀(0:25) |
基本圖形
| geom_blank() | 空白 | |
| geom_curve(aes(yend,xend,curvature)) geom_segment(aes(yend,xend)) | x, xend, y, yend, alpha, angle, color, curvature(曲率), linetype, size, arrow | 曲線 線段 |
| geom_path(lineend, linejoin, linemitre) | x, y, alpha, color, group, linetype, size(x為分類變量時(shí)必須設(shè)置group=1) | 路徑 |
| geom_polygon(aes(group)) | x, y, alpha, color, fill, group, linetype, size | 多邊形 |
| geom_rect(aes(xmin, ymin, xmax, ymax)) | xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size | 矩形 |
| geom_ribbon(aes(ymin, ymax)) | x, ymax, ymin, alpha, color, fill, group, linetype, size | 絲帶圖 |
| geom_abline(aes(intercept, slope)) | x, y, alpha, color, linetype, size | 斜線 |
| geom_hline(aes(yintercept)) | x, y, alpha, color, linetype, size | 水平線 |
| geom_vline(aes(xintercept)) | x, y, alpha, color, linetype, size | 垂直線 |
| geom_segment(aes(yend, xend)) | x, y, alpha, color, linetype, size | 線段 |
| geom_spoke(aes(angle, radius)) | x, y, alpha, color, linetype, size | 輻條 |
單變量
stat參數(shù) : bin/count/identity
- 若x為連續(xù)變量:stat= ”bin”
- 若x離散變量:stat = “count”或stat = “identity”
| geom_area() | x, y, alpha, color, fill, linetype, size,stat={identity,bin} | 面積圖 |
| geom_density(kernel = “gaussian”) | x, y, alpha, color, fill, group, linetype, size, weight, adjust=1/2(調(diào)整帶寬倍率), stat={density,count,scaled (密度估計(jì)值)} | 核密度圖 |
| geom_dotplot() | x, y, alpha, color, fill | 點(diǎn)狀分布圖 |
| geom_freqpoly() | x, y, alpha, color, group, linetype, size | 頻率多邊形(類似直方圖) |
| geom_histogram(binwidth) | x, y, alpha, color, fill, linetype, size, weight | 直方圖 |
| geom_qq(aes(sample)) | x, y, alpha, color, fill, linetype, size, weight | qq圖(檢測(cè)正態(tài)分布) |
| discrete | ||
| geom_bar() | x, alpha, color, fill, linetype, size, weight, stat={count,prop (分組比例)} | 柱狀圖 |
雙變量
| geom_jitter(height , width) | x, y, alpha, color, fill, shape, size | 抖點(diǎn)圖(避免重合),stat = “identity” |
| geom_point() | x, y, alpha, color, fill, shape, size, stroke | 散點(diǎn)圖stat = “identity” |
| geom_quantile() | x, y, alpha, color, group, linetype, size, weight | 四分位圖 |
| geom_rug(sides) | x, y, alpha, color, linetype, size,sides(地毯位置) | 地毯圖 |
| geom_smooth() | x, y, alpha, color, fill, group, linetype, size, weight | 擬合曲線,自動(dòng)計(jì)算變量:y/ymin/ymax/se(標(biāo)準(zhǔn)誤) |
geom_smooth(method = "auto", formula = y ~ x)
參數(shù):
method: eg. “l(fā)m”, “glm”, “gam”, “l(fā)oess”, “rlm”
formula :eg. y ~ x, y ~ poly(x, 2), y ~ log(x)
se : 是否繪制置信區(qū)間(默認(rèn)為TRUE)
level : 用的置信區(qū)間水平(默認(rèn)為95%)
| geom_density2d() | x, y, alpha, colour, group, linetype, size, fill=…level…(自動(dòng)計(jì)算變量) | 分布密度 |
| geom_bin2d(binwidth) | x, y, alpha, color, fill, linetype, size, weight | 矩形箱,stat = “bin2d” |
| geom_hex() | x, y, alpha, colour, fill, size | 六角倉,stat = “binhex” |
| geom_area() | x, y, alpha, color, fill, linetype, size | 面積圖 |
| geom_line() | x, y, alpha, color, group, linetype, size | 折線圖 |
| geom_step(direction = “hv”) | x, y, alpha, color, group, linetype, size | 階梯圖 |
| geom_col() | x, y, alpha, color, fill, group, linetype, size | 柱狀圖 |
| geom_boxplot() | x, y, lower, middle, upper, ymax, ymin, alpha, color, fill, group, linetype, shape, size, weight, notch(是否缺口), width,outlier | 箱線圖 |
| geom_dotplot(binaxis = “y”, stackdir = “center”) | x, y, alpha, color, fill, group | 點(diǎn)狀分布圖(不重合) |
| geom_violin(scale = “area”) | x, y, alpha, color, fill, group, linetype, size, weight | 小提琴圖 |
| ggforce::geom_sina() | 點(diǎn)狀小提琴圖 |
| geom_count() | x, y, alpha, color, fill, shape, size, stroke | 計(jì)數(shù)圖 |
三變量
| geom_contour(aes(z)) | x, y, z, alpha, colour, group, linetype, size, weight,bins(等高線數(shù)量),binwidth(等高線寬度) | 等高線圖, …level…(輪廓高度,自動(dòng)計(jì)算變量) |
| geom_raster(aes(fill)) | x,y,alpha,fill | 光柵(熱力圖) |
| geom_tile(aes(fill)) | x, y, alpha, color, fill, linetype, size, width | 瓦片(熱力圖) |
文本
| geom_text(aes(label), nudge_x, nudge_y, check_overlap = TRUE) | x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust | |
| geom_label(aes(label), nudge_x, nudge_y, check_overlap = TRUE) | x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust | 有背景框 |
參數(shù):
nudge_x, nudge_y: 微調(diào)
check_overlap:是否過重疊
vjust,hjust: 對(duì)齊方式(0:1)
angle: 角度
lineheight:行間距
family: 字體
size: 字體大小
fontface: 字體格式(1:4, plain標(biāo)準(zhǔn),bold加粗,italic斜體,bold.italic)
ggrepel包 : 文字不重疊
ggrepel:: geom_label_repel()
ggrepel:: geom_text_repel()
誤差可視化
| geom_crossbar(fatten) | x, y, ymax, ymin, alpha, color, fill, group, linetype, size | |
| geom_errorbar() | x, ymax, ymin, alpha, color, group, linetype, size, width (also geom_errorbarh()) | |
| geom_linerange() | x, ymin, ymax, alpha, color, group, linetype, size | |
| geom_pointrange() | x, y, ymin, ymax, alpha, color, fill, group, linetype, shape, size |
aes(ymin,ymax) : ymin,ymax需要在aes參數(shù)內(nèi)賦值
df <- data.frame(trt = factor(c('A', 'A', 'B', 'B')),resp = c(1, 5, 3, 4),group = factor(c(1, 2, 1, 2)),upper = c(1.1, 5.3, 3.3, 4.2),lower = c(0.8, 4.6, 2.4, 3.6) )dodge <- position_dodge(width=0.9) #位置微調(diào) p <- ggplot(df, aes(trt, resp, fill = group,ymin = lower, ymax = upper))+geom_col(position = dodge)p + geom_crossbar(fatten = 2,position = dodge) +ggtitle('geom_crossbar') p + geom_errorbar(position = dodge, width = 0.25) +ggtitle('geom_errorbar') p +geom_linerange(position = dodge) +ggtitle('geom_linerange') p +geom_pointrange(position = dodge) +ggtitle('geom_pointrange')地圖
| geom_map(aes(map_id), map) | map_id, alpha, color, fill, linetype, size | 地圖 |
參數(shù):
map_id:id/region
map: data.frame(x/long, y/lat, id/region) 或sp空間數(shù)據(jù)
Tips:
sf::st_read 讀取sp或json文件
sf::st_transform 轉(zhuǎn)換sf文件
地圖素材:
矢量地圖素材鏈接
shp數(shù)據(jù)地圖:GitHub ,GADM
Json數(shù)據(jù)地圖:阿里云, 百度Echarts
Stats
ggplot2還提供一種替代方案,建立一個(gè)圖層用stat計(jì)算新變量 (e.g., count, prop)作圖。
- 改變geom函數(shù)的stat默認(rèn)值,如geom_bar(stat="count")
- 調(diào)用stat_<FUNC>,如 stat_count(geom="bar")
- 用..name..的語法格式,將stat計(jì)算變量映射到幾何對(duì)象,如stat_density2d(aes(fill = ..level..), geom = "polygon")
| stat_bin(binwidth, origin) | x, y | …count…, …ncount…, …density…, …ndensity… | |
| stat_count(width = 1) | x, y | …count…, …prop… | |
| stat_density(adjust = 1, kernel = “gaussian") | x, y | …count…, …density…, …scaled… | |
| ------ | ------ | ------ | ------ |
| stat_bin_2d(bins, drop = TRUE) | x, y, fill | …count…, …density… | |
| stat_bin_hex(bins) | x, y, fill | …count…, …density… | |
| stat_density_2d(contour = TRUE, n = 100) | x, y, color, size | …level… | |
| stat_ellipse(level, segments, type = “t”) | 計(jì)算正常置信度橢圓 | ||
| ------ | ------ | ------ | ------ |
| stat_contour(aes(z)) | x, y, z, order | …level… | |
| stat_summary_hex(aes(z), bins, fun = max) | x, y, z, fill | …value… | 六邊形 |
| stat_summary_2d(aes(z), bins, fun = mean) | x, y, z, fill | …value… | 矩形 |
| stat_boxplot(coef) | x, y | …lower…, …middle…, …upper…, …width… , …ymin…, …ymax… | |
| stat_ydensity(kernel = “gaussian”, scale = “area") | x, y | …density…, …scaled…, …count…, …n…, …violinwidth…, …width… | |
| ------ | ------ | ------ | ------ |
| stat_ecdf(n) | x, y | …x…, …y… | 計(jì)算經(jīng)驗(yàn)累積分布 |
| stat_quantile(quantiles = c(0.1, 0.9), formula = y ~ log(x), method = “rq”) | x, y | …quantile… | |
| stat_smooth(method = “l(fā)m”, formula = y ~ x, se=T, level=0.95) | x, y | …se…, …x…, …y…, …ymin…, …ymax… | |
| ------ | ------ | ------ | ------ |
| stat_function(aes(x = -3:3), n = 99, fun = dnorm, args = list(sd=0.5)) | x | …x…, …y… | 計(jì)算每個(gè)x值的函數(shù) |
| stat_identity(na.rm = TRUE) | 保持原樣 | ||
| stat_qq(aes(sample=1:100), dist = qt, dparam=list(df=5)) | sample, x, y | …sample…, …theoretical… | |
| stat_sum() | x, y, size | …n…, …prop… | |
| stat_summary(fun.data = “mean_cl_boot”) | 將y值匯總在唯一x中 | ||
| stat_summary_bin(fun.y = “mean”, geom = “bar”) | 將y值匯總在分箱x中 | ||
| stat_unique() | 刪除重復(fù)項(xiàng) |
Scales
Scales傳遞數(shù)據(jù)給幾何對(duì)象,改變圖形的默認(rèn)標(biāo)尺。
p <- ggplot(mpg, aes(fl)) +geom_bar(aes(fill = fl))p + scale_fill_manual( # scale: scale, fill: 幾何對(duì)象, manual: 預(yù)處理的scale類型values = c("skyblue", "royalblue", "blue","navy"), #scale參數(shù)limits = c("d", "e", "p", "r"), #限制范圍breaks =c("d", "e", "p", "r"), #breaks to use in legend/axislabels = c("D", "E", "P", "R"), #labels to use in legend/axisname = "fuel") #legend/axis 標(biāo)題常用標(biāo)尺格式
type: color,size,fill,shape,linetype,alpha,etc
scale_<type>_continuous() #連續(xù)變量映射 scale_<type>_discrete() #離散變量映射 scale_<type>_identity() #原始數(shù)據(jù)直接映射 scale_<type>_manual(value=c(…)) #自定義值范圍坐標(biāo)軸標(biāo)尺
| lims(x,y)/xlim()/ylim() | |
| scale_x_continuous(breaks, labels,limits) | 刻度,標(biāo)簽,值的范圍 |
| scale_x_discrete(breaks, labels,limits) | |
| scale_x_date(date_breaks , date_labels) | 日期間隔(“2 weeks”),日期顯示格式(%m/%d) |
| scale_x_datetime() | 時(shí)間日期,參數(shù)同date |
| scale_x_log10() | log10 標(biāo)尺 |
| scale_x_reverse() | x軸方向顛倒 |
| scale_x_sqrt() |
Color and fill scales
| scale_fill_distiller(palette = “Blues”) | |
| scale_fill_gradient(low,high) | 漸變色調(diào)控 |
| scale_fill_gradient2(low,mid,high,midpoint) | 2極漸變色 |
| scale_fill_gradientn(values) | n極漸變色 |
| Discrete | |
| scale_fill_hue() | 離散色階 |
| scale_fill_brewer(palette = “Blues”) | 調(diào)色板 |
| scale_fill_grey(start, end) | 灰色調(diào) |
調(diào)色板離散色階palette選擇:
RColorBrewer::display.brewer.all()
連續(xù)色階選擇:
Also: rainbow(), heat.colors(), terrain.colors(), cm.colors()
RColorBrewer::brewer.pal()
Shape and size scales
| scale_shape() | 形狀 |
| scale_shape_manual(values) | |
| size | |
| scale_size() | 大小 |
| scale_radius(range) | 半徑 |
shape:
Coordinate Systems
| coord_cartesian() | xlim,ylim | 笛卡爾坐標(biāo)(默認(rèn)) |
| coord_fixed() | radio,xlim,ylim | 具有固定縱橫比的直角坐標(biāo) |
| coord_flip | xlim,ylim | x和y翻轉(zhuǎn)(笛卡爾坐標(biāo)) |
| coord_polar | theta, start, direction | 極坐標(biāo) |
| coord_trans() | xtrans, ytrans,xlim,ylim | 變換笛卡爾坐標(biāo)系,xtrans, ytrans接收函數(shù)名 |
| coord_quickmap() coord_map(projection = “ortho”,orientation ) | projection, orienztation, xlim, ylim | 地圖投影 projections:{mercator (default), azequalarea, lagrange, etc.} |
Position Adjustments
Position adjustments 對(duì)geoms進(jìn)行位置調(diào)整。
geom參數(shù)賦值字符串
字符串: identity,jitterdodge (同時(shí)閃避和抖動(dòng)),nudge(微距點(diǎn)固定距離)
| geom_bar/aera/density | dodge(并排), stack/fill(堆疊) |
| geom_point | jitter(減少點(diǎn)重疊) |
| geom_label | nudge(輕推來自點(diǎn)的標(biāo)簽) |
geom參數(shù)賦值position函數(shù)
| position_dodge(width) | 抖動(dòng)寬度(geom默認(rèn)width=0.9,調(diào)用此函數(shù)時(shí),width應(yīng)設(shè)為0.9) |
| position_identity() | 不調(diào)整 |
| position_jitter(width , height ) | |
| position_jitterdodge() | |
| position_nudge(x = 0, y = 0) | 平移距離 |
| position_stack(vjust = 1) | 對(duì)齊方式 |
| position_fill(vjust = 1) |
Themes
theme(…)
設(shè)置主題包括title,axis,legend, panel, background,etc
| theme_bw(base_size,base_family) | 黑白主題 |
| theme_grey() | 灰色主題(默認(rèn)) |
| theme_dark() | 黑色主題 |
| theme_void() | 空主題 |
| theme_minimal() | 最小主題 |
| ggtech::theme_tech() | 技術(shù)主題 |
控制theme元素函數(shù)(作為主題組件的參數(shù)出現(xiàn)):
| element_blank | 清空 |
| element_rect(fill,color,size,linetype) | 邊框和背景 |
| element_text(family,face, color, size,hjust, vjust,angle,lineheight, margin, debug) | 文字,參數(shù):lineheight(行高), margin(邊緣), debug(是否矩形背景) |
| element_line(color,size,linetype,lineend,arrow) | Line end style (round, butt, square),添加箭頭: grid::arrow(angle,length,ends=“l(fā)ast”/“first”/“both”) |
Faceting
刻面通過類別變量將圖形分塊顯示。
| facet_grid(var.row~var.col,scales,labeller) | 網(wǎng)格圖,單變量時(shí)var.row或var.col用點(diǎn)填充 |
| facet_wrap(~var+var,nrow,ncol,scales,labeller) | 將1d的面板卷成2d網(wǎng)格(nrow*ncol) |
| ggforce::facet_zoom(x, y, xy, split = FALSE, zoom.size = 2) | 子集zoom,x,y,xy 賦值(邏輯值):選取x軸,y軸,xy交叉子集 |
**scales參數(shù):**坐標(biāo)刻度自由
“fixed”(default,坐標(biāo)尺度統(tǒng)一), “free”(坐標(biāo)尺度自由),“free_x”,“free_y”
labeller參數(shù): 調(diào)整刻面標(biāo)簽
Annotations and Labels
| ggtitle(label, subtitle = NULL) | 圖標(biāo)題 |
| labs(x,y,title,subtitle,caption) | x/y軸標(biāo)題 |
| xlab(label)/ylab(label) | x/y軸標(biāo)題 |
| annotate(geom,…) | geom注釋,其余參數(shù)為geom參數(shù) |
| annotate(“text”,x,y,label, parse=FALSE) | 文本注釋 |
| annotate(“pointrange”, x , y, ymin, ymax) | |
| annotate(“rect”, xmin, xmax, ymin, ymax) | 矩形注釋 |
| annotate(“segment”,x, xend, y, yend, arrow) | 線段注釋 |
文本注釋參數(shù)parse:是否數(shù)學(xué)表達(dá)式, 更多公式語法可參考?plotmath
線段注釋參數(shù)arrow: 添加箭頭grid::arrow(angle,length,ends=”last” / ”first”/”both”)
Legends
theme(legend.position = "none"/"bottom"/"top"/"left"/"right") 在主題中設(shè)置 Legend
guides(…)設(shè)置legends幾何組件:colorbar, legend, or none (no legend)
| guide_colorbar(title,label…) | 連續(xù)型變量 |
| guide_legend(title,label…) | 離散型變量 |
Vector helpers
| stats::recoder(x_char,x_num,order=FALSE) | 重排序,返回factor/ord.factor |
| cut_interval(x,n,length) | n組有相同寬度的觀測(cè)值 |
| cut_number(x,n) | n組有相同數(shù)量的觀測(cè)值 |
| cut_width(x,width,center,boundary, closed = c(“right”,“l(fā)eft”)) |
混合圖
gridExtra包可以將多個(gè)ggplot2對(duì)象組合到一張圖中
gridExtra::grid.arrange(plot1,plot2,…,nrow,ncol)ggplot2 extensions
ggplot2 now has an official extension mechanism. This means that others can now easily create their own stats, geoms and positions, and provide them in other packages. This should allow the ggplot2 community to flourish, even as less development work happens in ggplot2 itself. This page showcases these extensions.
總結(jié)
以上是生活随笔為你收集整理的R手册(Visualise)--ggplot2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯智慧校园 php,【腾讯智慧校园1.
- 下一篇: PDF编辑处理神器 PDFdo PDF转