<?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>【scatter plot】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/scatter-plot/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>【scatter plot】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【R初心者必見】ggplotを使ってきれいな図で散布図と近似直線を書く</title>
		<link>https://brain-storm.space/rggplot_scatterplot/751/</link>
					<comments>https://brain-storm.space/rggplot_scatterplot/751/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Fri, 21 May 2021 01:34:29 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[scatter plot]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=751</guid>

					<description><![CDATA[相関の図を書くときに何のソフトを使っているでしょうか？ ggplot2を使えば無料でより見栄えの良い図を書くことができます それでは早速いってみましょう Contents データの準備散布図(scatter plot)の]]></description>
										<content:encoded><![CDATA[
<p>相関の図を書くときに何のソフトを使っているでしょうか？</p>



<p>ggplot2を使えば無料でより見栄えの良い図を書くことができます</p>



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



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="751" 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">散布図(scatter plot)の書き方</a><ul class="rtoc-mokuji mokuji_ul level-2"><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">geoms_pointでsizeとalphaを指定して点の大きさと透明度を変更できます</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">geom_smoothまたはstat_smoothで近似曲線を表示できます</a></li><li class="rtoc-item"><a href="#rtoc-8">geom_jitterで点同士の重なりをずらすことができます</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-9">facet_gridを使って複数のグラフを表示</a></li></ol></div><h2 id="rtoc-1"  class="wp-block-heading">データの準備</h2>



<p>他の記事と同様に作業ディレクトリを設定して、ggplot2をいれます</p>



<p>なにかと便利なtidyverseを使っていきます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
setwd(&quot;~/OneDrive/blog/Rpractice/&quot;)
library(tidyverse)
a &lt;- iris
head(a)

&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>準備ができました</p>



<p>がく片の長さとがく片の幅には相関があるかどうかみてみましょう</p>



<p>ここではまず実際のデータの分布をみてみましょう</p>



<h2 id="rtoc-2"  class="wp-block-heading">散布図(scatter plot)の書き方</h2>



<p>まずは散布図を書いてみましょう</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()
</pre></div>


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



<p>パッと見た感じ相関はありそうですが、島ができているのでこのまま単純に相関係数を出すことに意味はなさそうです</p>



<h3 id="rtoc-3"  class="wp-block-heading">種類ごと色分けをする</h3>



<p>一度種類によって色分けをするのがよさそうですね</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()
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/scatterplot2-2-1024x819.jpg" alt="" class="wp-image-757" width="625" height="500"/></figure>



<div class="wp-block-jin-gb-block-box concept-box2">
<p>他の人にみてもらうためのグラフは、<br>色盲の方でも判別できるものを心がけなくてはいけません</p>



<p>shapeも変えたり工夫することが大切です</p>
</div>



<h3 id="rtoc-4"  class="wp-block-heading">種類ごとに形を変える</h3>



<p>shapeで形を種類ごとに変えてみます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              shape = Species , color = Species)) +
  geom_point()
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/scatterplot3-1024x819.jpg" alt="" class="wp-image-760" width="625" height="500"/></figure>



<h3 id="rtoc-5"  class="wp-block-heading">geoms_pointでsizeとalphaを指定して点の大きさと透明度を変更できます</h3>


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


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/scatterplot5-1024x819.jpg" alt="" class="wp-image-762" width="625" height="500"/></figure>



<h2 id="rtoc-6"  class="wp-block-heading">近似曲線や近似直線を加える</h2>



<h3 id="rtoc-7"  class="wp-block-heading">geom_smoothまたはstat_smoothで近似曲線を表示できます</h3>



<p>seは信頼区間を表示するかどうかです(ここでは非表示にします)</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,
              shape = Species , color = Species)) +
  geom_point(size = 5, alpha = .5)+
  geom_smooth(se = FALSE)+
  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/scatterplot6-1024x819.jpg" alt="" class="wp-image-763" width="625" height="500"/></figure>



<p>線形近似でもよさそうですね</p>



<p>geom_smoothのmethodでlmを指定します</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              shape = Species , color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  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/scatterplot7-1024x819.jpg" alt="" class="wp-image-764" width="625" height="500"/></figure>



<p>一種類のみを表示してみましょう</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>



<p>tidyverseの中になるdplyrの機能の一つ、filterを使ってみます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
b &lt;- filter(a, Species == &quot;setosa&quot;)
view(b)
</pre></div>


<p>Speciesの列がsetosaだけになっていましたでしょうか？</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>



<p>それでは表示をしてみましょう</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  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/scatterplot8-1024x1024.jpg" alt="" class="wp-image-765" width="512" height="512"/></figure>



<h3 id="rtoc-8"  class="wp-block-heading">geom_jitterで点同士の重なりをずらすことができます</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_smooth(method = lm, se = FALSE, colour = &quot;red&quot;)+
  geom_jitter(colour=&quot;blue&quot;, 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/scatterplot9-1-1024x1024.jpg" alt="" class="wp-image-767" width="512" height="512"/></figure>



<p>どの程度ずらすか指定できます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_smooth(method = lm, se = FALSE, colour = &quot;red&quot;)+
  geom_jitter(colour=&quot;blue&quot;, size = 3, alpha = .5, 
              width = 0.2, height = 0.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/scatterplot11-1-1024x1024.jpg" alt="" class="wp-image-771" width="512" height="512"/></figure>



<div class="wp-block-jin-gb-block-box concept-box2">
<p>geom_jitterでは正確な点の配置ではなくなります</p>



<p>特に論文などの場合には使う場面に注意をしてください</p>
</div>



<p>geom_pointに戻して、geom_smoothのseをTRUEにしてみます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(b, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_smooth(method = lm, se = TRUE, colour = &quot;red&quot;)+
  geom_point(colour=&quot;blue&quot;, 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/scatterplot12-1024x1024.jpg" alt="" class="wp-image-772" width="512" height="512"/></figure>



<p>結構いいですよね</p>



<div class="wp-block-jin-gb-block-box concept-box1">
<p>点を半透明にするメリットは重なりを濃淡で表示できることです</p>
</div>



<h2 id="rtoc-9"  class="wp-block-heading">facet_gridを使って複数のグラフを表示</h2>



<p>acet_gridやfacet_wrapは一度に複数のグラフを表示するのに便利です</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              shape = Species , color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_grid(. ~ Species)
</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/scatterplot_facet_grid1-1-1024x410.jpg" alt="" class="wp-image-776"/></figure>



<p>ラベルの文字の大きさを調整します</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width,
              shape = Species , color = Species)) +
  geom_point(size = 5, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_grid(. ~ Species)+
  theme(strip.text.x = element_text(size=16, face=&quot;bold&quot;),
        axis.text.x = element_text(size = 16),
        axis.text.y = element_text(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"><img decoding="async" width="1024" height="410" src="https://brain-storm.space/wp-content/uploads/2021/05/scatterplot_facet_grid2-1024x410.jpg" alt="" class="wp-image-777"/></figure>



<p>scale_color_brewerで色を変えてみます</p>



<p>scale_color_manualなどで個別に指定などもできます</p>



<p>背景を今回はtheme_minimal()にもしてみます</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              shape = Species , color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  scale_color_brewer(palette = &quot;Set1&quot;)+
  facet_grid(. ~ Species)+
  theme_minimal()+
  theme(strip.text.x = element_text(size=16, face=&quot;bold&quot;),
        axis.text.x = element_text(size = 16),
        axis.text.y = element_text(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"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/scatterplot_facet_grid3-1024x410.jpg" alt="" class="wp-image-778"/></figure>



<p>見やすくなりましたね</p>



<p>ggplot2に慣れてしまえば、いいグラフができます</p>



<p>しかも無料でできますので、覚えて損はないですよ</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/rggplot_scatterplot/751/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
