拿R来画画(六):很漂亮的Cleveland点图
生活随笔
收集整理的這篇文章主要介紹了
拿R来画画(六):很漂亮的Cleveland点图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 簡單粗暴堆點
- 各點有序上升下降
- 對樣本分組繪圖
這種圖主要是針對一一對應的關系,簡潔明了,圖形的可讀性很強,因為條形圖在空間上是二維的,不容易直觀建立一一對應的變量關聯。
簡單粗暴堆點
library(gcookbook) library(ggplot2) tophit <- tophitters2001[1:25,] head(tophit)| walkela01 | Larry | Walker | Larry Walker | 2001 | 1 | COL | NL | 142 | 497 | ... | 14 | 5 | 82 | 103 | 6 | 14 | 0 | 8 | 9 | 0.3501 |
| suzukic01 | Ichiro | Suzuki | Ichiro Suzuki | 2001 | 1 | SEA | AL | 157 | 692 | ... | 56 | 14 | 30 | 53 | 10 | 8 | 4 | 4 | 3 | 0.3497 |
| giambja01 | Jason | Giambi | Jason Giambi | 2001 | 1 | OAK | AL | 154 | 520 | ... | 2 | 0 | 129 | 83 | 24 | 13 | 0 | 9 | 17 | 0.3423 |
| alomaro01 | Roberto | Alomar | Roberto Alomar | 2001 | 1 | CLE | AL | 157 | 575 | ... | 30 | 6 | 80 | 71 | 5 | 4 | 9 | 9 | 9 | 0.3357 |
| heltoto01 | Todd | Helton | Todd Helton | 2001 | 1 | COL | NL | 159 | 587 | ... | 7 | 5 | 98 | 104 | 15 | 5 | 1 | 5 | 14 | 0.3356 |
| aloumo01 | Moises | Alou | Moises Alou | 2001 | 1 | HOU | NL | 136 | 513 | ... | 5 | 1 | 57 | 57 | 14 | 3 | 0 | 8 | 18 | 0.3314 |
各點有序上升下降
觀察一下上面的圖可以發現,點上升得很凌亂,沒有規律。因為這里Y軸是按字母序排列的,我們使用reorder來重排一下。reorder會首先將輸入的第一個參數轉化為因子類型,然后根據第二個參數對其進行排序。
根據上表在theme中調整主題,這里去掉背景的網格。
ggplot(tophit, aes(x = avg, y = reorder(name, avg))) + geom_point(size = 3) + # 與默認主題theme_gray相對,背景空白 theme_bw() + theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(), panel.grid.major.y = element_line(colour = 'lightblue', linetype = 'dashed'))顛倒一下順序
ggplot(tophit, aes(x = avg, y = reorder(name, -avg))) + geom_point(size = 3) + # 與默認主題theme_gray相對,背景空白 theme_bw() + theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(), panel.grid.major.y = element_line(colour = 'lightblue', linetype = 'dashed'))也可以直接X軸Y軸對調,此時引入更多的美化操作,比如X軸的文本傾斜,以及更細致地調整點和線條。
ggplot(tophit, aes(x = reorder(name, avg), y = avg)) + geom_point(size = 4, fill = 'lightblue', color = 'lightblue') + # 與默認主題theme_gray相對,背景空白 theme_bw() + theme(axis.text.x = element_text(angle = 60,hjust = 1),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(), panel.grid.major.x = element_line(colour = 'orange', linetype = 'dashed', size = 0.8)) + #設置線段到數據點出結束 geom_segment(aes(xend=name),yend=0,colour='lightblue', size = 1.5)對樣本分組繪圖
根據lg和avg對name進行排序,并將其轉換為因子水平的level
nameorder <- tophit$name[order(tophit$lg, tophit$avg)] tophit$name <- factor(tophit$name, levels = nameorder) ggplot(tophit, aes(x = avg, y =name)) + geom_segment(aes(yend=name, colour = lg),xend=0, size = 1.5) + # 按顏色分組 geom_point(size = 3, aes(colour = lg)) + scale_color_brewer(palette = "Set1", limits = c("NL", "AL")) + # 與默認主題theme_gray相對,背景空白 theme_bw() + theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(), panel.grid.major.y = element_blank(), # 將圖標移到圖的里面去legend.position=c(1, 0.55), legend.justification =c(1, 0.5))分面圖
ggplot(tophit, aes(x = avg, y =name)) + geom_segment(aes(yend=name),xend=0, size = 1.5, colour = 'grey50') + # 按顏色分組 geom_point(size = 3, aes(colour = lg)) + # 取消guide分組 scale_color_brewer(palette = "Set1", limits = c("NL", "AL"), guide = FALSE) + # 與默認主題theme_gray相對,背景空白 theme_bw() + theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(), panel.grid.major.y = element_blank())+# 分面圖facet_grid(lg~., scales = "free_y", space = "free_y")總結
以上是生活随笔為你收集整理的拿R来画画(六):很漂亮的Cleveland点图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 福建厦门:企业征信报告 可多渠道查询
- 下一篇: python自学行吗知乎_怎么自学pyt