項目が一つ
世代別加入者数
1st | 2nd | 3rd | 4th |
5 | 3 | 1 | 4 |
#1変数
number.group <- c(5,3,1,4)
names(number.group) <- c("1st","2nd","3rd","4th")
棒グラフ
#棒グラフ
barplot(number.group)
折れ線グラフ
#折れ線グラフ
matplot(number.group,type="o",pch=1)
- type="p"…点のみ,"l"…線のみ,"o"…線と点 など
- pch=マーカー(点)の形(0…□,1…○,2…△,3…+,4…×,5…◇など)
円グラフ
#円グラフ
pie(number.group)
帯グラフ
#帯グラフ
barplot(as.matrix(number.group/sum(number.group)),horiz=T,legend=names(number.group))
項目が二つ以上
グループ世代別加入者人数
グループ | 1st | 2nd | 3rd | 4th |
A | 5 | 3 | 1 | 4 |
B | 4 | 4 | 1 | 3 |
C | 4 | 4 | 3 | 1 |
#2変数以上
number.groups <- matrix(c(5,3,1,4,4,4,1,3,4,4,3,1),byrow=T,ncol=4)
colnames(number.groups) <- c("1st","2nd","3rd","4th")
rownames(number.groups) <- c("A","B","C")
names(dimnames(number.groups)) <- c("group","gen")
この表をグループ別に比較したい。
棒グラフ
#積み上げ棒グラフ
barplot(t(number.groups),legend=colnames(number.groups))
#集合棒グラフ(グループを先にまとめる)
barplot(t(number.groups),beside=T,legend=colnames(number.groups))
- t(X)…行列の入れ替え
- biside = T(Tなら棒を横に配置)
- legend = 凡例
#集合棒グラフ(世代を先にまとめる)
barplot(number.groups,beside=T,legend=rownames(number.groups))
- biside = T(Tなら棒を横に配置)
- legend = 凡例
折れ線グラフ
#折れ線グラフ
matplot(t(number.groups),type="o",pch=c(1,2,3),xaxt="n") #折れ線グラフ
axis(side=1,at=1:length(colnames(number.groups)),labels=colnames(number.groups))
legend("bottomleft",legend=rownames(number.groups),col=c(1,2,3),lty=c(1,2,3),pch=c(1,2,3))
- matplot←折れ線グラフを描画
- axis←x軸に項目名を指定している
- legend←凡例
帯グラフ
#帯グラフ(グループ別構成比率)
barplot(prop.table(t(number.groups),margin=2),horiz=T,legend=colnames(number.groups))
- prop.table(X)…比率計算
- t(X)…行列入れ替え
- horiz…横棒
- legend…凡例
#帯グラフ(世代別構成比率)
barplot(prop.table(number.groups,margin=2),horiz=T,legend=rownames(number.groups))
- prop.table(X)…比率計算
- horiz…横棒
- legend…凡例
散布図
氏名 | グループ | 身長 | 年齢 |
安倍なつみ | モーニング娘。 | 152 | 35 |
飯窪春菜 | モーニング娘。 | 160 | 21 |
飯田圭織 | モーニング娘。 | 168 | 35 |
生田衣梨奈 | モーニング娘。 | 159 | 19 |
… |
data <- read.csv("http://kyoto-edu.sakura.ne.jp/weblesson/statistics/data/height-age.csv", fileEncoding = "utf-8")
#散布図
plot(身長~年齢,data) #年齢と身長の散布図
- 変数Y~変数X…Y軸とX軸に指定する変数名
- data…変数名を含むデータ