こんてんつ
ggplotで散布図を書く時の備忘録である。
- 基本編
- 条件合致のみプロット編
- 軸の調整編
- 軸ラベルやタイトル編
- 点の見た目の変更編
- 色の調整編
- 並べてプロット編
- 回帰直線編
基本編
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
geom_point()
条件合致のみプロット編
iris %>%
dplyr::filter(Petal.Width >= 0.40, Petal.Width <= 1.8) %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()
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"))
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)
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))
並べてプロット編
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()+
facet_grid(. ~ Species, scales = "fixed")
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()+
facet_grid(Species ~ ., scales = "free")
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)
その他
iris %>%
mutate(Sepal.Length = Sepal.Length * 1000,
Petal.Length = Petal.Length * 1000) %>%
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()
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Petal.Width))+
geom_point()
条件合致のみプロット編
iris %>%
dplyr::filter(Petal.Width >= 0.40, Petal.Width <= 1.8) %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()
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"))
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)
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))
並べてプロット編
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()+
facet_grid(. ~ Species, scales = "fixed")
iris %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()+
facet_grid(Species ~ ., scales = "free")
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)
その他
iris %>%
mutate(Sepal.Length = Sepal.Length * 1000,
Petal.Length = Petal.Length * 1000) %>%
ggplot(aes(x=Sepal.Length, y=Petal.Length, color = Species))+
geom_point()