Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@carta5

# ggplotの使い方メモ

More than 1 year has passed since last update.

Rにてggplotを使う際の忘備録を残しておきます

## 基本的なグラフの描画

ggplotにもとから入っているdiamondsデータを用いて基本的なグラフを描画します

### 棒グラフ

bar.R
```ggplot(data=diamonds)+
geom_bar(mapping = aes(x = cut))
```

### 箱ひげ図

boxplot.R
```ggplot(data = diamonds)+
geom_boxplot(mapping = aes(x=color,y=price))
```

### ヒストグラム

hist.R
```ggplot(data=diamonds)+
geom_histogram(mapping=aes(x=price))
```

### 散布図

scatter.R
```ggplot(data = diamonds)+
geom_boxplot(mapping = aes(x=carat,y=price))
```

## おまけ

### 省略方法

dataやmappingといったものは省略することが出来ます
また，同じデータを用いて色々なグラフを描画する際にはこうやって書くと便利です

syouryaku.R
```#箱ひげ図の例
gg = ggplot(diamonds,aes(x=carat,y=price))
gg + geom_boxplot()
```

### 色でわける

aesの中にcolorやfillを指定することでグラフを色分けすることが出来ます
colorとfillはグラフによってわかりやすさが変わるので使い分けが必要です

color.R
```ggdia = ggplot(diamonds,aes(x=carat,y=price))
#colorで色分け
ggdia +geom_boxplot(aes(color = cut))
```

fill.R
```ggdia = ggplot(data =diamonds)

#fillで色分け
ggdia + geom_histogram(aes(price,fill = clarity))

```

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away