R手册(Visualise)--ggplot2
文章目錄
- Overview
- Geoms
- 基本圖形
- 單變量
- 雙變量
- 三變量
- 文本
- 誤差可視化
- 地圖
- Stats
- Scales
- 常用標尺格式
- 坐標軸標尺
- 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思想,通過數據集、幾何對象和坐標系統建立圖形。
所有的ggplot2繪圖都以ggplot()開始, 默認由aes()將數據集映射至幾何對象。在行尾+添加圖層:幾何,比例尺,坐標和面等。要將繪圖保存,請使用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參數可以在ggplot, geom_<FUNC>, stat_<FUNC>任一函數中加載
aes() 控制數據中的變量映射到幾何圖形。aes()映射可以在ggplot()和geom圖層中設置。常用參數:alpha, color, group, linetype, size
ggsave(filename, plot = last_plot(),path=NULL,width, height, units= c("in", "cm", "mm"))保存ggplot2圖形,默認保存最后一個圖
qplot(),quickplot(): Quick plot
Geoms
用geom函數表現圖形,geom中的aes參數映射數據,每一個geom函數添加一個圖層。
| color/fill | color/fill=NA 消除線條或填充 |
| alpha | 0 <= alpha <= 1 |
| linetype | 線條形式(1:6) |
| size | 點的尺寸和線的寬度 |
| shape | 點的形狀(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為分類變量時必須設置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參數 : bin/count/identity
- 若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(調整帶寬倍率), stat={density,count,scaled (密度估計值)} | 核密度圖 |
| geom_dotplot() | x, y, alpha, color, fill | 點狀分布圖 |
| 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圖(檢測正態分布) |
| discrete | ||
| geom_bar() | x, alpha, color, fill, linetype, size, weight, stat={count,prop (分組比例)} | 柱狀圖 |
雙變量
| geom_jitter(height , width) | x, y, alpha, color, fill, shape, size | 抖點圖(避免重合),stat = “identity” |
| geom_point() | x, y, alpha, color, fill, shape, size, stroke | 散點圖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 | 擬合曲線,自動計算變量:y/ymin/ymax/se(標準誤) |
geom_smooth(method = "auto", formula = y ~ x)
參數:
method: eg. “lm”, “glm”, “gam”, “loess”, “rlm”
formula :eg. y ~ x, y ~ poly(x, 2), y ~ log(x)
se : 是否繪制置信區間(默認為TRUE)
level : 用的置信區間水平(默認為95%)
| geom_density2d() | x, y, alpha, colour, group, linetype, size, fill=…level…(自動計算變量) | 分布密度 |
| 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 | 點狀分布圖(不重合) |
| geom_violin(scale = “area”) | x, y, alpha, color, fill, group, linetype, size, weight | 小提琴圖 |
| ggforce::geom_sina() | 點狀小提琴圖 |
| geom_count() | x, y, alpha, color, fill, shape, size, stroke | 計數圖 |
三變量
| geom_contour(aes(z)) | x, y, z, alpha, colour, group, linetype, size, weight,bins(等高線數量),binwidth(等高線寬度) | 等高線圖, …level…(輪廓高度,自動計算變量) |
| 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 | 有背景框 |
參數:
nudge_x, nudge_y: 微調
check_overlap:是否過重疊
vjust,hjust: 對齊方式(0:1)
angle: 角度
lineheight:行間距
family: 字體
size: 字體大小
fontface: 字體格式(1:4, plain標準,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參數內賦值
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) #位置微調 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 | 地圖 |
參數:
map_id:id/region
map: data.frame(x/long, y/lat, id/region) 或sp空間數據
Tips:
sf::st_read 讀取sp或json文件
sf::st_transform 轉換sf文件
地圖素材:
矢量地圖素材鏈接
shp數據地圖:GitHub ,GADM
Json數據地圖:阿里云, 百度Echarts
Stats
ggplot2還提供一種替代方案,建立一個圖層用stat計算新變量 (e.g., count, prop)作圖。
- 改變geom函數的stat默認值,如geom_bar(stat="count")
- 調用stat_<FUNC>,如 stat_count(geom="bar")
- 用..name..的語法格式,將stat計算變量映射到幾何對象,如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”) | 計算正常置信度橢圓 | ||
| ------ | ------ | ------ | ------ |
| 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… | 計算經驗累積分布 |
| stat_quantile(quantiles = c(0.1, 0.9), formula = y ~ log(x), method = “rq”) | x, y | …quantile… | |
| stat_smooth(method = “lm”, 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… | 計算每個x值的函數 |
| 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() | 刪除重復項 |
Scales
Scales傳遞數據給幾何對象,改變圖形的默認標尺。
p <- ggplot(mpg, aes(fl)) +geom_bar(aes(fill = fl))p + scale_fill_manual( # scale: scale, fill: 幾何對象, manual: 預處理的scale類型values = c("skyblue", "royalblue", "blue","navy"), #scale參數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 標題常用標尺格式
type: color,size,fill,shape,linetype,alpha,etc
scale_<type>_continuous() #連續變量映射 scale_<type>_discrete() #離散變量映射 scale_<type>_identity() #原始數據直接映射 scale_<type>_manual(value=c(…)) #自定義值范圍坐標軸標尺
| lims(x,y)/xlim()/ylim() | |
| scale_x_continuous(breaks, labels,limits) | 刻度,標簽,值的范圍 |
| scale_x_discrete(breaks, labels,limits) | |
| scale_x_date(date_breaks , date_labels) | 日期間隔(“2 weeks”),日期顯示格式(%m/%d) |
| scale_x_datetime() | 時間日期,參數同date |
| scale_x_log10() | log10 標尺 |
| scale_x_reverse() | x軸方向顛倒 |
| scale_x_sqrt() |
Color and fill scales
| scale_fill_distiller(palette = “Blues”) | |
| scale_fill_gradient(low,high) | 漸變色調控 |
| scale_fill_gradient2(low,mid,high,midpoint) | 2極漸變色 |
| scale_fill_gradientn(values) | n極漸變色 |
| Discrete | |
| scale_fill_hue() | 離散色階 |
| scale_fill_brewer(palette = “Blues”) | 調色板 |
| scale_fill_grey(start, end) | 灰色調 |
調色板離散色階palette選擇:
RColorBrewer::display.brewer.all()
連續色階選擇:
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 | 笛卡爾坐標(默認) |
| coord_fixed() | radio,xlim,ylim | 具有固定縱橫比的直角坐標 |
| coord_flip | xlim,ylim | x和y翻轉(笛卡爾坐標) |
| coord_polar | theta, start, direction | 極坐標 |
| coord_trans() | xtrans, ytrans,xlim,ylim | 變換笛卡爾坐標系,xtrans, ytrans接收函數名 |
| coord_quickmap() coord_map(projection = “ortho”,orientation ) | projection, orienztation, xlim, ylim | 地圖投影 projections:{mercator (default), azequalarea, lagrange, etc.} |
Position Adjustments
Position adjustments 對geoms進行位置調整。
geom參數賦值字符串
字符串: identity,jitterdodge (同時閃避和抖動),nudge(微距點固定距離)
| geom_bar/aera/density | dodge(并排), stack/fill(堆疊) |
| geom_point | jitter(減少點重疊) |
| geom_label | nudge(輕推來自點的標簽) |
geom參數賦值position函數
| position_dodge(width) | 抖動寬度(geom默認width=0.9,調用此函數時,width應設為0.9) |
| position_identity() | 不調整 |
| position_jitter(width , height ) | |
| position_jitterdodge() | |
| position_nudge(x = 0, y = 0) | 平移距離 |
| position_stack(vjust = 1) | 對齊方式 |
| position_fill(vjust = 1) |
Themes
theme(…)
設置主題包括title,axis,legend, panel, background,etc
| theme_bw(base_size,base_family) | 黑白主題 |
| theme_grey() | 灰色主題(默認) |
| theme_dark() | 黑色主題 |
| theme_void() | 空主題 |
| theme_minimal() | 最小主題 |
| ggtech::theme_tech() | 技術主題 |
控制theme元素函數(作為主題組件的參數出現):
| element_blank | 清空 |
| element_rect(fill,color,size,linetype) | 邊框和背景 |
| element_text(family,face, color, size,hjust, vjust,angle,lineheight, margin, debug) | 文字,參數:lineheight(行高), margin(邊緣), debug(是否矩形背景) |
| element_line(color,size,linetype,lineend,arrow) | Line end style (round, butt, square),添加箭頭: grid::arrow(angle,length,ends=“last”/“first”/“both”) |
Faceting
刻面通過類別變量將圖形分塊顯示。
| facet_grid(var.row~var.col,scales,labeller) | 網格圖,單變量時var.row或var.col用點填充 |
| facet_wrap(~var+var,nrow,ncol,scales,labeller) | 將1d的面板卷成2d網格(nrow*ncol) |
| ggforce::facet_zoom(x, y, xy, split = FALSE, zoom.size = 2) | 子集zoom,x,y,xy 賦值(邏輯值):選取x軸,y軸,xy交叉子集 |
**scales參數:**坐標刻度自由
“fixed”(default,坐標尺度統一), “free”(坐標尺度自由),“free_x”,“free_y”
labeller參數: 調整刻面標簽
Annotations and Labels
| ggtitle(label, subtitle = NULL) | 圖標題 |
| labs(x,y,title,subtitle,caption) | x/y軸標題 |
| xlab(label)/ylab(label) | x/y軸標題 |
| annotate(geom,…) | geom注釋,其余參數為geom參數 |
| 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) | 線段注釋 |
文本注釋參數parse:是否數學表達式, 更多公式語法可參考?plotmath
線段注釋參數arrow: 添加箭頭grid::arrow(angle,length,ends=”last” / ”first”/”both”)
Legends
theme(legend.position = "none"/"bottom"/"top"/"left"/"right") 在主題中設置 Legend
guides(…)設置legends幾何組件:colorbar, legend, or none (no legend)
| guide_colorbar(title,label…) | 連續型變量 |
| guide_legend(title,label…) | 離散型變量 |
Vector helpers
| stats::recoder(x_char,x_num,order=FALSE) | 重排序,返回factor/ord.factor |
| cut_interval(x,n,length) | n組有相同寬度的觀測值 |
| cut_number(x,n) | n組有相同數量的觀測值 |
| cut_width(x,width,center,boundary, closed = c(“right”,“left”)) |
混合圖
gridExtra包可以將多個ggplot2對象組合到一張圖中
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.
總結
以上是生活随笔為你收集整理的R手册(Visualise)--ggplot2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯智慧校园 php,【腾讯智慧校园1.
- 下一篇: PDF编辑处理神器 PDFdo PDF转