<?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>【violin】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/violin/feed/" rel="self" type="application/rss+xml" />
	<link>https://brain-storm.space</link>
	<description>脳や研究について発信するブログです。This site is for research and statistics.</description>
	<lastBuildDate>Fri, 24 Mar 2023 12:07:33 +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>【violin】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【R初心者向け】ggplot2でバイオリンプロット(violin plot)を描く</title>
		<link>https://brain-storm.space/violin-plot/871/</link>
					<comments>https://brain-storm.space/violin-plot/871/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Mon, 31 May 2021 06:50:12 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[violin]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=871</guid>

					<description><![CDATA[データを可視化するときにいくつかの方法があります。その一つがバイオリンプロットです。 バイオリンプロットは複数のデータを比較する時に便利な方法です。バイオリンプロットを書いているだけで、「この人出来る」というような印象が]]></description>
										<content:encoded><![CDATA[
<p>データを可視化するときにいくつかの方法があります。その一つがバイオリンプロットです。</p>



<p><span class="marker">バイオリンプロットは複数のデータを比較する時に便利な方法です</span>。バイオリンプロットを書いているだけで、「この人出来る」というような印象があるかもしれません。是非チャンスがあれば使ってみてください。</p>



<p>それでは書き方にいきます。ついでにドットプロットでデータの分布を確認してみます。</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="871" 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">バイオリンプロットを描く</a></li><li class="rtoc-item"><a href="#rtoc-3">バイオリンに色をつける</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-4">枠に色を付ける</a></li><li class="rtoc-item"><a href="#rtoc-5">バイオリンの中に色を塗る</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-6">平均値や中央値を入れる</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-7">平均値をマークする</a></li><li class="rtoc-item"><a href="#rtoc-8">中央値をマークする</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-9">平滑化の度合いを変更する</a></li><li class="rtoc-item"><a href="#rtoc-10">ボックスプロットと重ね合わせる</a></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">データ生成と分布の確認</h2>



<p>データを作ってみます。自分のデータがある場合にはそちらを使ってみてください。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
#作業ディレクトリのセットとtidyverse、ggbeeswarmの起動
#ggbeeswarmはドットプロットを描くためです
setwd(&quot;~/Rpractice/&quot;)
library(tidyverse)
library(ggbeeswarm)

#データを生成します
dat &lt;- list(
  X &lt;- rnorm(100, 5, 10),
  Y &lt;- rnorm(100, 20, 10),
  Z &lt;- rnorm(100, 15, 15)
)

#データを整えます
#単に使いやすいデータの形にするだけです
df &lt;- data.frame(matrix(unlist(dat), nrow=100))
colnames(df) &lt;- c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;)
df.long &lt;- pivot_longer(df, cols = A:C, names_to = &quot;Categories&quot;, values_to = &quot;Values&quot;)

#ドットプロットです
ggplot(df.long, aes(x = Categories, y = Values))+
  geom_beeswarm(aes(color = Categories),
                size = 2,
                cex = 2,
                alpha = .8)+
  theme_classic()+
  theme(legend.position = &quot;none&quot;)

</pre></div>


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



<p>ドットプロット、ggbeeswarmについてはこちらからどうぞ。またデータをpivot_longerで縦長に変換しています。</p>



https://brain-storm.space/ggbeeswarm/820/



https://brain-storm.space/tidyr_gather_pivot_longer/784/



<p>それでは、バイオリンプロットを描いていきましょう！</p>



<h2 id="rtoc-2"  class="wp-block-heading">バイオリンプロットを描く</h2>



<p>ggplot2でバイオリンプロットは<span class="marker">geom_violin()を使います</span>！</p>



<p>theme_classic()で背景を白くします。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values))+
  geom_violin()+
  theme_classic()
</pre></div>


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



<p>これがバイオリンプロットの基本形になります。バイオリンプロットの両端が切れていますね。この上にドットプロットを重ねてみると、その答えがわかります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values))+
  geom_violin()+
  geom_beeswarm(aes(color = Categories),
                size = 2,
                cex = 2,
                alpha = .8)+
  theme_classic()
</pre></div>


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



<p>このバイオリンプロットの両端は最大値と最小値でカットされていることがわかります。これをカットしないのは<span class="marker">geom_violin(trim = FALSE)</span>で指定します。</p>



<p>ドットプロットも重ねてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values))+
  geom_violin(trim = FALSE)+
  geom_beeswarm(aes(color = Categories),
                size = 2,
                cex = 2,
                alpha = .8)+
  theme_classic()
</pre></div>


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



<p>このように点のないところまで上下に伸びますので要注意です。</p>



<h2 id="rtoc-3"  class="wp-block-heading">バイオリンに色をつける</h2>



<p>バイオリンに色を付ける場合、枠に色をつけるか、中を塗りつぶすかで指定の仕方を変えます。</p>



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



<p><span class="marker">枠に色をつけるのはcolorで指定します。</span></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, color = Categories))+
  geom_violin()+
  theme_classic()

</pre></div>


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



<div class="wp-block-jin-gb-block-box concept-box2">
<p>重ね合わせの時にバイオリンだけ色をつけたい、などでcolorをgeom_violinのaesで指定もできます。</p>
</div>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values))+
  geom_violin(aes(color = Categories))+
  theme_classic()

</pre></div>


<p> 出てくる図は上と同じです。慣れた人には当たり前なんですが、初心者のうちは意外に迷うものです。</p>



<h3 id="rtoc-5"  class="wp-block-heading">バイオリンの中に色を塗る</h3>



<p>　<span class="marker">バイオリンの中を塗りつぶす時には、fillを使います</span>。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, fill = Categories))+
  geom_violin()+
  theme_classic()
</pre></div>


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



<p>色を変えるときにはいくつか方法があります。今回は<span class="marker">scale_fill_brewerで指定をしてみます。</span></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, fill = Categories))+
  geom_violin()+
  scale_fill_brewer(palette = &quot;Set2&quot;)+
  theme_classic()

</pre></div>


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



<h2 id="rtoc-6"  class="wp-block-heading">平均値や中央値を入れる</h2>



<p>バイオリンプロットに平均値や中央値を入れるのは、stat_summaryを使います。</p>



<h3 id="rtoc-7"  class="wp-block-heading">平均値をマークする</h3>



<p>平均値をマークしてみましょう。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, color = Categories))+
  geom_violin()+
  stat_summary(fun = mean, geom = &quot;point&quot;, 
               shape =16, size = 2, color = &quot;red&quot;)+
  theme_classic()
</pre></div>


<p>平均値を赤い点にしました。stat_summary()の中のshapeとcolorで形と色を指定をしています。</p>



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



<div class="wp-block-jin-gb-block-border jin-sen"><div class="jin-sen-solid" style="border-width:3px;border-color:#f48789"></div></div>



<p>stat_summaryのshapeはpchと同じです。</p>



<p>数値に対応する図形はこのようになっています！</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/pch_figure-5.png" alt="" class="wp-image-722" width="693" height="389"/></figure>



<div class="wp-block-jin-gb-block-border jin-sen"><div class="jin-sen-solid" style="border-width:3px;border-color:#f48789"></div></div>



<h3 id="rtoc-8"  class="wp-block-heading">中央値をマークする</h3>



<p>今度は中央値です。</p>



<p>ボックスプロットを重ねれば必ずしも必要ないかもしれませんが、先程と同じようにstat_summaryを使うことができます。shapeで十字マーク(shape = 3)にしています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, color = Categories))+
  geom_violin()+
  stat_summary(fun = median, geom = &quot;point&quot;, 
               shape = 3, size = 2, color = &quot;red&quot;)+
  theme_classic()

</pre></div>


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



<h2 id="rtoc-9"  class="wp-block-heading">平滑化の度合いを変更する</h2>



<p>平滑化の度合いを変更したいときにはgeom_violinの中でadjustを指定します。</p>



<p>デフォルトはadjust = 1です。</p>



<p>まずはadjustの値を小さくしてみます。adjustを0.2にしてみましょう！</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, fill = Categories))+
  geom_violin(adjust = .2)+
  theme_classic()
</pre></div>


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



<p>細かい並々がたくさんできました。反対にadjustを大きくしてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, fill = Categories))+
  geom_violin(adjust = 2)+
  theme_classic()
</pre></div>


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



<p>今度は赤の群にあったくびれがなくなってしまいました。</p>



<h2 id="rtoc-10"  class="wp-block-heading">ボックスプロットと重ね合わせる</h2>



<p>ボックスプロットとの重ね合わせはよく使われます。</p>



<p>ボックスプロットを重ねることでその威力を発揮します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, fill = Categories))+
  geom_violin()+
  geom_boxplot(width = .1, fill = &quot;white&quot;)+
  theme_classic()

</pre></div>


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



<p>外れ値を表示しないようにするにはoutlier.color = NAを指定します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values, fill = Categories))+
  geom_violin()+
  geom_boxplot(width = .1, fill = &quot;white&quot;, outlier.color = NA)+
  theme_classic()

</pre></div>


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



<p>ボックスプロットを黒く塗って、中央値を白丸にする方法です。白黒印刷物に投稿する場合は役に立ちます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = Categories, y = Values))+
  geom_violin()+
  geom_boxplot(width = .1, fill = &quot;black&quot;, outlier.color = NA) +
  stat_summary(fun = median, geom = &quot;point&quot;, fill = &quot;white&quot;, shape = 21, size = 3) +
  theme_classic()
</pre></div>


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



<p>これでバイオリンプロットも描けると思います。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/violin-plot/871/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
