Help us understand the problem. What is going on with this article?

ggplotの使い方メモ

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

基本的なグラフの描画

基本的にはデータを指定してあとは好きなグラフを指定するという仕組み
ggplotにもとから入っているdiamondsデータを用いて基本的なグラフを描画します

棒グラフ

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

bar.png

箱ひげ図

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

box.png

ヒストグラム

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

hist.png

散布図

横軸:色,縦軸:価格としています

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

scatter.png

おまけ

省略方法

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))

irowake1.png

fill.R
ggdia = ggplot(data =diamonds)

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

irowake2.png

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした