#install.packages( "datarium" )
library( datarium )
job = jobsatisfaction
mkt = marketing
job = as.data.frame( jobsatisfaction )
str(job)
## 'data.frame': 58 obs. of 4 variables:
## $ id : Factor w/ 58 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ gender : Factor w/ 2 levels "male","female": 1 1 1 1 1 1 1 1 1 1 ...
## $ education_level: Factor w/ 3 levels "school","college",..: 1 1 1 1 1 1 1 1 1 2 ...
## $ score : num 5.51 5.65 5.07 5.51 5.94 5.8 5.22 5.36 4.78 6.01 ...
dat = c( 1, 2, 4, 8, 16, 32 )
plot ( dat )
hist() 関数を使うことでヒストグラムが作成できます。 breaks を指定することでビンの数を変更できます。
hist(
mkt$sales,
main = "Sales",
xlab = "Bin",
col = "#2792b3", # 斜線の色
density = 30, # 斜線の密度
angle = 30, # 斜線の角度
border = "gray100", # 棒の枠線の色
labels = TRUE, # 棒の高さの値を表示するかとか
ylim = c( 0, 80 ),
cex.main = 1.5,
cex.lab = 1.5,
cex.axis = 1.5
)
## ggplot2 パッケージによる作図
#install.packages( "ggplot2" )
#install.packages( "cowplot" )
#install.packages( "RColorBrewer" )
library( ggplot2 )
library( Hmisc )
## 要求されたパッケージ lattice をロード中です
## 要求されたパッケージ survival をロード中です
## 要求されたパッケージ Formula をロード中です
##
## 次のパッケージを付け加えます: 'Hmisc'
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## format.pval, units
library( cowplot )
library( RColorBrewer )
最小限の要素のみ 1要因の場合
ggplot(
job,
aes( x = education_level, y = score) ) +
stat_summary( fun = "mean", geom = "bar"
)
# 集合棒グラフ
ggplot( job, aes( x = education_level, y = score, fill = gender ) ) +
stat_summary( fun = "mean", geom = "bar", position = position_dodge() )
# 積み上げ棒グラフ
ggplot( job, aes( x = education_level, y = score, fill = gender ) ) +
stat_summary( fun = "mean", geom = "bar", position = position_stack() )
1 要因の場合
# 色分け
ggplot( job, aes( x = education_level, y = score, fill = education_level ) ) +
stat_summary( fun = "mean", geom = "bar" )
# エラーバーを追加
ggplot( job, aes( x = education_level, y = score, fill = education_level ) ) +
stat_summary( fun.y = "mean", geom = "bar" ) +
stat_summary( fun.data = mean_cl_normal, geom = "errorbar",
alpha = 0.8, size = 1, width = 0.1 )
## Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun` argument instead.
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
# 凡例を消す、棒の枠線を追加
ggplot( job, aes( x = education_level, y = score, fill = education_level ) ) +
stat_summary( fun.y = "mean", geom = "bar", color = "black" ) +
theme( legend.position = "none" )
# 棒だけ
ggplot( job, aes( x = education_level, y = score, fill = gender ) ) +
stat_summary( fun = "mean", geom = "bar", position = position_dodge() )
# エラーバーを追加
ggplot( job, aes( x = education_level, y = score, fill = education_level ) ) +
stat_summary( fun.y = "mean", geom = "bar" ) +
stat_summary( fun.data = mean_cl_normal, geom = "errorbar",
alpha = 0.8, size = 1, width = 0.1 )
# 色を変更
ggplot( job, aes( x = education_level, y = score, fill = gender ) ) +
stat_summary( fun.y = "mean", geom = "bar", position = position_dodge() ) +
stat_summary( fun.data = mean_cl_normal, geom = "errorbar", alpha = 0.8, size = 1,
width = 0.1, position = position_dodge( width = .9 ) ) +
scale_fill_manual( values = c( "#5B84B1FF", "#FC766AFF" ) )
library( cowplot )
library( RColorBrewer )
ggplot( job, aes( x = education_level, y = score, fill = gender ) ) +
stat_summary( fun.y = "mean", geom = "bar", position = position_dodge() ) +
stat_summary( fun.data = mean_cl_normal, geom = "errorbar",
alpha = 0.7, size = 1, width = 0.2, position = position_dodge( width = 0.9 ) ) +
scale_y_continuous( limits = c( 0, 12 ), breaks = 0:4 * 3, expand = c( 0, 0 ) ) +
theme_cowplot( 26 ) +
scale_fill_manual( values = c( "#5B84B1FF", "#FC766AFF" ) ) +
xlab( "Education level" ) +
ylab( "Score" )