日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

拿R来画画(六):很漂亮的Cleveland点图

發布時間:2024/3/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 拿R来画画(六):很漂亮的Cleveland点图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 簡單粗暴堆點
    • 各點有序上升下降
    • 對樣本分組繪圖

這種圖主要是針對一一對應的關系,簡潔明了,圖形的可讀性很強,因為條形圖在空間上是二維的,不容易直觀建立一一對應的變量關聯。

簡單粗暴堆點

library(gcookbook) library(ggplot2) tophit <- tophitters2001[1:25,] head(tophit) A data.frame: 6 × 26 idfirstlastnameyearstintteamlggab...sbcsbbsoibbhbpshsfgidpavg<fct><chr><chr><chr><int><int><fct><fct><int><int>...<int><int><int><int><int><int><int><int><int><dbl>
walkela01Larry WalkerLarry Walker 20011COLNL142497...14 5 82103 61408 90.3501
suzukic01Ichiro SuzukiIchiro Suzuki 20011SEAAL157692...5614 30 5310 844 30.3497
giambja01Jason GiambiJason Giambi 20011OAKAL154520... 2 0129 83241309170.3423
alomaro01RobertoAlomarRoberto Alomar20011CLEAL157575...30 6 80 71 5 499 90.3357
heltoto01Todd HeltonTodd Helton 20011COLNL159587... 7 5 9810415 515140.3356
aloumo01 Moises Alou Moises Alou 20011HOUNL136513... 5 1 57 5714 308180.3314
ggplot(tophit, aes(x = avg, y = name)) + geom_point()

各點有序上升下降

觀察一下上面的圖可以發現,點上升得很凌亂,沒有規律。因為這里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点图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。