---
disqus: ahb0222
GA : G-CQ4L16KHK4
---
# Rstudio 繪圖裝置修改-加速9倍ggplot2繪圖速度
> [color=#40f1ef][name=LHB阿好伯, 2021/07/16][:earth_africa:](https://www.facebook.com/LHB0222/)
###### tags: `R` `可視化`
[TOC]
今天來分享一個加快ggplot2繪圖的方法
只需要簡單的修改Rstudio設定中的繪圖系統即可
在暴力測試隨機一千萬筆的散佈圖中可以將等待時間由18分鐘縮短為2分鐘
整整快了9倍超猛
# 各式繪圖裝置性能測試
## 預設
```r=
install.packages("profvis")
library(profvis)
install.packages("ggplot2")
library(ggplot2)
profvis({
g <- ggplot(diamonds, aes(carat, price)) + geom_point(size = 1, alpha = 0.2)
print(g)
})
```

耗時

## AGG
只要一個簡單的步驟修改Rstudio的繪圖裝置
主要有AGG或Cairo可以選
測試過後是發現AGG的穩定叫好沒出現錯誤
而Cairo速度會快一些些



## Cairo


# 上色測試
```r=
profvis({
for (i in c(1:3)) {
g <- ggplot(diamonds, aes(carat, price,colour = cut)) + geom_point(size = 1, alpha = 0.2)
print(g)
print(i)
}
})
```
# 預設


# AGG


連續繪製三次

## Cairo


連續繪製三次

# 分面繪製
# 預設


# AGG


## Cairo


# 輸出多張圖性能
## 預設

## AGG

## Cairo

# 十萬散佈圖測試
```r=
df <- data.frame(x1 = sample(100000), y1 = sample(100000),z1 = sample(c(1:3),10000,replace = T))
df
profvis({
g <- ggplot(df, aes(x1, y1,colour = z1)) + geom_point(size = 1, alpha = 0.2)
print(g)
})
```
## 預設


# AGG


## Cairo


# 百萬散佈圖測試
```r=
df <- data.frame(x1 = sample(1000000), y1 = sample(1000000),z1 = sample(c(1:3),100000,replace = T))
df
profvis({
g <- ggplot(df, aes(x1, y1,colour = z1)) + geom_point(size = 1, alpha = 0.2)
print(g)
})
```
## 預設


# AGG


## Cairo


# 千萬散佈圖測試
## 預設
整整花了18分鐘崩潰XD


改用其他兩個繪圖裝置都縮減到2分鐘完成
## AGG


## Cairo


Cairo遇到的崩況問題

🌟全文可以至下方連結觀看或是補充
https://hackmd.io/@LHB-0222/GraphicsDevice
全文分享至
https://www.facebook.com/LHB0222/
https://www.instagram.com/ahb0222/
有疑問想討論的都歡迎於下方留言
喜歡的幫我分享給所有的朋友 \o/
有所錯誤歡迎指教
# [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings)
