<?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>【ggbeeswarm】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/ggbeeswarm/feed/" rel="self" type="application/rss+xml" />
	<link>https://brain-storm.space</link>
	<description>脳や研究について発信するブログです。This site is for research and statistics.</description>
	<lastBuildDate>Wed, 01 Mar 2023 14:06:47 +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>【ggbeeswarm】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
