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

<image>
	<url>https://brain-storm.space/wp-content/uploads/2021/04/cropped-3d0209af428738b78799159b4ce75ad9-32x32.png</url>
	<title>【facet_wrap】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
