<?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>【ggplot2】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/ggplot2/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 13:21:30 +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>【ggplot2】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[R for beginners] Creating Error Bars for Bar Graphs and Line Graphs in ggplot2</title>
		<link>https://brain-storm.space/error-bar-en/1291/</link>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Fri, 24 Mar 2023 13:13:56 +0000</pubDate>
				<category><![CDATA[Statistical Analysis]]></category>
		<category><![CDATA[bar graph]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[error bar]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[tidyverse]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1291</guid>

					<description><![CDATA[The #8216;T#8217; symbol on bar graphs and line graphs represents error bars that extend above and below the]]></description>
										<content:encoded><![CDATA[
<p>The &#8216;T&#8217; symbol on bar graphs and line graphs represents error bars that extend above and below the data point, indicating standard error or standard deviation. Adding error bars in R&#8217;s ggplot2 is easy. Here&#8217;s a step-by-step guide:</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1291" 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">Preparing the Data</a></li><li class="rtoc-item"><a href="#rtoc-2">Calculating the Mean, Standard Deviation, and Standard Error of the Data</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">Use group_by and summarise_all functions in dplyr to Calculate</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-4">Drawing Error Bars in Bar Graphs</a></li><li class="rtoc-item"><a href="#rtoc-5">Drawing Error Bars on a Line Graph</a></li><li class="rtoc-item"><a href="#rtoc-6">Adding Error Bars to Grouped Data</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-7">Adding Error Bars to Grouped Bar Graphs</a></li><li class="rtoc-item"><a href="#rtoc-8">Drawing Error Bars on a Grouped Line Graph</a></li></ul></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">Preparing the Data</h2>



<p>We&#8217;ll be using tidyverse for this. If you have your own data, skip ahead.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# Set working directory
setwd(&quot;~/Rpractice/&quot;)

# Load tidyverse
library(tidyverse)

# Generate random data
dat &lt;- list(
  X &lt;- rnorm(50, 30, 10),
  Y &lt;- rnorm(50, 50, 5),
  Z &lt;- rnorm(50, 40, 15)
)
df &lt;- data.frame(matrix(unlist(dat), nrow=50))
colnames(df) &lt;- c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;)

# Transform the data from wide to long
df.long &lt;- pivot_longer(df, cols = A:C, 
                        names_to = &quot;Categories&quot;, 
                        values_to = &quot;Values&quot;)
</pre></div>


<p>Now we have data that looks like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; head(df.long)
# A tibble: 6 x 2
  Categories Values
  &lt;chr&gt;       &lt;dbl&gt;
1 A            26.8
2 B            53.7
3 C            27.3
4 A            31.0
5 B            58.7
6 C            56.2
&gt; 
</pre></div>


<p>To add error bars to bar graphs and line graphs, you need the mean and standard error (or standard deviation) of the data. We use pivot_longer to transform the data from wide to long format.&#8221;</p>



<h2 id="rtoc-2"  class="wp-block-heading">Calculating the Mean, Standard Deviation, and Standard Error of the Data</h2>



<p>To add error bars, we need to calculate the mean, standard deviation, and standard error. If you already have this data, you can skip this section.</p>



<p>Calculating these values is easy with the dplyr package in tidyverse.</p>



<h3 id="rtoc-3"  class="wp-block-heading">Use group_by and summarise_all functions in dplyr to Calculate</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
a &lt;- group_by(df.long, Categories) %&gt;% 
  summarise_all(list(mean = ~mean(.), 
                     sd = ~sd(.), 
                     se = ~sd(.)/sqrt(length(.))))

</pre></div>


<p>We can simplify this code further,</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
a &lt;- group_by(df.long, Categories) %&gt;% 
  summarise_all(list(mean = mean, 
                     sd = sd, 
                     se = ~sd/sqrt(length(.))))

</pre></div>


<p>Let&#8217;s take a look at the data for &#8216;a&#8217;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; a
# A tibble: 3 x 4
  Categories  mean    sd    se
  &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 A           30.1  9.93 1.40 
2 B           49.2  5.00 0.708
3 C           43.9 15.5  2.20 
</pre></div>


<p>We have generated a distribution that looks like this. Since the data was generated randomly, the values may vary slightly if you follow the same steps.</p>



<h2 id="rtoc-4"  class="wp-block-heading">Drawing Error Bars in Bar Graphs</h2>



<p>We will use the calculated data to create a bar graph and specify the error bars using <code><span class="marker">geom_errorbar()</span></code>. First, let&#8217;s specify only <code>ymin</code> and <code>ymax</code> in <code>geom_errorbar()</code> and take a look at the graph.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Categories, y = mean, fill = Categories))+
  geom_bar(stat = &quot;identity&quot;) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se))

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar1-1024x614.jpg" alt="" class="wp-image-905" width="512" height="307"/></figure>



<p>The width is too wide. Let&#8217;s adjust it using the <code>width</code> argument. We will narrow both the bar graph and error bars.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Categories, y = mean, fill = Categories))+
  geom_bar(stat = &quot;identity&quot;, width = 0.6) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1)

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar2-1024x614.jpg" alt="" class="wp-image-906" width="512" height="307"/></figure>



<p>It&#8217;s a good to adjust the width according to the size of the output image.</p>



<h2 id="rtoc-5"  class="wp-block-heading">Drawing Error Bars on a Line Graph</h2>



<p>The process for adding error bars to a line graph is the same as above. First, draw the line graph and then add <code>geom_errorbar()</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Categories, y = mean)) +
  geom_line(group = 1) +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar3-1024x614.jpg" alt="" class="wp-image-907" width="512" height="307"/></figure>



<h2 id="rtoc-6"  class="wp-block-heading">Adding Error Bars to Grouped Data</h2>



<p>Let&#8217;s generate grouped data.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;# Add ID column
data &lt;- df.long %&gt;% 
  tibble::rownames_to_column(var = &quot;ID&quot;)

# Convert ID from character to numeric
data$ID &lt;- as.numeric(data$ID)

# Assign 1 to ID &lt;= 75 and 0 to ID &gt;= 76 to create the &quot;group&quot; column
data &lt;- mutate(data, group = ifelse(ID &lt; 76, 1, 0))

# Calculate Mean, SD, and SE
b &lt;- group_by(data, group, Categories) %&gt;% 
  summarise_at(vars(Values), list(mean = ~mean(.), 
                     sd = ~sd(.), 
                     se = ~sd(.)/sqrt(length(.))))

# Convert group column values from numeric to character
b$group &lt;- as.character(b$group)

# Check the data included in b
&gt; head(b)
# A tibble: 6 x 5
# Groups:   group &lt;img class=&quot;ranking-number&quot; src=&quot;https://brain-storm.space/wp-content/themes/jin/img/rank02.png&quot; /&gt;
  group Categories  mean    sd    se
  &lt;chr&gt; &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 0     A           31.4 11.1  2.21 
2 0     B           51.0  4.31 0.862
3 0     C           33.9 13.5  2.70 
4 1     A           29.5 10.4  2.08 
5 1     B           49.5  4.02 0.804
6 1     C           36.5 15.8  3.16 
&gt; &lt;/pre&gt;
</pre></div>


<p>Now that we have grouped data, let&#8217;s draw a bar graph with error bars.</p>



<h3 id="rtoc-7"  class="wp-block-heading">Adding Error Bars to Grouped Bar Graphs</h3>



<p>By specifying <code>position = "dodge"</code>, you can create a grouped bar chart like this.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, fill = group))+
  geom_bar(stat = &quot;identity&quot;, width = 0.6, 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/06/errorbar4-1024x614.jpg" alt="" class="wp-image-909" width="512" height="307"/></figure>



<p>Let&#8217;s add error bars to this plot.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, fill = group))+
  geom_bar(stat = &quot;identity&quot;, width = 0.6, position = &quot;dodge&quot;)+
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), 
                    position = &quot;dodge&quot;, width = .1)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar5-1024x614.jpg" alt="" class="wp-image-910" width="512" height="307"/></figure>



<p>It is shifted to the center. You can adjust the position with <span class="marker"><code>position = position_dodge()</code></span>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, fill = group)) +
  geom_bar(stat = &quot;identity&quot;, width = 0.6, position = &quot;dodge&quot;) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                position = position_dodge(0.6), width = .1)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar6-1024x614.jpg" alt="" class="wp-image-911" width="512" height="307"/></figure>



<p>You&#8217;ve created a nice bar graph!</p>



<h3 id="rtoc-8"  class="wp-block-heading">Drawing Error Bars on a Grouped Line Graph</h3>



<p>The same can be done for a grouped line graph as well.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, group = group, color = group)) +
  geom_line() +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1, color = &quot;black&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar7-1024x614.jpg" alt="" class="wp-image-913" width="512" height="307"/></figure>



<p>The error bars are on top and overlapping, making it difficult to see. Let&#8217;s move <span class="marker">the error bars to the back first</span>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, group = group, color = group)) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1, color = &quot;black&quot;) +
  geom_line() +
  geom_point(size = 3)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar8-1024x614.jpg" alt="" class="wp-image-914" width="512" height="307"/></figure>



<p>By <span class="marker">shifting the position to the left or right</span> using <code>position_dodge()</code>, overlapping of error bars can be avoided.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, group = group, color = group)) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1, color = &quot;black&quot;, position = position_dodge(.2)) +
  geom_line(position = position_dodge(.2)) +
  geom_point(size = 3, position = position_dodge(.2))

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar10-1024x614.jpg" alt="" class="wp-image-915" width="512" height="307"/></figure>



<p>Now you can add error bars to line graphs. I hope this was helpful.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>R, ggplot2を使った論文に使えるグラフテクニック【まとめ】</title>
		<link>https://brain-storm.space/r_graph/962/</link>
					<comments>https://brain-storm.space/r_graph/962/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Fri, 24 Mar 2023 12:25:12 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=962</guid>

					<description><![CDATA[Rではキレイなビジュアルのグラフを無料で作ることができます。高いソフトウェアは不要で、論文に十分使える図ができます！特にggplot2はその使いやすさや見た目のキレイさからとても好評です。 様々なデータがあると思いますが]]></description>
										<content:encoded><![CDATA[
<p>Rではキレイなビジュアルのグラフを<span class="marker">無料で</span>作ることができます。高いソフトウェアは不要で、論文に十分使える図ができます！特にggplot2はその使いやすさや見た目のキレイさからとても好評です。</p>



<p>様々なデータがあると思いますが、一番良く使うのは箱ひげ図や散布図ではないでしょうか。それらをよりわかりやすくビジュアライズしていきたい方向けの基礎的なテクニックです！</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="962" 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><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-2">baseのboxplotとbeeswarmライブラリを用いて作図する</a></li><li class="rtoc-item"><a href="#rtoc-3">ggplot2を用いてボックスプロットとドットプロットを作図する</a></li><li class="rtoc-item"><a href="#rtoc-4">ggbeeswarmを用いてggplot2のドットを散らばらせる</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-5">ggplot2でバイオリンプロットを描く</a></li><li class="rtoc-item"><a href="#rtoc-6">ggplot2を使って散布図を描く</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-7">散布図と近似直線を描く方法</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-8">複数データはfacet_gridとfacet_wrapを使う</a></li><li class="rtoc-item"><a href="#rtoc-9">geom_barで棒グラフを作図する</a></li><li class="rtoc-item"><a href="#rtoc-10">geom_errorbarで棒グラフと折れ線グラフにエラーバーをつける</a></li><li class="rtoc-item"><a href="#rtoc-11">geom_tileで相関行列のヒートマップをつくる</a></li><li class="rtoc-item"><a href="#rtoc-12">背景の一部(四角)に色をつけるgeom_rect</a></li><li class="rtoc-item"><a href="#rtoc-13">ROC曲線を描く</a></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">ボックスプロット(箱ひげ図)とドットプロットを描く</h2>



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



<p>このグラフは<span class="marker">ドットプロット</span>と呼ばれます。箱ひげ図の上にドットプロットを重ねています。</p>



<p>箱ひげ図(box plot)とドットプロット(dot plot)は実際のデータがどのような分布になっているのかみるのに有用です。これで外れ値などもよくわかりますし、誤魔化している感じがない正直なプロットです。</p>



<p>base機能とbeeswarmパッケージを用いた方法とggplot2を使う方法があります。ggplot2を使う場合にはggbeeswarmというパッケージを使うとよりきれいになります。</p>



<h3 id="rtoc-2"  class="wp-block-heading">baseのboxplotとbeeswarmライブラリを用いて作図する</h3>



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



<p>箱ひげ図は、baseと呼ばれるRに元々入っている機能を使ったものです。箱ひげ図はかんたんにかけますが、その後のドットプロットについてはbeeswarmというライブラリを使います。beeswarmは蜂群図とも呼ばれ、点を散らばらせることで視覚的に印象的にみせる効果があります。beeswarmライブラリで簡単に作図をすることができます。</p>



<p>x軸の並び替えもこちらに書いています。</p>



https://brain-storm.space/beeswarm_r/698/



<h3 id="rtoc-3"  class="wp-block-heading">ggplot2を用いてボックスプロットとドットプロットを作図する</h3>



<p>ggplot2を使うのに慣れた人向けの方法です。これは単純なドットプロットを描く方法で、いわゆるbeeswarm(蜂群図)ではありませんが、視覚的効果は十分にあります。</p>



https://brain-storm.space/rggplot2_boxplot_dotplot/620/



<h3 id="rtoc-4"  class="wp-block-heading">ggbeeswarmを用いてggplot2のドットを散らばらせる</h3>



<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>ggplot2でもbeeswarm(蜂群図)を作成することができます。これにはggplot2に加えて、ggbeeswarmというライブラリを使います。点の散らし方にもいくつかの方法があります。</p>



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



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



<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>バイオリンプロットも複数のデータを比較する時にとても便利です。とてもキレイな図になります。この中では、バイオリンプロットにドットプロットや箱ひげ図の重ね方を説明しています。バイオリンプロットはggplot2のgeom_violinで描くことができます。</p>



https://brain-storm.space/violin-plot/871/



<h2 id="rtoc-6"  class="wp-block-heading">ggplot2を使って散布図を描く</h2>



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



<p>相関を数字で出すよりも実際に散布図を出したほうがより直感的にもわかりやすくなります。これも正直な印象を与えることができます。　ggplot2を用いて、geom_pointで散布図を、geom_smoothまたはstat_smoothで近似曲線を表示できます。</p>



<h3 id="rtoc-7"  class="wp-block-heading">散布図と近似直線を描く方法</h3>



https://brain-storm.space/rggplot_scatterplot/751/



<h2 id="rtoc-8"  class="wp-block-heading">複数データはfacet_gridとfacet_wrapを使う</h2>



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



<p>複数データを一度に表示したいとき、一つ一つ出していたのでは大変です。そんなときはfacet_gridやfacet_wrapを使うと一度にできて、見栄えのあるグラフができます。</p>



<p>またタイトルラベルなどの変え方にも少しテクニックが必要なので、その説明も加えています。</p>



https://brain-storm.space/r_facet/843/



<h2 id="rtoc-9"  class="wp-block-heading">geom_barで棒グラフを作図する</h2>



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



<p>棒グラフは最も基本的なグラフの一つです。エクセルでも出来てしまうので、それほど困らないかもしれませんが、ggplot2でも簡単に出来てしまいます。次のリンクで紹介をしています。</p>



https://brain-storm.space/bargraph/1025/



<h2 id="rtoc-10"  class="wp-block-heading">geom_errorbarで棒グラフと折れ線グラフにエラーバーをつける</h2>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar6-1024x614.jpg" alt="" class="wp-image-911" width="512" height="307"/></figure>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar10-1024x614.jpg" alt="" class="wp-image-915" width="512" height="307"/></figure>



<p>棒グラフや折れ線グラフは平均値の大小や推移などを可視化するのによく使われます。データ解析ではそこにエラーバーをつけたりしますが、そのやり方も知らなくては作図ができません。</p>



<p>さらにRでの<span class="marker">標準偏差や標準誤差の算出方法も説明しています</span>。計算で標準誤差などを出せば簡単に作図ができます。</p>



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



<h2 id="rtoc-11"  class="wp-block-heading">geom_tileで相関行列のヒートマップをつくる</h2>



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



<p>相関行列をつくったとき、それをテーブルにしても十分ですが、それを視覚的にわかりやすくするとより印象的な図になります。一目でデータが理解出来るのは、大きなメリットになります。</p>



<p>ggplot2以外のいくつかのパッケージでも描くことはできますが、ggplot2を使った方法を説明しています。それを紹介したのは次のリンクになります。</p>



https://brain-storm.space/corrheatmap/942/



<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>ggplot2を使うときにデータを横長のデータから縦長のデータへ変換する必要があるときがあります。そんなときはpivot_longerを使うことで簡単に変換をすることができます。これは次の記事で詳細に説明をしています。</p>



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



<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>今回紹介した作図法だけでも、論文のグラフの多くは作図ができると思います。今後もさらなる方法を広めて行きたいと思っています。</p>



<h2 id="rtoc-12"  class="wp-block-heading">背景の一部(四角)に色をつけるgeom_rect</h2>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig6.jpeg" alt="" class="wp-image-1199" width="400" height="400"/></figure>



<p>背景に色をつけて、その部分を強調したり、領域を分けたいことがあります。そんな時はgeom_rectを使います。</p>



https://brain-storm.space/geom_rect/1191/



<h2 id="rtoc-13"  class="wp-block-heading">ROC曲線を描く</h2>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_comparison_final_8-1.jpeg" alt="ROC曲線" class="wp-image-1251" width="400" height="400"/><figcaption class="wp-element-caption">ROC curve</figcaption></figure>



<p>ROC曲線を書くのは思ったよりも簡単です。Rの初心者でも短時間で書くことができます。AUC(Area under the curve)の比較もあっという間です。</p>



https://brain-storm.space/roc-curve/1212/



<p>今後もさらに追加していく予定です。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/r_graph/962/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【R初心者向け】ggplot2で棒グラフと折れ線グラフにエラーバー(error bar)を書く</title>
		<link>https://brain-storm.space/ggplot2_errorbar/895/</link>
					<comments>https://brain-storm.space/ggplot2_errorbar/895/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Fri, 24 Mar 2023 12:20:55 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[bar graph]]></category>
		<category><![CDATA[error bar]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[エラーバー]]></category>
		<category><![CDATA[棒グラフ]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=895</guid>

					<description><![CDATA[棒グラフや折れ線グラフなどについている#8221;T#8221;が上下についているものがエラーバーです。エラーバーは標準誤差や標準偏差を示しています。 Rのggplot2を使えば簡単にできます。ステップバイステップで]]></description>
										<content:encoded><![CDATA[
<p>棒グラフや折れ線グラフなどについている&#8221;T&#8221;が上下についているものがエラーバーです。エラーバーは標準誤差や標準偏差を示しています。</p>



<p>Rの<span class="marker">ggplot2を使えば簡単にできます</span>。ステップバイステップで書いていきます。</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="895" 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><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">dplyrのgroup_byとsummarise_allを使う</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-4">棒グラフでエラーバーを描く</a></li><li class="rtoc-item"><a href="#rtoc-5">折れ線グラフでエラーバーを描く</a></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></ol></div><h2 id="rtoc-1"  class="wp-block-heading">データの準備する</h2>



<p>tidyverseを利用します。自分のデータを使う場合には読み飛ばしてください。</p>


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

# tidyverseの起動
library(tidyverse)

# ランダムデータの生成
dat &lt;- list(
  X &lt;- rnorm(50, 30, 10),
  Y &lt;- rnorm(50, 50, 5),
  Z &lt;- rnorm(50, 40, 15)
)
df &lt;- data.frame(matrix(unlist(dat), nrow=50))
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;)
</pre></div>


<p>これでこのようなデータができました。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; head(df.long)
# A tibble: 6 x 2
  Categories Values
  &lt;chr&gt;       &lt;dbl&gt;
1 A            26.8
2 B            53.7
3 C            27.3
4 A            31.0
5 B            58.7
6 C            56.2
&gt; 
</pre></div>


<p>棒グラフと折れ線グラフにエラーバーを加えるには、平均値と標準誤差(または標準偏差)が必要です。pivot_longerで縦長データにしています。</p>



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



<h2 id="rtoc-2"  class="wp-block-heading">データの平均、標準偏差、標準誤差を算出する</h2>



<p>エラーバーのためには平均(mean)、標準偏差(sd)、標準誤差(se)を算出します。すでにこれらのデータがある場合にはここも読み飛ばしてください。</p>



<p>その算出方法も慣れれば簡単です！tidyverseに入っているdplyrを使っていきましょう。</p>



<h3 id="rtoc-3"  class="wp-block-heading">dplyrのgroup_byとsummarise_allを使う</h3>



<p>(summariseはアメリカ英語でsummarizeでもOKです)</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
a &lt;- group_by(df.long, Categories) %&gt;% 
  summarise_all(list(mean = ~mean(.), 
                     sd = ~sd(.), 
                     se = ~sd(.)/sqrt(length(.))))

</pre></div>


<p>これはもう少し簡素化できます。どちらを使っても大丈夫です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
a &lt;- group_by(df.long, Categories) %&gt;% 
  summarise_all(list(mean = mean, 
                     sd = sd, 
                     se = ~sd/sqrt(length(.))))

</pre></div>


<p>ではaのデータをみてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; a
# A tibble: 3 x 4
  Categories  mean    sd    se
  &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 A           30.1  9.93 1.40 
2 B           49.2  5.00 0.708
3 C           43.9 15.5  2.20 
</pre></div>


<p>このような分布になりました。データはランダムで生成しましたので、同じ手順をなぞってもこの数値は若干変化します。</p>



<h2 id="rtoc-4"  class="wp-block-heading">棒グラフでエラーバーを描く</h2>



<p>計算をしたデータを使って棒グラフをかきます。<span class="marker">エラーバーはgeom_errorbarで指定します</span>。まずはgeom_errorbarでyminとymaxのみを指定して、グラフをみてみましょう。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Categories, y = mean, fill = Categories))+
  geom_bar(stat = &quot;identity&quot;) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se))

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar1-1024x614.jpg" alt="" class="wp-image-905" width="512" height="307"/></figure>



<p>横幅が広すぎますね。<span class="marker">これをwidthで調整します。</span>バーグラフ、エラーバーともに狭くしてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Categories, y = mean, fill = Categories))+
  geom_bar(stat = &quot;identity&quot;, width = 0.6) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1)

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar2-1024x614.jpg" alt="" class="wp-image-906" width="512" height="307"/></figure>



<p>幅は出力する画像の大きさに合わせて適宜調整をするといいです。</p>



<h2 id="rtoc-5"  class="wp-block-heading">折れ線グラフでエラーバーを描く</h2>



<p>折れ線グラフも上と同じ要領です。折れ線グラフを描いて、geom_errorbarを付け足します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Categories, y = mean)) +
  geom_line(group = 1) +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar3-1024x614.jpg" alt="" class="wp-image-907" width="512" height="307"/></figure>



<h2 id="rtoc-6"  class="wp-block-heading">グループ化されたデータでのエラーバーを加える</h2>



<p>グループ化したデータを生成してみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;# IDを追加する
data &lt;- df.long %&gt;% 
  tibble::rownames_to_column(var = &quot;ID&quot;)

# IDを文字列から数値へ変換する
data$ID &lt;- as.numeric(data$ID)

# IDが75以下を1, 76以上を0と割り当てて、group列を作る
data &lt;- mutate(data, group = ifelse(ID &lt; 76, 1, 0))

# mean, SD, SEを算出
b &lt;- group_by(data, group, Categories) %&gt;% 
  summarise_at(vars(Values), list(mean = ~mean(.), 
                     sd = ~sd(.), 
                     se = ~sd(.)/sqrt(length(.))))

# group列の数値を文字列に変換する
b$group &lt;- as.character(b$group)

# bに含まれているデータを確認する
&gt; head(b)
# A tibble: 6 x 5
# Groups:   group &lt;img class=&quot;ranking-number&quot; src=&quot;https://brain-storm.space/wp-content/themes/jin/img/rank02.png&quot; /&gt;
  group Categories  mean    sd    se
  &lt;chr&gt; &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 0     A           31.4 11.1  2.21 
2 0     B           51.0  4.31 0.862
3 0     C           33.9 13.5  2.70 
4 1     A           29.5 10.4  2.08 
5 1     B           49.5  4.02 0.804
6 1     C           36.5 15.8  3.16 
&gt; &lt;/pre&gt;
</pre></div>


<p>これでグループ化されました。このデータでエラーバーの入った棒グラフを描いてみましょう。</p>



<h3 id="rtoc-7"  class="wp-block-heading">グループ化された棒グラフにエラーバーを追加する</h3>



<p>position = &#8220;dodge&#8221;をするとこのような棒グラフができます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, fill = group))+
  geom_bar(stat = &quot;identity&quot;, width = 0.6, 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/06/errorbar4-1024x614.jpg" alt="" class="wp-image-909" width="512" height="307"/></figure>



<p>これにエラーバーを加えていきます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, fill = group))+
  geom_bar(stat = &quot;identity&quot;, width = 0.6, position = &quot;dodge&quot;)+
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), 
                    position = &quot;dodge&quot;, width = .1)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar5-1024x614.jpg" alt="" class="wp-image-910" width="512" height="307"/></figure>



<p>真ん中に寄ってしまいました。この位置は<span class="marker">position = position_dodge()で調整できます</span>。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, fill = group)) +
  geom_bar(stat = &quot;identity&quot;, width = 0.6, position = &quot;dodge&quot;) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                position = position_dodge(0.6), width = .1)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar6-1024x614.jpg" alt="" class="wp-image-911" width="512" height="307"/></figure>



<p>これでいい棒グラフができました！</p>



<h3 id="rtoc-8"  class="wp-block-heading">グループ化された折れ線グラフでのエラーバーを書く</h3>



<p>折れ線グラフも同じようにできます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, group = group, color = group)) +
  geom_line() +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1, color = &quot;black&quot;)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar7-1024x614.jpg" alt="" class="wp-image-913" width="512" height="307"/></figure>



<p>エラーバーが前面に出ていて重なっているので見にくくなっています。まず<span class="marker">エラーバーを後ろに持って</span><span class="marker">い</span><span class="marker">きます。</span></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, group = group, color = group)) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1, color = &quot;black&quot;) +
  geom_line() +
  geom_point(size = 3)
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar8-1024x614.jpg" alt="" class="wp-image-914" width="512" height="307"/></figure>



<p>エラーバーが重なりは<span class="marker">左右にずらす</span>ことで解消ができます。さきほどの<span class="marker">position_dodgeを使います</span>！</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Categories, y = mean, group = group, color = group)) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                width = .1, color = &quot;black&quot;, position = position_dodge(.2)) +
  geom_line(position = position_dodge(.2)) +
  geom_point(size = 3, position = position_dodge(.2))

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar10-1024x614.jpg" alt="" class="wp-image-915" width="512" height="307"/></figure>



<p>これで折れ線グラフのエラーバーを加えることができました。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/ggplot2_errorbar/895/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Violin Plot in R &#8211; How to Draw It &#8211;</title>
		<link>https://brain-storm.space/violin-plot-en/1265/</link>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Fri, 24 Mar 2023 10:33:25 +0000</pubDate>
				<category><![CDATA[Statistical Analysis]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Violinplot]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1265</guid>

					<description><![CDATA[There are several ways to visualize data, and one of them is the violin plot! This method is useful for compar]]></description>
										<content:encoded><![CDATA[
<p>There are several ways to visualize data, and one of them is the violin plot! This method is useful for comparing multiple sets of data, and it has an appealing appearance.</p>



<p>First, you&#8217;ll need to prepare your data. While you&#8217;re at it, you can also check the data distribution using a dot plot.</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1265" 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">Data Generation and Distribution Checking</a></li><li class="rtoc-item"><a href="#rtoc-2">Draw a Simple Violin Plot</a></li><li class="rtoc-item"><a href="#rtoc-3">Add Color to Violin Plot</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-4">Add Color to the Borders</a></li><li class="rtoc-item"><a href="#rtoc-5">Fill the interior of Violin Plot</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-6">Add Mean or Median to the Violin Plot</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-7">Add Mean</a></li><li class="rtoc-item"><a href="#rtoc-8">Add Median</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-9">Change the Degree of Smoothing</a></li><li class="rtoc-item"><a href="#rtoc-10">Overlaying a Box Plot</a></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">Data Generation and Distribution Checking</h2>



<p>Now, let&#8217;s create some data. If you have your own data, feel free to use that instead.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# Set working directory
setwd(&quot;~/Rpractice/&quot;)
# load tidyverse and ggbeeswarm
library(tidyverse)
library(ggbeeswarm)

# generate sample data
dat &lt;- list(
  X &lt;- rnorm(100, 5, 10),
  Y &lt;- rnorm(100, 20, 10),
  Z &lt;- rnorm(100, 15, 15)
)

# format the data into a more usable shape using pivot_longer
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;)

# draw dotplot
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>The dot plot displays the data distribution and can be used to confirm that the data is appropriate for creating a violin plot.</p>



<h2 id="rtoc-2"  class="wp-block-heading">Draw a Simple Violin Plot</h2>



<p>To draw a violin plot using ggplot2, you can utilize the geom_violin() function. To create a clean and simple plot, set the background color to white using the theme_classic() function.</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>The ends of the violin plot may appear cut off. By overlaying a dot plot on top of the violin plot, you can address this issue. To do so, you can use either geom_dotplot() or geom_beeswarm(), which are both part of the ggplot2 package.</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>You can see that the ends of this violin plot are cut off at the minimum and maximum values. If you don&#8217;t want to cut off the ends, you can use geom_violin(trim = FALSE) to specify this preference.</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>The violin plot may stretch vertically up and down, even in areas where there are no data points.</p>



<h2 id="rtoc-3"  class="wp-block-heading">Add Color to Violin Plot</h2>



<h3 id="rtoc-4"  class="wp-block-heading">Add Color to the Borders</h3>



<p>To add color to the border of the violin plot, you can use aes(color = ) inside the geom_violin() function.</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>



<h3 id="rtoc-5"  class="wp-block-heading">Fill the interior of Violin Plot</h3>



<p>If you want to fill the interior of the violin plot with color, you can use aes(fill =) inside the geom_violin() function.</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>There are several ways to change the color of the violin plot. One way is to use the scale_fill_brewer() function to specify the color scheme.</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">Add Mean or Median to the Violin Plot</h2>



<p>To add the mean or median to the violin plot, you can use the stat_summary() function.</p>



<h3 id="rtoc-7"  class="wp-block-heading">Add Mean</h3>


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


<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>The shape parameter in stat_summary() is the same as pch in base R. Here&#8217;s a list of shapes that correspond to each numerical value:</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">Add Median</h3>


<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">Change the Degree of Smoothing</h2>



<p>To change the degree of smoothing in the violin plot, you can use the adjust parameter inside the geom_violin() function. The default value for adjust is 1.</p>



<p>To decrease the degree of smoothing, you can set adjust to a smaller value. For example, to set adjust to 0.2, you can use the following code:</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>Conversely, if you want to increase the degree of smoothing in the violin plot, you can set the adjust parameter to a larger value.</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>If you increase the degree of smoothing too much, the violin plot can become overly smoothed and lose important details.</p>



<h2 id="rtoc-10"  class="wp-block-heading">Overlaying a Box Plot</h2>



<p>Overlaying a violin plot with a box plot is a common technique in data visualization, and it can be a powerful way to display data.</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>To hide the outliers in the box plot when overlaying it with a violin plot, you can use the outlier.color parameter inside the geom_boxplot() function.</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>You can fill the box plot with black color and add a white circle at the median value.</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>With the information provided, I believe you can now create a violin plot. I hope this guidance proves helpful!</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>【R簡単テクニック】背景に四角の領域(背景)をいれる</title>
		<link>https://brain-storm.space/geom_rect/1191/</link>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Mon, 06 Mar 2023 22:01:58 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[geom_rect]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1191</guid>

					<description><![CDATA[ggplotを使ったグラフの中に、四角の領域を入れる方法を紹介します。とても簡単です。ggplot2を使うためまずはライブラリのtidyverse(またはggplot2)を起動します。 資格の領域を作るにはgeom_re]]></description>
										<content:encoded><![CDATA[
<p>ggplotを使ったグラフの中に、四角の領域を入れる方法を紹介します。とても簡単です。ggplot2を使うためまずはライブラリのtidyverse(またはggplot2)を起動します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
library(tidyverse)
</pre></div>


<p>資格の領域を作るにはgeom_rectを使います。その領域をxmin, xmax, ymin, ymaxで指定します。その領域の色はデフォルトで灰色(grey20)になっています。</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1191" 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">geom_rectの使い方</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-2">まずは書いてみます</a></li><li class="rtoc-item"><a href="#rtoc-3">色を塗る</a></li><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></ul></li><li class="rtoc-item"><a href="#rtoc-8">他のグラフの背景にする</a></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">geom_rectの使い方</h2>



<h3 id="rtoc-2"  class="wp-block-heading">まずは書いてみます</h3>



<p>四角の領域を書くにはgeom_rectを使います。x軸の最小値(xmin)と最大値(xmax)、y軸の最小値yminと最大値ymaxをしています。すると次のようなグラフが描けます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot() +
　geom_rect(aes(xmin=1, xmax=3, ymin=2, ymax=5))
</pre></div>


<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig1.jpeg" alt="" class="wp-image-1192" width="400" height="400"/></figure>



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



<p>色をつけていきます。中の色はfillで指定します。今はレジェンドが邪魔なので消しておきます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: Code example; notranslate">
ggplot() +
  geom_rect(aes(xmin=1, xmax=3, ymin=2, ymax=5, fill='red')) +
  theme(legend.position = &quot;none&quot;)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig2.jpeg" alt="" class="wp-image-1193"/></figure>



<h3 id="rtoc-4"  class="wp-block-heading">枠線の色を塗る</h3>



<p>次にその領域の枠の色も指定します。color=&#8217;色&#8217;で枠線の色が入ります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: Code example; notranslate">
ggplot() +
geom_rect(aes(xmin=1, xmax=3, ymin=2, ymax=5, fill='red'), color='blue') +
  theme(legend.position = &quot;none&quot;)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig3.jpeg" alt="" class="wp-image-1194"/></figure>



<h3 id="rtoc-5"  class="wp-block-heading">グラフ外まで領域が広がっている時</h3>



<p>例えば、その図の外側までずっと続くような感じのグラフにしたい場合、Infを使うとできます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot()+
  geom_rect(aes(xmin=0, xmax=Inf, ymin=0, ymax=Inf, fill='red'), color='red') +
  scale_x_continuous(limits = c(-2, 6)) + 
  scale_y_continuous(limits = c(-2, 6)) +
  theme(legend.position = &quot;none&quot;)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig4.jpeg" alt="" class="wp-image-1196"/></figure>



<p>マイナス方向でずっと領域が続く時には-Infを使います。複数の領域を書く方法はまたあとで書きますが、-Infを使うと次のようになります。</p>



<p>透明度はalphaで1より小さく、そして小さい値ほど透明になります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
df &lt;- data.frame(x1=c(-Inf, 0), x2=c(0, Inf), y1=c(-Inf, 0), y2=c(0, Inf), 
                group=c('a', 'b'))
ggplot() +
  geom_rect(data=df, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill=group), 
           alpha=0.3) +
  scale_x_continuous(limits = c(-6, 6)) + 
  scale_y_continuous(limits = c(-6, 6)) +
  theme_minimal()

</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig5.jpeg" alt="" class="wp-image-1197"/></figure>



<h2 id="rtoc-6"  class="wp-block-heading">複数の領域を書きたい時</h2>



<h3 id="rtoc-7"  class="wp-block-heading">この時はデータフレームで領域を指定すると楽になります</h3>



<p>複数の四角を作りたい時、データフレームにします。下の式ではx1が左辺、x2が右辺のx座標、y1が下辺、y2が上辺のy座標です。データフレームではさらにグループ分けもしています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
df=data.frame(x1=c(1,2, 3,5), x2=c(5, 8, 5,Inf), 
              y1=c(1, 2, 2, 4), y2=c(2, 3, 6,Inf), 
             group=c('a', 'a','b','b'))
ggplot() +
geom_rect(data=df, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill=group), 
          color='black', alpha=0.3) +
scale_x_continuous(limits =c(0, 10)) +
scale_y_continuous(limits = c(0, 7))
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig6.jpeg" alt="" class="wp-image-1199"/></figure>



<p>scale_x_continuousとscale_y_continuousでx軸とy軸のどの数値まで入れるか指定しています。</p>



<h2 id="rtoc-8"  class="wp-block-heading">他のグラフの背景にする</h2>



<p>使い所としては他のグラフの背景にして目立たせるということでしょう。ggplot2は記述を＋で重ねて行くだけなので、とても簡単です。単純な直線のグラフの背景として書いてみます。geom_ablineが追加された斜めの線です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
rect &lt;- data.frame(x1=c(-Inf, 0), x2=c(0, Inf), y1=c(-Inf, 0), y2=c(0, Inf), 
                group=c('a', 'b'))
ggplot()+
  geom_rect(data=rect, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill=group), 
            alpha=0.3) +
  geom_abline(intercept=0, slope = 1) +
  scale_x_continuous(limits = c(-6, 6)) + 
  scale_y_continuous(limits = c(-6, 6)) +
  theme_minimal()
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig7.jpeg" alt="" class="wp-image-1201"/></figure>



<p>いかがだったでしょうか。わかってしまえば簡単です。ぜひ試してみてください。</p>
]]></content:encoded>
					
		
		
			</item>
		<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>
		<item>
		<title>【R】ggplot2で相関行列ヒートマップを描く</title>
		<link>https://brain-storm.space/corrheatmap/942/</link>
					<comments>https://brain-storm.space/corrheatmap/942/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Tue, 22 Jun 2021 08:09:26 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[heatmap]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=942</guid>

					<description><![CDATA[相関行列の相関係数を表にすることも多いと思います。しかし視覚に訴求する強力なツールとしてRで図を作成することができます。 それの一つが相関行列です。 Rではいくつものパッケージがあるので、そちらを使うこともできますが、シ]]></description>
										<content:encoded><![CDATA[
<p>相関行列の相関係数を表にすることも多いと思います。しかし視覚に訴求する強力なツールとしてRで図を作成することができます。</p>



<p><span class="marker">それの一つが相関行列です。</span></p>



<p>Rではいくつものパッケージがあるので、そちらを使うこともできますが、シンプルにggplot2を使った方法を紹介します。</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="942" 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><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-2">mtcarsとは</a></li><li class="rtoc-item"><a href="#rtoc-3">相関行列の生成</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-4">geom_tileを使って相関行列のヒートマップを描く</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-5">geoｍ_tile()を使う</a></li><li class="rtoc-item"><a href="#rtoc-6">scale_fill_gradientで色の変更をする</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-7">ヒートマップの中に数値を表示する</a></li><li class="rtoc-item"><a href="#rtoc-8">軸ラベルの順番を変える</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-9">y軸を反転させる</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-10">三角行列で表示をしたい時</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-11">上三角行列</a></li><li class="rtoc-item"><a href="#rtoc-12">下三角行列</a></li></ul></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">データの準備をします</h2>



<p>相関行列を使います。デモのためにこの記事ではmtcarsのデータを使ってみます。</p>



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



<h3 id="rtoc-2"  class="wp-block-heading">mtcarsとは</h3>



<div class="wp-block-jin-gb-block-box concept-box5">
<p>mtcarsは1974年にMotor Trends US Magazineという雑誌から抽出された、各車種における燃費などの車性能が入ったデータです。</p>



<p>mpg:燃費、cyl:気筒数、disp:排気量などのデータが入っています。</p>
</div>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
&gt; 
</pre></div>


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



<h3 id="rtoc-3"  class="wp-block-heading">相関行列の生成</h3>



<p>相関行列を生成してみます。</p>



<p>mtcarsのデータからSpearmanの順位相関係数をみてみます。またggplot2で使いやすいようにデータを変形していきます。相関はcorで計算をしており、その中のmethodでspearmanと指定しています。</p>



<p>縦のデータへの変換はpivot_longerを用いています。pivot_longerは次の記事に詳しい説明があります。</p>



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


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# tidyverseの起動
library(tidyverse)

#mtcars
a &lt;- mtcars

#相関行列(Spearman)
cormat &lt;- cor(a, method = &quot;spearman&quot;)

#データフレーム化
df &lt;- data.frame(cormat) %&gt;% 

#列名の抽出
tibble::rownames_to_column(var = &quot;Y&quot;)

#縦のデータへ
df.long &lt;- pivot_longer(df, 
                            cols = mpg:carb,
                            names_to = &quot;X&quot;, 
                            values_to = &quot;rho&quot;)

#データの確認
&gt; head(df.long)
# A tibble: 6 x 3
  Y     X        rho
  &lt;fct&gt; &lt;fct&gt;  &lt;dbl&gt;
1 mpg   mpg    1    
2 mpg   cyl   -0.911
3 mpg   disp  -0.909
4 mpg   hp    -0.895
5 mpg   drat   0.651
6 mpg   wt    -0.886
&gt; 


</pre></div>


<p>以上のようなデータになりました。自分のデータで相関行列があればそれを縦のデータに変換して利用することができます。</p>



<h2 id="rtoc-4"  class="wp-block-heading">geom_tileを使って相関行列のヒートマップを描く</h2>



<p>geom_tile()を使うことで視覚化することができます。</p>



<p>次のようになります。</p>



<h3 id="rtoc-5"  class="wp-block-heading">geoｍ_tile()を使う</h3>


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


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



<h3 id="rtoc-6"  class="wp-block-heading">scale_fill_gradientで色の変更をする</h3>



<p>色を変更してみます。よく見るのは値の高いところが赤、低いところが青ですよね？</p>



<p>scale_fill_gradient2を使ってみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x = X, y = Y, fill = rho)) + 
  geom_tile(color = &quot;white&quot;) + 
  scale_fill_gradient2(low = &quot;blue&quot;, high = &quot;red&quot;, mid = &quot;white&quot;, 
                       midpoint = 0, limit = c(-1, 1), 
                       name = &quot;Rho&quot;, space = &quot;Lab&quot;)

</pre></div>


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



<p> これで出来ました！</p>



<p>ところでここに数値を入れたい人もいるかもしれません。</p>



<h2 id="rtoc-7"  class="wp-block-heading">ヒートマップの中に数値を表示する</h2>



<p>数値を入れるにはgeom_text()で出来ます！</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
df.long %&gt;% 
  ggplot(aes(x = X, y = Y, fill = rho)) + 
  geom_tile(color = &quot;white&quot;) + 
  scale_fill_gradient2(low = &quot;blue&quot;, high = &quot;red&quot;, mid = &quot;white&quot;, 
                       midpoint = 0, limit = c(-1, 1), name = &quot;Rho&quot;, space = &quot;Lab&quot;)+
  geom_text(aes(label = sprintf(&quot;%0.2f&quot;, rho)),
            color = &quot;black&quot;, size = 2.5)
</pre></div>


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



<h2 id="rtoc-8"  class="wp-block-heading">軸ラベルの順番を変える</h2>



<p>軸ラベルがアルファベット順になっていることに気がついたでしょうか。</p>



<p>これが不便に感じるときがあります。そういうときはfactor(data, levels = )で軸ラベルの順番を指定すれば解決です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
lbs &lt;- df&#91;,1]
df.long$X &lt;- factor(df.long$X, levels = lbs)
df.long$Y &lt;- factor(df.long$Y, levels = lbs)

df.long %&gt;% 
  ggplot(aes(x = X, y = Y, fill = rho)) + 
  geom_tile(color = &quot;white&quot;) + 
  scale_fill_gradient2(low = &quot;blue&quot;, high = &quot;red&quot;, mid = &quot;white&quot;, 
                       midpoint = 0, limit = c(-1, 1), name = &quot;Rho&quot;, space = &quot;Lab&quot;)+
  geom_text(aes(label = sprintf(&quot;%0.2f&quot;, rho)),
            color = &quot;black&quot;, size = 2.5)
</pre></div>


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



<p>順番はdata &lt;- factor(data, levels = c(&#8220;mpg&#8221;, &#8220;cyl&#8221;, &#8220;disp&#8221;, &#8230;.))というように一つ一つ指定することもできます。</p>



<h3 id="rtoc-9"  class="wp-block-heading">y軸を反転させる</h3>



<p>上のグラフは左下からラベルが並んでいます。y軸は上からはじまって欲しい場合があります。</p>



<p>そういうときはy軸の並びを反転させます。</p>



<p>scale_y_discrete(limits = rev(levels( )))で反転できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
df.long %&gt;% 
  ggplot(aes(x = X, y = Y, fill = rho)) + 
  geom_tile(color = &quot;white&quot;) + 
  scale_fill_gradient2(low = &quot;blue&quot;, high = &quot;red&quot;, mid = &quot;white&quot;, 
                       midpoint = 0, limit = c(-1, 1), name = &quot;Rho&quot;, space = &quot;Lab&quot;)+
  geom_text(aes(label = sprintf(&quot;%0.2f&quot;, rho)),
            color = &quot;black&quot;, size = 2.5)+
  scale_y_discrete(limits = rev(levels(df.long$Y)))
</pre></div>


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



<h2 id="rtoc-10"  class="wp-block-heading">三角行列で表示をしたい時</h2>



<p>上の図では対角線で対称になっていますが、余計な情報だと思うかもしれません。それであれば三角行列にしてみます。</p>



<p>ただし、ggplotで三角行列にするというより、データ自体を三角行列になるようにしていきます。</p>



<h3 id="rtoc-11"  class="wp-block-heading">上三角行列</h3>



<p> 上三角行列にするには下の部分をNAにして、その部分を空欄にします。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# 上三角行列にするためのfunction
# 下にはNAを代入する
get_upper_tri &lt;- function(cormat){
  cormat&#91;lower.tri(cormat)]&lt;- NA
  return(cormat)
}

# 実際のデータに適応
upper_tri &lt;- get_upper_tri(cormat)

# データの変形
df_upper_tri &lt;- data.frame(upper_tri) %&gt;% 
  tibble::rownames_to_column(var = &quot;Y&quot;)

df_upper_tri.long &lt;- pivot_longer(df_upper_tri, 
                        cols = mpg:carb,
                        names_to = &quot;X&quot;, 
                        values_to = &quot;rho&quot;)

# ラベルの並び替え
lbs2 &lt;- df_upper_tri&#91;,1]
df_upper_tri.long$X &lt;- factor(df_upper_tri.long$X, levels = lbs2)
df_upper_tri.long$Y &lt;- factor(df_upper_tri.long$Y, levels = lbs2)

# 作図
df_upper_tri.long %&gt;% 
  ggplot(aes(x = X, y = Y, fill = rho)) + 
  geom_tile(color = &quot;white&quot;) + 
  scale_fill_gradient2(low = &quot;blue&quot;, high = &quot;red&quot;, mid = &quot;white&quot;, 
                       midpoint = 0, limit = c(-1, 1), 
                       name = &quot;Rho&quot;, space = &quot;Lab&quot;,
                       na.value = &quot;white&quot;)+
  geom_text(aes(label = ifelse(is.na(rho) | rho %in% &quot;&quot;,NA, sprintf(&quot;%0.2f&quot;, rho))),
            color = &quot;black&quot;, size = 4.5)+
  scale_y_discrete(limits = rev(levels(df_upper_tri.long$Y)))+
  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/06/cormat6-1024x819.jpg" alt="" class="wp-image-980" width="512" height="410"/></figure>



<h3 id="rtoc-12"  class="wp-block-heading">下三角行列</h3>



<p>下三角も同様に出来ます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# 下三角行列にするためのfunction
# 上にはNAを代入する
get_lower_tri &lt;- function(cormat){
  cormat&#91;upper.tri(cormat)]&lt;- NA
  return(cormat)
}

# 実際のデータに適応
lower_tri &lt;- get_lower_tri(cormat)

# データの変形
df_lower_tri &lt;- data.frame(lower_tri) %&gt;% 
  tibble::rownames_to_column(var = &quot;Y&quot;)

df_lower_tri.long &lt;- pivot_longer(df_lower_tri, 
                                  cols = mpg:carb,
                                  names_to = &quot;X&quot;, 
                                  values_to = &quot;rho&quot;)

# ラベルの並び替え
lbs3 &lt;- df_lower_tri&#91;,1]
df_lower_tri.long$X &lt;- factor(df_lower_tri.long$X, levels = lbs3)
df_lower_tri.long$Y &lt;- factor(df_lower_tri.long$Y, levels = lbs3)

# 作図
df_lower_tri.long %&gt;% 
  ggplot(aes(x = X, y = Y, fill = rho)) + 
  geom_tile(color = &quot;white&quot;) + 
  scale_fill_gradient2(low = &quot;blue&quot;, high = &quot;red&quot;, mid = &quot;white&quot;, 
                       midpoint = 0, limit = c(-1, 1), 
                       name = &quot;Rho&quot;, space = &quot;Lab&quot;,
                       na.value = &quot;white&quot;)+
  geom_text(aes(label = ifelse(is.na(rho) | rho %in% &quot;&quot;,NA, sprintf(&quot;%0.2f&quot;, rho))),
            color = &quot;black&quot;, size = 2.5)+
  scale_y_discrete(limits = rev(levels(df_lower_tri.long$Y)))+
  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/06/cormat7-1024x819.jpg" alt="" class="wp-image-982" width="512" height="410"/></figure>



<p>ifelseの使い方が初心者には難しいかもしれませんが、慣れれば特定の閾値以下は表示しない、などのテクニックなども使えます。</p>



<p>便利なグラフなので、覚えて損はありません。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/corrheatmap/942/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>【Rテクニック】facet_gripとfacet_wrapの使い方とタイトルラベルを変えたい！</title>
		<link>https://brain-storm.space/r_facet/843/</link>
					<comments>https://brain-storm.space/r_facet/843/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Sat, 29 May 2021 03:29:46 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[facet_grid]]></category>
		<category><![CDATA[facet_wrap]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=843</guid>

					<description><![CDATA[facet_gripとfacet_wrapは複数グラフを一度に作成できるのでとても便利です！ またビジュアルとしてとても有用で、魅力的なグラフになります。 タイトルラベルを変えるのは少しテクニックがありますので、それを含]]></description>
										<content:encoded><![CDATA[
<p><span class="marker">facet_gripとfacet_wrap</span>は複数グラフを一度に作成できるのでとても便利です！</p>



<p>またビジュアルとしてとても有用で、魅力的なグラフになります。</p>



<p>タイトルラベルを変えるのは少しテクニックがありますので、それを含めて紹介します。</p>



<p>それではいってみましょう！</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="843" 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">facet_gridとfacet_wrapの使い方</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-2">facet_gridを使ってみる！</a></li><li class="rtoc-item"><a href="#rtoc-3">facet_wrapを使ってみる！</a></li><li class="rtoc-item"><a href="#rtoc-4">スケールを別々にする方法</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-5">ファセットラベルの変更の仕方</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-6">labellerの設定をする</a></li><li class="rtoc-item"><a href="#rtoc-7">タイトル、サブタイトルをつける！</a></li></ul></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">facet_gridとfacet_wrapの使い方</h2>



<p>facet_gridとfacet_wrapはどちらもデータをグループごとに並べるものになります。いつも便利なiris dataを用いていきます。</p>



<h3 id="rtoc-2"  class="wp-block-heading">facet_gridを使ってみる！</h3>



<p>まずはデータの準備をします。</p>



<p>tidyverseを起動しています。</p>


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

#libraryの起動
library(tidyverse)

#irisデータ
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>まずはfacet_gridの使い方を見てみましょう！</p>



<p>まずは水平方向です。</p>



<p>facet_gridのカッコの中を(. ~ Species)で指定しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, 
              y = Sepal.Width, 
              color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = TRUE)+
  facet_grid(. ~ Species)

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_grid1-1024x384.jpg" alt="" class="wp-image-848"/></figure>



<p>次は垂直方向です！</p>



<p>これは反対にfacet_grid(Species ~ .)で指定しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = TRUE)+
  facet_grid(Species ~ .)

</pre></div>


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



<p>このような感じになりました。</p>



<p>今回は１行または１列で表示しましたが、それぞれ垂直方向、水平方向を指定して複数の行列で表示することもできます。</p>



<h3 id="rtoc-3"  class="wp-block-heading">facet_wrapを使ってみる！</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = TRUE)+
  facet_wrap(~ Species)  #ドットは不要です

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="384" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap1-1024x384.jpg" alt="" class="wp-image-851"/></figure>



<p>２行にしてみましょう。</p>



<p>facet_wrapのカッコの中のncolまたはnrowで指定できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = TRUE)+
  facet_wrap(~ Species, nrow = 2)

</pre></div>


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



<h3 id="rtoc-4"  class="wp-block-heading">スケールを別々にする方法</h3>



<p>スケールが合わさっていると極端に偏ったグラフが出来てしまうことがあります。</p>



<p>そのときはscales = &#8220;free&#8221;を設定します。</p>



<div class="wp-block-jin-gb-block-icon-box jin-icon-caution jin-iconbox"><div class="jin-iconbox-icons"><i class="jic jin-ifont-caution jin-icons"></i></div><div class="jin-iconbox-main">
<p>ただし、逆に見にくくなることもあるので注意をしてください。</p>
</div></div>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap3-1024x384.jpg" alt="" class="wp-image-854" width="840" height="315"/></figure>



<h2 id="rtoc-5"  class="wp-block-heading">ファセットラベルの変更の仕方</h2>



<p>これを変更する一つの方法はデータを変更してしまうことです。</p>



<p><span class="marker">もう一つの方法として、labellerを指定します</span>！</p>



<p>早速やり方を見ていきましょう。</p>



<h3 id="rtoc-6"  class="wp-block-heading">labellerの設定をする</h3>



<p>facet_gridまたはfacet_wrapでlabeller = labellar()で指定をすることができます。</p>



<p>下記の方法は少しテクニックが必要ですが、真似をするだけで変更できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
Att.labs &lt;- c(&quot;Att:Setosa&quot;, 
              &quot;Att:Versicolor&quot;, 
              &quot;Att:Virginica&quot;)
names(Att.labs) &lt;- c(&quot;setosa&quot;,
                      &quot;versicolor&quot;,
                      &quot;virginica&quot;)

ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(~ Species,  scales = &quot;free&quot;, 
             labeller = labeller(Species = Att.labs))

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="384" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap4-1024x384.jpg" alt="" class="wp-image-855"/></figure>



<p>またタイトルやサブタイトルもつけることができます</p>



<h3 id="rtoc-7"  class="wp-block-heading">タイトル、サブタイトルをつける！</h3>



<p>タイトルのラベルはlabs(title = )でしていできます。</p>



<p>また同様にlabsでサブタイトルやx軸、y軸のラベルを指定することができます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(~ Species,  scales = &quot;free&quot;, 
             labeller = labeller(Species = Att.labs)
  )+
  labs(title = &quot;Iris data analysis&quot;, subtitle = &quot;Sepal measurement&quot;,
       x = &quot;Length&quot;,
       y = &quot;Width&quot;)
</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="384" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap5-1024x384.jpg" alt="" class="wp-image-856"/></figure>



<p>フォントサイズを整えます。またtheme()でその大きさをそれぞれ指定できます。</p>



<p>レジェンドもあんまり役に立っていないので、なくしちゃいましょう。その時はlegend.position = &#8220;none&#8221;とすることで表示をなくします。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(. ~ Species,  scales = &quot;free&quot;, 
             labeller = labeller(Species = Att.labs))+
  labs(title = &quot;Iris data analysis&quot;, 
       subtitle = &quot;Sepal measurement&quot;,
       x = &quot;Length&quot;, y = &quot;Width&quot;)+
  theme(axis.text.x = element_text(vjust = 1, size = 16, 
                                   hjust = 1, face = &quot;bold&quot;),
        axis.text.y = element_text(hjust = 1, size = 16),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.title = element_text(size=16),
        legend.text = element_text(size=16),
        strip.text.x = element_text(size = 20),
        plot.title = element_text(size=22),
        plot.subtitle = element_text(size=20),
        legend.position = &quot;none&quot;)

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="410" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap6-1024x410.jpg" alt="" class="wp-image-857"/></figure>



<p>スケールメモリを統一したほうが実際は見やすいことが多いです。</p>



<p>最後にスケールメモリを統一してみましょう。</p>



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


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(. ~ Species,
             labeller = labeller(Species = Att.labs))+
  labs(title = &quot;Iris data analysis&quot;, 
       subtitle = &quot;Sepal measurement&quot;,
       x = &quot;Length&quot;, y = &quot;Width&quot;)+
  theme_classic() +
  theme(axis.text.x = element_text(vjust = 1, size = 16, 
                                   hjust = 1, face = &quot;bold&quot;),
        axis.text.y = element_text(hjust = 1, size = 16),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.title = element_text(size=16),
        legend.text = element_text(size=16),
        strip.text.x = element_text(size = 20),
        plot.title = element_text(size=22),
        plot.subtitle = element_text(size=20),
        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/facet_wrap7-1024x512.jpg" alt="" class="wp-image-935" width="768" height="384"/></figure>



<p>それぞれを比較するにはとても見やすくなったと思います。</p>



<p>facet_gridとfacet_wrapはとても便利ですよね！</p>



<p>お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/r_facet/843/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【R初心者向け】ggplot2でもbeeswarmで作図したい！ggbeeswarmを使った方法</title>
		<link>https://brain-storm.space/ggbeeswarm/820/</link>
					<comments>https://brain-storm.space/ggbeeswarm/820/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Mon, 24 May 2021 23:00:00 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[ggbeeswarm]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=820</guid>

					<description><![CDATA[ggplot2でドットプロットを紹介しましたが、ggplot2でbeeswarmに出来ないでしょうか、という質問をいただきました。 今回はggplot2でbeeswarmを描く方法を紹介します。ggbeeswarmという]]></description>
										<content:encoded><![CDATA[
<p>ggplot2でドットプロットを紹介しましたが、ggplot2でbeeswarmに出来ないでしょうか、という質問をいただきました。</p>



<p>今回は<span class="marker">ggplot2でbeeswarmを描く方法</span>を紹介します。ggbeeswarmというパッケージを使えばできます。</p>



<p>過去にggplot2を使ったドットプロットの書き方と、ggplot2を使わずbeeswarmパッケージを使った方法を紹介しています。それについてはこちらを参考にしてください。</p>



https://brain-storm.space/rggplot2_boxplot_dotplot/620/



https://brain-storm.space/beeswarm_r/698/



<p>それでは早速ggplot2でbeeswarmを書いていきましょう。</p>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="820" 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">ggbeeswarmのgeom_beeswarmを使う</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">ggbeeswarmを使っていないドットプロット</a></li><li class="rtoc-item"><a href="#rtoc-4">geom_beeswarmを使う</a></li><li class="rtoc-item"><a href="#rtoc-5">geom_quasirandomを使う</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-6">methodによる違い</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-7">geom_beeswarm</a></li><li class="rtoc-item"><a href="#rtoc-8">quasirandom</a></li><li class="rtoc-item"><a href="#rtoc-9">pseudorandom</a></li><li class="rtoc-item"><a href="#rtoc-10">smiley</a></li><li class="rtoc-item"><a href="#rtoc-11">frowney</a></li><li class="rtoc-item"><a href="#rtoc-12">tukey</a></li><li class="rtoc-item"><a href="#rtoc-13">tukeyDense</a></li></ul></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">データ準備とパッケージのインストール</h2>



<p>まずはtidyverseとggbeeswarmをいれていきます。ggplot2はtidyverseの中に入っています。</p>



<p>まずは作業ディレクトリの設定</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
setwd(&quot;~/Rpractice/&quot;)
</pre></div>


<p>ggbeeswarmをCRANからインストールをします。</p>



<p>tidyverseやgglot2はすでにインストールされている前提ですすめていきますね。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
install.packages(&quot;ggbeeswarm&quot;)
</pre></div>


<p>それではパッケージを起動します</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
library(tidyverse)
library(ggbeeswarm)
</pre></div>


<p>アヤメデータを使います</p>



<div class="wp-block-jin-gb-block-box concept-box5">
<p>アヤメはアヤメ科の植物ですが、英語でiris(アイリス)といいます</p>



<p>ギリシャ語の虹(イリス)に由来するそうです</p>
</div>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
#irisデータをaに格納
a &lt;- iris
#ボックスプロットを描画
ggplot(a, aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_boxplot(fill = &quot;white&quot;)+
  theme_classic()
</pre></div>


<h2 id="rtoc-2"  class="wp-block-heading">ggbeeswarmのgeom_beeswarmを使う</h2>



<h3 id="rtoc-3"  class="wp-block-heading">ggbeeswarmを使っていないドットプロット</h3>



<p>以前の記事でggplotを用いて書いた図です。ggplotでのボックスプロットはこちらを参考にしてください。</p>



https://brain-storm.space/rggplot2_boxplot_dotplot/620/


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_boxplot(fill = &quot;white&quot;)+
  geom_dotplot(binaxis = &quot;y&quot;, binwidth = 0.1, stackdir = &quot;center&quot;)+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 50, vjust = 1, size = 16, hjust = 1, face = &quot;bold&quot;),
        axis.text.y = element_text(hjust = 1, size = 16),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.title = element_text(size =16),
        legend.text = element_text(size =16))
</pre></div>


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



<p>それではggbeeswarmを使っていきたいと思います。</p>



<h3 id="rtoc-4"  class="wp-block-heading">geom_beeswarmを使う</h3>



<p>geom_beeswarmを使ってみましょう。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x=Species, y=Sepal.Length))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_beeswarm()+
  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/beeswarm1-2-1024x1024.jpg" alt="" class="wp-image-829" width="512" height="512"/></figure>



<p>いまいちですね。色をつけてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x=Species, y=Sepal.Length))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_beeswarm(aes(color = Species))+
  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/beeswarm2-1024x1024.jpg" alt="" class="wp-image-830" width="512" height="512"/></figure>



<p>点が小さいし、点どうしが重なっています！これでは使い物になりませんので、大きくして、もう少し点をちらしてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Species, y = Sepal.Length))+
  geom_boxplot(fill = &quot;white&quot;)+
  geom_beeswarm(aes(color = Species), size = 3, cex = 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/beeswarm3-1024x1024.jpg" alt="" class="wp-image-831" width="512" height="512"/></figure>



<p>まあまあいいですが、このグラフではbeeswarmの良さが見えてこないかもしれません。下でもう少し別のセットを使って示してみます。</p>



<h3 id="rtoc-5"  class="wp-block-heading">geom_quasirandomを使う</h3>



<p>今度はggbeeswarmパッケージに入っているgeom_quasirandomを使ってみます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Species, y = Sepal.Length))+
  geom_boxplot(fill = &quot;white&quot;)+
  geom_quasirandom()+
  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/quasirandom1-1-1024x1024.jpg" alt="" class="wp-image-828" width="512" height="512"/></figure>



<p>結構点がバラバラに表示をされました</p>



<p>色と点の大きさを変えてみましょう</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Species, y = Sepal.Length))+
  geom_boxplot(fill = &quot;white&quot;)+
  geom_quasirandom(aes(color = Species), 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/beeswarm4-1024x1024.jpg" alt="" class="wp-image-833" width="512" height="512"/></figure>



<h2 id="rtoc-6"  class="wp-block-heading">methodによる違い</h2>



<p>点の数は多いほうが、ggbeeswarmの特徴がよくわかります。rnormを使って、さらに点の数が多い分布を作ってみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
dat &lt;- list(
  X &lt;- rnorm(150, 10, 10),
  Y &lt;- rnorm(150, 25, 10),
  Z &lt;- rnorm(150, 20, 15)
)
df &lt;- data.frame(matrix(unlist(dat), nrow=150))
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;)
</pre></div>


<p>ではいってみましょう。pivot_longerの使い方はこちらから。</p>



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



<h3 id="rtoc-7"  class="wp-block-heading">geom_beeswarm</h3>


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


<p>重なりを表示するためにalphaを指定して半透明にしています。</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/method_beeswarm_mod-1024x768.jpg" alt="" class="wp-image-863" width="768" height="576"/></figure>



<p>beeswarmの良さがみえてきました。十分発表や論文に使えるレベルだと思います。</p>



<h3 id="rtoc-8"  class="wp-block-heading">quasirandom</h3>



<p>geom_quasirandomでいくつかのmethodを指定することができます。</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_boxplot(fill=&quot;white&quot;)+
  geom_quasirandom(aes(color = Categories),
                   size = 3, 
                   alpha =.5)+
  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/method_quasirandom_mod-1024x768.jpg" alt="" class="wp-image-864" width="768" height="576"/></figure>



<p>次は他のいくつかの方法を示していきます。</p>



<h3 id="rtoc-9"  class="wp-block-heading">pseudorandom</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x=Categories, y = Values))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_quasirandom(aes(color = Categories),
                   method ='pseudorandom', 
                   size = 3, 
                   alpha =.5)+
  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/method_pseudorandom_mod-1024x768.jpg" alt="" class="wp-image-865" width="768" height="576"/></figure>



<h3 id="rtoc-10"  class="wp-block-heading">smiley</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))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_quasirandom(aes(color = Categories),
                   method ='smiley', 
                   size = 3, 
                   alpha =.5)+
  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/method_smiley_mod-1024x768.jpg" alt="" class="wp-image-866" width="768" height="576"/></figure>



<p>口角が上がっているようにみえます。</p>



<h3 id="rtoc-11"  class="wp-block-heading">frowney</h3>



<p>smileyとは反対に口角が下がっているような図になります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x=Categories, y = Values))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_quasirandom(aes(color = Categories),
                   method ='frowney', 
                   size = 3, 
                   alpha =.5)+
  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/method_frowney_mod-1024x768.jpg" alt="" class="wp-image-867" width="768" height="576"/></figure>



<p>おもしろい名前ですよね。</p>



<h3 id="rtoc-12"  class="wp-block-heading">tukey</h3>



<p>turkeyという方法です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x=Categories, y = Values))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_quasirandom(aes(color = Categories),
                   method ='tukey', 
                   size = 3, 
                   alpha =.5)+
  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/method_tukey_mod-1024x768.jpg" alt="" class="wp-image-868" width="768" height="576"/></figure>



<h3 id="rtoc-13"  class="wp-block-heading">tukeyDense</h3>



<p>最後にtukeyDenseを紹介します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(df.long, aes(x=Categories, y = Values))+
  geom_boxplot(fill=&quot;white&quot;)+
  geom_quasirandom(aes(color = Categories),
                   method ='tukeyDense', 
                   size = 3, 
                   alpha =.5)+
  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/method_tukeyDense_mod-1024x768.jpg" alt="" class="wp-image-869" width="768" height="576"/></figure>



<p>ggplotをベースにしたggbeeswarmはキレイですね。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/ggbeeswarm/820/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
