<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>【rstats】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/rstats/feed/" rel="self" type="application/rss+xml" />
	<link>https://brain-storm.space</link>
	<description>脳や研究について発信するブログです。This site is for research and statistics.</description>
	<lastBuildDate>Mon, 13 Dec 2021 13:21:54 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>

<image>
	<url>https://brain-storm.space/wp-content/uploads/2021/04/cropped-3d0209af428738b78799159b4ce75ad9-32x32.png</url>
	<title>【rstats】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【ggplot2】Rで棒グラフを作成する</title>
		<link>https://brain-storm.space/bargraph/1025/</link>
					<comments>https://brain-storm.space/bargraph/1025/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Thu, 01 Jul 2021 04:33:36 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[bar graph]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[rstats]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1025</guid>

					<description><![CDATA[最もよく使われる棒グラフですが、その中でもいくつか使い分けが必要です。そのいくつかの種類の棒グラフの描き方を紹介します。 Contents データの準備棒グラフはgeom_bar()を使う色を付けるグループ化された棒グラ]]></description>
										<content:encoded><![CDATA[
<p>最もよく使われる棒グラフですが、その中でもいくつか使い分けが必要です。そのいくつかの種類の棒グラフの描き方を紹介します。</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1025" data-theme="jin-child">
			<div id="rtoc-mokuji-title" class=" rtoc_left">
			<button class="rtoc_open_close rtoc_open"></button>
			<span>Contents</span>
			</div><ol class="rtoc-mokuji decimal_ol level-1"><li class="rtoc-item"><a href="#rtoc-1">データの準備</a></li><li class="rtoc-item"><a href="#rtoc-2">棒グラフはgeom_bar()を使う</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">色を付ける</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-4">グループ化された棒グラフを作図する</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-5">積み上げ棒グラフ</a></li><li class="rtoc-item"><a href="#rtoc-6">グループ別の棒グラフ</a></li><li class="rtoc-item"><a href="#rtoc-7">幅を調整する</a></li><li class="rtoc-item"><a href="#rtoc-8">100％積み上げ棒グラフ</a></li><li class="rtoc-item"><a href="#rtoc-9">横向き棒グラフ</a></li><li class="rtoc-item"><a href="#rtoc-10">凡例の順番を入れ替える</a></li><li class="rtoc-item"><a href="#rtoc-11">色を変える</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-12">エラーバーをつける</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-13">縦棒グラフ＋エラーバー</a></li><li class="rtoc-item"><a href="#rtoc-14">横棒グラフ＋エラーバー</a></li></ul></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">データの準備</h2>



<p>まずはデータの準備をします。デモ用のデータとして最もよく使われるiris(アヤメ)データセットを今回も使います。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# 作業ディレクトリ
setwd(&quot;~/Rpractice/&quot;)

# tidyverseの起動
library(tidyverse)

#irisデータをaに格納
a &lt;- iris

#データの確認
&gt; head(a)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
&gt; 
</pre></div>


<p>それではこのirisデータを用いていくつかの棒グラフをつくっていきます。</p>



<h2 id="rtoc-2"  class="wp-block-heading">棒グラフはgeom_bar()を使う</h2>



<p>それではgeom_bar()を使っていきます。まずはアヤメデータの先頭にあるSepal.Lengthの値を使っていきます。</p>



<p>横軸にアヤメの種類(Species)、縦軸にSepal.Lengthをとります。値をy軸に取るときにはgeom_bar(stat = &#8220;identity&#8221;)とidentityを指定します。その他にはcountとbinを取ることが出来ます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(Species, Sepal.Length))+
  geom_bar(stat = &quot;identity&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph1-1024x1024.jpg" alt="" class="wp-image-1026" width="512" height="512"/></figure>



<div class="wp-block-jin-gb-block-box concept-box2">
<p>グラフを作成したら何が縦軸の値をとっているのか、しっかり確認することが必要です。元のデータと対比し、自分の出したい値が作図されているか確認しましょう。上の例では積み上げになっています。</p>
</div>



<h3 id="rtoc-3"  class="wp-block-heading">色を付ける</h3>



<p>塗りつぶしにはaesの中でfillを指定します。枠の色を指定したいときはcolourで指定します</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(Species, Sepal.Length, fill = Species))+
  geom_bar(stat = &quot;identity&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph2-1024x1024.jpg" alt="" class="wp-image-1027" width="512" height="512"/></figure>



<p>個数を出したいときはgeom_bar(stat = &#8220;count&#8221;)と指定します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Species, fill = Species))+
  geom_bar(stat = &quot;count&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph3-1024x1024.jpg" alt="" class="wp-image-1028" width="512" height="512"/></figure>



<h2 id="rtoc-4"  class="wp-block-heading">グループ化された棒グラフを作図する</h2>



<p>まず最初に平均値を計算します。合わせて、SDやSEも計算をしておくとエラーバーを付け加えることができます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# 縦長のグラフ
a.long &lt;- pivot_longer(a, col = Sepal.Length:Petal.Width,
                         names_to = &quot;iris.attr&quot;, 
                       values_to = &quot;measures&quot;)

# グループごとの平均値などの計算
a.sum &lt;- group_by(a.long, iris.attr, Species) %&gt;% 
  summarize_all(list(mean = mean,
                     max = max,
                     sd = sd, 
                     se = ~sd/sqrt(length(.))))

</pre></div>


<h3 id="rtoc-5"  class="wp-block-heading">積み上げ棒グラフ</h3>



<p>geom_barでpositionを指定しなければ積み上げ棒グラフになります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean,　fill = Species))+
  geom_bar(stat = &quot;identity&quot;)

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph3.5-1-1024x819.jpg" alt="" class="wp-image-1032" width="512" height="410"/></figure>



<h3 id="rtoc-6"  class="wp-block-heading">グループ別の棒グラフ</h3>



<p>それぞれグループを横に並べた棒グラフはgeom_barでposition = &#8220;dodge&#8221;と指定します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean,　fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph4-1-1024x819.jpg" alt="" class="wp-image-1030" width="512" height="410"/></figure>



<h3 id="rtoc-7"  class="wp-block-heading">幅を調整する</h3>



<p>横幅についてはwidthで調整をします。width = 0.5にしてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.long, aes(x = iris.attr, y =measures, fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot; , width = 0.5)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph3.8-1024x819.jpg" alt="" class="wp-image-1039" width="512" height="410"/></figure>



<h3 id="rtoc-8"  class="wp-block-heading">100％積み上げ棒グラフ</h3>



<p>実際の数値ではなく、割合で比較したい時に有用です。</p>



<p>geom_bar(position = &#8220;fill&#8221;)とすることで100％積み上げ棒グラフを作図できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean,　fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph5-1024x819.jpg" alt="" class="wp-image-1033" width="512" height="410"/></figure>



<h3 id="rtoc-9"  class="wp-block-heading">横向き棒グラフ</h3>



<p>横向きにするときにはcood_flip()を追加します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y =mean,　fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot;) +
  coord_flip()
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph6-1024x819.jpg" alt="" class="wp-image-1034" width="512" height="410"/></figure>



<h3 id="rtoc-10"  class="wp-block-heading">凡例の順番を入れ替える</h3>



<p>棒グラフの並びと凡例の並びが合わないときにはguides(fill = guide_legend(reverse = TRUE))を指定します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y =mean,　fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot;) +
  coord_flip()+
  guides(fill = guide_legend(reverse = TRUE))
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph7-1024x819.jpg" alt="" class="wp-image-1036" width="512" height="410"/></figure>



<h3 id="rtoc-11"  class="wp-block-heading">色を変える</h3>



<p>色を変える方法は色々とありますが、scale_fill_brewerで簡単にセット化されたものを使ってみます。</p>



<p>scale_fill_brewer(palette = &#8220;Set1&#8221;)と指定します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean, fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot; , width = 0.9)+
  scale_fill_brewer(palette = &quot;Set1&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph8-1024x819.jpg" alt="" class="wp-image-1037" width="512" height="410"/></figure>



<p>さらにscale_fill_brewer(palette = &#8220;Set2&#8221;)で色を変えてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean, fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot; , width = 0.9)+
  scale_fill_brewer(palette = &quot;Set2&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph9-1024x819.jpg" alt="" class="wp-image-1038" width="512" height="410"/></figure>



<h2 id="rtoc-12"  class="wp-block-heading">エラーバーをつける</h2>



<p>最後にエラーバーをつけます。</p>



<p>エラーバーをの付け方については、次のリンクで詳しく説明をしています。</p>



https://brain-storm.space/ggplot2_errorbar/895/



<h3 id="rtoc-13"  class="wp-block-heading">縦棒グラフ＋エラーバー</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean, fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot; , width = 0.9)+
  scale_fill_brewer(palette = &quot;Set1&quot;)+
    geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                  position = position_dodge(0.9), width = .2)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph10-1024x819.jpg" alt="" class="wp-image-1040" width="512" height="410"/></figure>



<h3 id="rtoc-14"  class="wp-block-heading">横棒グラフ＋エラーバー</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a.sum, aes(x = iris.attr, y = mean, fill = Species))+
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot; , width = 0.9)+
  scale_fill_brewer(palette = &quot;Set1&quot;)+
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                position = position_dodge(0.9), width = .2)+
  guides(fill = guide_legend(reverse = TRUE))+
  coord_flip()

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph11-1024x819.jpg" alt="" class="wp-image-1041" width="512" height="410"/></figure>



<p>お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/bargraph/1025/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
