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

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



<h3 id="rtoc-2" >まずは書いてみます</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" >色を塗る</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" >枠線の色を塗る</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" >グラフ外まで領域が広がっている時</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" >複数の領域を書きたい時</h2>



<h3 id="rtoc-7" >この時はデータフレームで領域を指定すると楽になります</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" >他のグラフの背景にする</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>
	</channel>
</rss>
