2
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで色々な棒グラフを作成してみた

Last updated at Posted at 2018-01-10

ggplot2の基礎知識は岩嵜 航 氏のサイトへ

解析環境

OS: Windows 10
App: R Version 3.4.3

図にしたいデータ

# Clusters x.stage y.stage z.stage
1 black 91 190 127
2 green 163 564 258
3 turquoise 318 420 149
4 red 200 381 295

出典:
https://www.biostars.org/p/92218/

必要なlibraryは2つだけ

library(ggplot2)
library(reshape2)

Rにデータを読み込ませるよ

dat=read.table(header=T, text="Clusters x-stage y-stage    z-stage
+ black         91         190    127
+ green        163         564    258
+ turquoise    318         420    149
+ red          200         381     295")

データを整形して使いやすくする

dm=melt(dat,id.var=c("Clusters"))

Clusters variable value
1 black x.stage 91
2 green x.stage 163
3 turquoise x.stage 318
4 red x.stage 200
5 black y.stage 190
6 green y.stage 564
7 turquoise y.stage 420
8 red y.stage 381
9 black z.stage 127
10 green z.stage 258
11 turquoise z.stage 149
12 red z.stage 295

x軸にvariable、y軸にvalueの棒グラフ(Cluster分は統合)

p1 <- ggplot(dm, aes(variable, value, fill=variable)) + 
theme_bw() + geom_histogram(stat="identity")

image.png

x軸にvariable、y軸にvalue、Clustersの積み上げ棒グラフ

p1 <- ggplot(dm, aes(variable, value, fill=Clusters)) + 
theme_bw() + geom_bar(stat="identity")

image.png

x軸にvariable、y軸にvalue、Clustersを横並びにした棒グラフ

p1 <- ggplot(dm, aes(variable, value, fill=Clusters)) + theme_bw() + 
geom_bar(stat="identity", position = "dodge")

image.png

x軸にvariable, y軸にvalueを共通としたClusterごとの棒グラフ

参考
https://www.biostars.org/p/92218/

p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() +
  geom_histogram(stat="identity") + facet_grid(. ~ Clusters)

image.png

横にして数値を表記させる

参考

p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() + 
geom_histogram(stat="identity") + facet_grid(. ~ Clusters) + coord_flip() + 
geom_text(aes(variable, value, label = sprintf("%2.1f", value)),
position = position_dodge(width = 1))

image.png

y軸の並べ方を変更する

参考
https://stackoverflow.com/questions/34227967/reversed-order-after-coord-flip-in-r

p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() + 
geom_histogram(stat="identity") + facet_grid(. ~ Clusters) + coord_flip() + 
geom_text(aes(label = sprintf("%2.1f", value)),
position = position_dodge(width = 1)) + scale_x_discrete(limits = rev(levels(dm$variable)))

image.png

labelの位置を揃えて数値を見やすくする

p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() + 
geom_histogram(stat="identity") + 
facet_grid(. ~ Clusters) + 
coord_flip() + 
geom_text(aes(label = format(value, digits = 4), y = 0.7)) + 
scale_x_discrete(limits = rev(levels(dm$variable)))

image.png

※ 他のデータでは右端に揃うのにこのデータでは左端にめり込んでしまう

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?