理学療法士がまとめたノート

統計学備忘録(R言語のメモ)

since2016 ときどきTEXのメモ

ヒストグラム

library(ggplot2)

data <- c(rnorm(n=100, mean=5, sd=1),rnorm(n=100, mean=3, sd=3))
cat <- c(rep("x",100), rep("y",100))
dat1 <- data.frame(cat, data)
colnames(dat1) <- c("name","value")
#乱数なのでデータは変わります

単純なヒストグラム

HG1= ggplot(dat1, aes(x = value)) + 
geom_histogram(binwidth = 1)
plot(HG1)

f:id:yoshida931:20220206224947p:plain:w400

color=枠、fill=塗りつぶし、alpha=透かし (0-1)

HG2= ggplot(dat1, aes(x = value)) + 
geom_histogram(binwidth = 1, color="black", fill="grey", alpha=0.6)
plot( HG2 ) 

f:id:yoshida931:20220206225103p:plain:w400

各グループで色分け、凡例の位置(position)

HG3= ggplot(dat1, aes(x = value, color = name, fill =name)) + 
geom_histogram(position = "identity", binwidth = 1, alpha=0.6)
plot(HG3) 

f:id:yoshida931:20220206225224p:plain:w400

枠と塗りつぶしの色を変更

HG4= HG3 + scale_color_manual(values = c("red", "black")) +
scale_fill_manual(values = c("red", "grey"))
plot(HG4) 

f:id:yoshida931:20220206225311p:plain:w400

背景の操作1

HG5= HG4 + theme_bw() 
plot(HG5) 

f:id:yoshida931:20220206225406p:plain:w400

背景の操作2

HG6= HG4 + theme_void()
plot(HG6) 

f:id:yoshida931:20220206225502p:plain:w400

背景の操作3

HG7= HG4 + theme_minimal()
plot(HG7) 

f:id:yoshida931:20220206225600p:plain:w400

背景の操作4

HG8= HG4 + theme_classic()
plot(HG8) 

f:id:yoshida931:20220206225713p:plain:w400