1 ggplotオブジェクト作成

2 外観の変更

3 散布図geom_point

geom_point(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", ..., na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE)
  
# 散布図 
scp <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_point() + theme_bw() 

# カテゴリで分けてplot (色は自動) 
scp_1 <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour = Species)) +
  geom_point() + 
  theme_bw() + 
  labs(title="aes(colour)")

# 色を指定する 
scp_2 <- scp_1 +
  geom_point() +
  scale_color_manual(values = c("#1B9E77", "#D95F02", "#7570B3")) +
  labs(title="scale_color_manual()")

# 形,サイズ変更, 透過度の変更 
scp_3 <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour = Species, shape = Species)) +
  geom_point(size=5, alpha=0.3) + 
  theme_bw() + 
  labs(title="aes(shape = Species)") 

# 形を指定 scale_shape_manual
scp_4 <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour = Species, shape = Species)) +
  geom_point(size=5) + 
  scale_shape_manual(values = c(0:2)) +
  theme_bw() + 
  labs(title="scale_shape_manual()") 

# aesのオプションでサイズ変更(値を反映させる)  
scp_5 <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour = Species)) +
  geom_point(aes(size=Petal.Length), alpha = 0.5) + 
  theme_bw() +
  labs(title="aes(size=Petal.Length)")

# x,y軸変換 
scp_6 <- scp_1 + coord_flip() +
        labs(title="coord_flip()")

# ylim xlim 
scp_7 <- scp_1 + coord_cartesian(xlim = c(1, 2.5), ylim = c(3,7)) +
        labs(title="coord_cartesian(xlim, ylim)")

# 散布図 + 回帰直線 
scp_8 <- scp + 
        geom_point(aes(colour=Species)) + 
        geom_smooth(show.legend = T, method = "glm", 
                    linetype=3, size=0.5, colour="magenta") +
        labs(title="geom_smooth()")   

# まとめて描画 
library(gridExtra)
grid.arrange(scp, scp_1, scp_2, scp_3, scp_4, scp_5, scp_6, scp_7, scp_8, ncol=3)

4 項目名を文字列で与える場合aes_string()

5 グラフにテキスト追加 geom_text

6 凡例labs, theme, guides

scp <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour = Species)) + geom_point() + theme_bw() 


# レジェンドの位置変更 
leg_1 <- scp +
  theme(legend.position = "top") +
  labs(subtitle = "theme(legend.position=\"top\")")

# レジェンドの位置(座標で指定) 
leg_2 <- scp +
  theme(legend.position = c(0.9,0.25),
        legend.background = element_blank()) +
  labs(subtitle = "theme(legend.position = c(0.9,0.25))")

# レジェンドタイトルテキストサイズ 
leg_3 <- scp +
  theme(legend.title = element_text(size=20)) +
  labs(subtitle="theme(legend.title = element_text(size=20)")

# レジェンドテキストサイズ, キーサイズ 
leg_4 <- scp +
  theme(legend.text = element_text(size=15), 
        legend.key.size = unit(1, "lines")) +
  labs(subtitle="theme(legend.text = element_text(size=15)")

# レジェンドタイトルラベル変更
leg_5 <- scp +
  labs(subtitle="labs(title,x,y,colour)", x = "PL", y = "PW", colour = "SP")

# ラベルを消す
leg_6 <- leg_5 +
  labs(subtitle="labs(x=NULL, y = NULL, colour = NULL)", x = NULL, y = NULL, colour = NULL)

# レジェンドのテキスト変更
leg_7 <- scp + 
  scale_color_discrete(labels = c("Set","Ver", "Vir")) +
  labs(subtitle="scale_color_discrete(labels = c('Set','Ver', 'Vir')")

# 凡例を複数列表示
leg_8 <- scp +
  guides(colour = guide_legend(nrow = 2)) +
  labs(subtitle="guides(colour = guide_legend(nrow = 1))")

# 凡例の設定変更  
leg_9 <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour=Species)) +
  geom_point(size=3, alpha=0.3) + 
  theme_bw() +
  guides(colour = guide_legend(override.aes = list(alpha=1, size = 5))) +
  labs(subtitle="guides(colour = guide_legend(override.aes = list(alpha=1, size = 5))))")


grid.arrange(leg_1,leg_2,leg_3,leg_4,leg_5, leg_6, leg_7, leg_8, leg_9, ncol=3)

7 信頼区間stat_ellipse

8 配置

9 ボックスプロットgeom_boxplot

10 ヒストグラム geom_histogram

geom_histogram(mapping = NULL, data = NULL, stat = "bin",
  position = "stack", ..., binwidth = NULL, bins = NULL, na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE)
  

11 ドットプロットgeom_dotplot

geom_dotplot(mapping = NULL, data = NULL, position = "identity", ...,
  binwidth = NULL, binaxis = "x", method = "dotdensity",
  binpositions = "bygroup", stackdir = "up", stackratio = 1,
  dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE,
  width = 0.9, drop = FALSE, na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE)
  

12 折れ線グラフgeom_line

13 棒グラフgeom_bar

13.1 x軸のみ指定で度数を表示

13.3 facet, 積み上げ棒グラフ

  • facet_grid(variable ~ .)
  • facet_grid(. ~ variable)
  • facet_wrap(~variable, ncol=2)
gatherを使ってunpivot
Species keys values
setosa Sepal.Length 250.3
versicolor Sepal.Length 296.8
virginica Sepal.Length 329.4
setosa Sepal.Width 171.4
versicolor Sepal.Width 138.5
virginica Sepal.Width 148.7
setosa Petal.Length 73.1
versicolor Petal.Length 213.0
virginica Petal.Length 277.6
setosa Petal.Width 12.3
versicolor Petal.Width 66.3
virginica Petal.Width 101.3

14 ヒートマップgeom_tile

15 ggplot2のデフォルトカラーコード

16 GGally

17 環境

## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.1
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] GGally_1.4.0   bindrcpp_0.2.2 ggrepel_0.8.0  dplyr_0.7.8   
## [5] gridExtra_2.3  ggplot2_3.1.0 
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.0         RColorBrewer_1.1-2 highr_0.7         
##  [4] pillar_1.3.1       compiler_3.5.1     plyr_1.8.4        
##  [7] bindr_0.1.1        tools_3.5.1        digest_0.6.18     
## [10] viridisLite_0.3.0  evaluate_0.12      tibble_2.0.0      
## [13] gtable_0.2.0       pkgconfig_2.0.2    rlang_0.3.1       
## [16] yaml_2.2.0         withr_2.1.2        stringr_1.3.1     
## [19] knitr_1.20         rprojroot_1.3-2    grid_3.5.1        
## [22] tidyselect_0.2.5   reshape_0.8.8      glue_1.3.0        
## [25] R6_2.3.0           rmarkdown_1.10     pacman_0.5.0      
## [28] reshape2_1.4.3     purrr_0.2.5        tidyr_0.8.2       
## [31] magrittr_1.5       MASS_7.3-51.1      backports_1.1.2   
## [34] scales_1.0.0       htmltools_0.3.6    assertthat_0.2.0  
## [37] colorspace_1.3-2   labeling_0.3       stringi_1.2.4     
## [40] lazyeval_0.2.1     munsell_0.5.0      crayon_1.3.4