【R】ggplotで散布図を書く時のチートシート

こんてんつ

ggplotで散布図を書く時の備忘録である。

  • 基本編
  • 条件合致のみプロット編
  • 軸の調整編
  • 軸ラベルやタイトル編
  • 点の見た目の変更編
  • 色の調整編
  • 並べてプロット編
  • 回帰直線編

チートシート

基本編

##Species毎に散布図を書く
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

##Petal.Widthで色付けして散布図を書く
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
  geom_point()

条件合致のみプロット編

##Petal.Widthが0.4以上、1.8以下のものを抜き出して散布図を書く
iris %>%
  dplyr::filter(Petal.Width >= 0.40, Petal.Width <= 1.8) %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

##virginicaのみを抜き出して散布図を書く
iris %>%
  dplyr::filter(Species == "virginica") %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

軸の調整編

##軸の範囲を変更する(枠外も考慮するため推奨)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  coord_cartesian(xlim=c(4,10),ylim=c(0,15))

##軸の範囲を変更する(枠外を無視するため非推奨)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  ylim(0,15)+
  xlim(4,10)

##軸の範囲と区切り線の単位を変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  coord_cartesian(xlim=c(4,10),ylim=c(0,15))+
  scale_x_continuous(breaks = seq(4,10,by=0.5))+
  scale_y_continuous(breaks = seq(0,15,length=16))

軸ラベルやタイトル編

##軸のラベルを変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  labs(x = "NEW X LABEL", y = "NEW Y LABEL", color = "NEW COLOR LABEL")

##軸のラベルを消す
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  theme(axis.title.x = element_blank(),axis.title.y = element_blank())

##グラフのタイトルを変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  labs(title = "NEW TITLE 1")

##グラフのタイトルを変更して位置を調整する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  ggtitle("NEW TITLE 2")+
  theme(plot.title = element_text(hjust = 0.5, vjust = -10))

点の見た目の変更編

##点のサイズと形状を変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point(shape = 4, size = 3)

色の調整編

##自分で指定した任意の色でプロットする①
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  scale_color_manual(values = c(setosa = "#FF4B00",
                                versicolor = "#005AFF",
                                virginica = "#03AF7A" ))

##自分で指定した任意の色でプロットする②
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  scale_color_manual(values = c("#44AA99","#999933","#CC6677"))


##colorの色分けを自分で決めたカラーパレットにする
pal <- c("#3B9AB2","#53A5B9","#6BB1C1","#8FBBA5","#BDC367",
         "#EBCC2A","#E6C019","#E3B408","#E49100","#EB5500","#F21A00")
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
  geom_point() +
  scale_color_gradientn(colors = pal) 
                  
##色付けscale_color_gradientnの範囲を定める
pal <- c("#3B9AB2","#53A5B9","#6BB1C1","#8FBBA5","#BDC367",
         "#EBCC2A","#E6C019","#E3B408","#E49100","#EB5500","#F21A00")
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
  geom_point() +
  scale_color_gradientn(colors = pal,limits=c(0.5,2.0)) 

並べてプロット編

##Species毎にgridで並べる(gridの横軸にSpecies)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  facet_grid(. ~ Species, scales = "fixed")

##Species毎にgridで並べる(gridの縦軸にSpecies)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  facet_grid(Species ~ ., scales = "free")

##Species毎に別々の図で並べる
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  facet_wrap(~Species, nrow=2, scales = "free_y")

回帰直線編

##それっぽい回帰直線を引く
library(ggpmisc)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  geom_smooth(method="lm",aes(color=Species),se=TRUE)+
  stat_poly_eq(formula = y ~ x,
                 aes(label = paste(stat(eq.label))),
                 parse = TRUE)

##相関係数を載せる
library(ggpmisc)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  geom_smooth(method="lm",aes(color=Species),se=TRUE)+
  stat_poly_eq(aes(label = paste(stat(rr.label))),
               label.x = "right",label.y = "bottom",
               parse = TRUE)

##回帰直線の式と相関係数を両方載せる
library(ggpmisc)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  geom_smooth(method="lm",aes(color=Species),se=TRUE)+
  stat_poly_eq(formula = y ~ x,
               aes(label = paste(stat(eq.label))),
               parse = TRUE)+
  stat_poly_eq(aes(label = paste(stat(rr.label))),
               label.x = "right",label.y = "bottom",
               parse = TRUE)

その他

##データの値を1000倍して散布図を書く
iris %>%
  mutate(Sepal.Length = Sepal.Length * 1000,
         Petal.Length = Petal.Length * 1000) %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

実施例

基本編

##Species毎に散布図を書く
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

##Petal.Widthで色付けして散布図を書く
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
  geom_point()

条件合致のみプロット編

##Petal.Widthが0.4以上、1.8以下のものを抜き出して散布図を書く
iris %>%
  dplyr::filter(Petal.Width >= 0.40, Petal.Width <= 1.8) %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

##virginicaのみを抜き出して散布図を書く
iris %>%
  dplyr::filter(Species == "virginica") %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()

軸の調整編

##軸の範囲を変更する(枠外も考慮するため推奨)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  coord_cartesian(xlim=c(4,10),ylim=c(0,15))

##軸の範囲を変更する(枠外を無視するため非推奨)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  ylim(0,15)+
  xlim(4,10)

##軸の範囲と区切り線の単位を変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  coord_cartesian(xlim=c(4,10),ylim=c(0,15))+
  scale_x_continuous(breaks = seq(4,10,by=0.5))+
  scale_y_continuous(breaks = seq(0,15,length=16))

軸ラベルやタイトル編

##軸のラベルを変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  labs(x = "NEW X LABEL", y = "NEW Y LABEL", color = "NEW COLOR LABEL")

##軸のラベルを消す
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  theme(axis.title.x = element_blank(),axis.title.y = element_blank())

##グラフのタイトルを変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  labs(title = "NEW TITLE 1")

##グラフのタイトルを変更して位置を調整する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  ggtitle("NEW TITLE 2")+
  theme(plot.title = element_text(hjust = 0.5, vjust = -10))

点の見た目の変更編

##点のサイズと形状を変更する
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point(shape = 4, size = 3)

色の調整編

##自分で指定した任意の色でプロットする①
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  scale_color_manual(values = c(setosa = "#FF4B00",
                                versicolor = "#005AFF",
                                virginica = "#03AF7A" ))

##自分で指定した任意の色でプロットする②
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  scale_color_manual(values = c("#44AA99","#999933","#CC6677"))

##colorの色分けを自分で決めたカラーパレットにする
pal <- c("#3B9AB2","#53A5B9","#6BB1C1","#8FBBA5","#BDC367",
         "#EBCC2A","#E6C019","#E3B408","#E49100","#EB5500","#F21A00")
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
  geom_point() +
  scale_color_gradientn(colors = pal) 

##色付けscale_color_gradientnの範囲を定める
pal <- c("#3B9AB2","#53A5B9","#6BB1C1","#8FBBA5","#BDC367",
         "#EBCC2A","#E6C019","#E3B408","#E49100","#EB5500","#F21A00")
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
  geom_point() +
  scale_color_gradientn(colors = pal,limits=c(0.5,2.0)) 

並べてプロット編

##Species毎にgridで並べる(gridの横軸にSpecies)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  facet_grid(. ~ Species, scales = "fixed")

##Species毎にgridで並べる(gridの縦軸にSpecies)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  facet_grid(Species ~ ., scales = "free")

##Species毎に別々の図で並べる
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  facet_wrap(~Species, nrow=2, scales = "free_y")

回帰直線編

##それっぽい回帰直線を引く
library(ggpmisc)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  geom_smooth(method="lm",aes(color=Species),se=TRUE)+
  stat_poly_eq(formula = y ~ x,
                 aes(label = paste(stat(eq.label))),
                 parse = TRUE)

##相関係数を載せる
library(ggpmisc)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  geom_smooth(method="lm",aes(color=Species),se=TRUE)+
  stat_poly_eq(aes(label = paste(stat(rr.label))),
               label.x = "right",label.y = "bottom",
               parse = TRUE)

##回帰直線の式と相関係数を両方載せる
library(ggpmisc)
iris %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()+
  geom_smooth(method="lm",aes(color=Species),se=TRUE)+
  stat_poly_eq(formula = y ~ x,
               aes(label = paste(stat(eq.label))),
               parse = TRUE)+
  stat_poly_eq(aes(label = paste(stat(rr.label))),
               label.x = "right",label.y = "bottom",
               parse = TRUE)

その他

##データの値を1000倍して散布図を書く
iris %>%
  mutate(Sepal.Length = Sepal.Length * 1000,
         Petal.Length = Petal.Length * 1000) %>%
  ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
  geom_point()