<?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>【R】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/r/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 17:13:38 +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>【R】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[R beginners] Drawing ROC curve</title>
		<link>https://brain-storm.space/roc-curve-en/1304/</link>
					<comments>https://brain-storm.space/roc-curve-en/1304/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Sat, 25 Mar 2023 07:00:00 +0000</pubDate>
				<category><![CDATA[Statistical Analysis]]></category>
		<category><![CDATA[pROC]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[ROC]]></category>
		<category><![CDATA[sensitivity]]></category>
		<category><![CDATA[specificity]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1304</guid>

					<description><![CDATA[I will introduce how to draw an ROC curve. In R, it is very easy to draw an ROC curve. There are many librarie]]></description>
										<content:encoded><![CDATA[
<p>I will introduce how to draw an ROC curve. In R, it is very easy to draw an ROC curve. There are many libraries available for drawing ROC curves, but this time I will introduce how to draw it using the pROC library, which is commonly used and easy to use.</p>



<h3 id="rtoc-1"  class="wp-block-heading">Creating Sample Data</h3>



<p>This section is about creating sample data. If you don&#8217;t need it, please skip to the next section. Let&#8217;s consider how well we can detect people with the disease using two assays, &#8220;assay1&#8221; and &#8220;assay2&#8221;.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
set.seed(3)
condition &lt;- rep(c(&quot;healthy&quot;, &quot;disease&quot;), each = 50)
assay1 &lt;- c(rnorm(40, mean = 1, sd = 2), 
            rnorm(10, mean = 3, sd = 1), 
            rnorm(40, mean = 5, sd = 2), 
            rnorm(10, mean = 7, sd = 1))
assay2 &lt;- c(rnorm(30, mean = 1, sd = 3), 
            rnorm(20, mean = 2, sd = 2), 
            rnorm(30, mean = 3, sd = 3), 
            rnorm(20, mean = 4, sd = 2))

df = data.frame(condition, assay1, assay2)
</pre></div>


<p>these distributions would look like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
library(beeswarm)
beeswarm(data = df, assay1 ~ condition, 
         col = c(&quot;red&quot;,&quot;blue&quot;), pch=19)
beeswarm(data = df, assay2 ~ condition,
         col = c(&quot;red&quot;, &quot;blue&quot;), pch=19)
</pre></div>


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



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



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1304" data-theme="jin-child">
			<div id="rtoc-mokuji-title" class=" rtoc_left">
			<button class="rtoc_open_close rtoc_open"></button>
			<span>Contents</span>
			</div><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-1">Creating Sample Data</a></li><li class="rtoc-item"><a href="#rtoc-2">Use pROC</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">Install pROC package</a></li><li class="rtoc-item"><a href="#rtoc-4">To draw a single ROC curve</a></li><li class="rtoc-item"><a href="#rtoc-5">Convert to Percentage Display</a></li><li class="rtoc-item"><a href="#rtoc-6">Display Confidence Interval of Sensitivity</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-7">Display Optimal Cutoff Point</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-8">display Optimal Cutoff based on Youden Index</a></li><li class="rtoc-item"><a href="#rtoc-9">Display Optimal Cutoff using Top Left Method</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-10">Extracting Necessary Values</a></li><li class="rtoc-item"><a href="#rtoc-11">Comparing two ROC curves by overlapping them</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-12">Overlay Two ROC Curves</a></li><li class="rtoc-item"><a href="#rtoc-13">Compare Two Assays</a></li></ul></li></ol></div><h2 id="rtoc-2"  class="wp-block-heading">Use pROC</h2>



<h3 id="rtoc-3"  class="wp-block-heading">Install pROC package</h3>



<p>First, you need to install and load the pROC package to use it in R.</p>


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


<h3 id="rtoc-4"  class="wp-block-heading">To draw a single ROC curve</h3>



<p>The data frame looks like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; head(df)
  condition     assay1     assay2
1   healthy -0.9238668  2.8520501
2   healthy  0.4149486 -0.2152325
3   healthy  1.5175764  4.1593113
4   healthy -1.3042638  2.8068527
5   healthy  1.3915657  4.0523835
6   healthy  1.0602479  2.8245020
</pre></div>


<p>There are several ways to proceed from here, but first let&#8217;s use roc() to store the result in an object.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
</pre></div>


<p>As an alternative, you can also use the following notation with &#8220;~&#8221;:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(df$condition ~ df$assay1, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
</pre></div>


<p>The <code>condition</code> column can be 0 and 1, as it is a binary variable. <code>ci</code> refers to the confidence interval.</p>



<p>you can use the <code>plot()</code> function to draw the ROC curve.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1)
</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/roc_1.jpeg" alt="" class="wp-image-1224"/><figcaption class="wp-element-caption">Assay1</figcaption></figure>



<h3 id="rtoc-5"  class="wp-block-heading">Convert to Percentage Display</h3>



<p>You can customize the axis to display in percentage format by adding <code>percent=TRUE</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, percent=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
plot(roc1)
</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/roc_plot2_percent.jpeg" alt="" class="wp-image-1225"/></figure>



<h3 id="rtoc-6"  class="wp-block-heading">Display Confidence Interval of Sensitivity</h3>



<p>You can display the confidence interval of the sensitivity. To calculate the confidence interval of sensitivity, use <code>ci.se()</code>. <code>col</code> is used to specify the color.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
plot(roc2)
rocCI &lt;- ci.se(roc2)
plot(rocCI, type=&quot;shape&quot;, col=&quot;lightblue&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/roc_ci_3.jpeg" alt="" class="wp-image-1227"/></figure>



<h2 id="rtoc-7"  class="wp-block-heading">Display Optimal Cutoff Point</h2>



<p>One important aspect of the ROC curve is determining the optimal cut-off point. While determining the optimal cut-off point is a big topic on its own, here we introduce two methods: the Youden Index and top left.</p>



<p>To explain briefly, the Youden Index is calculated as &#8220;sensitivity + specificity &#8211; 1&#8221;, and the point with the highest value is selected as the optimal cut-off point. On the other hand, the top left method extracts the point closest to the upper left corner of the ROC curve.</p>



<h3 id="rtoc-8"  class="wp-block-heading">display Optimal Cutoff based on Youden Index</h3>



<p>You can add the optimal cutoff to the plot by modifying the <code>plot()</code> function call. Setting <code>print.thres = "best"</code> and <code>print.thres.best.method = "youden"</code> will display the optimal point. Setting <code>legacy.axes = TRUE</code> will make the x-axis display as 1-Specificity.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1, main = &quot;ROC Curve&quot;,      
     identity = TRUE,
     print.thres = &quot;best&quot;,
     print.thres.best.method = &quot;youden&quot;,
     legacy.axes = TRUE)
</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/roc_youden_5.jpeg" alt="" class="wp-image-1231"/><figcaption class="wp-element-caption">optimal cutoff using the Youden Index (J = Sensitivity + Specificity &#8211; 1)</figcaption></figure>



<p>The optimal cut-off point in this case is 3.912 with sensitivity of 0.820 and specificity of 0.960.</p>



<h3 id="rtoc-9"  class="wp-block-heading">Display Optimal Cutoff using Top Left Method</h3>



<p>In the case of top left, set <code>print.thres.best.method="closest.topleft"</code> in <code>plot()</code> function.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1, main = &quot;ROC Curve&quot;,      
     identity = TRUE,
     print.thres = &quot;best&quot;,
     print.thres.best.method = &quot;closest.topleft&quot;
     legacy.axes = TRUE)

</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/roc_topleft_4.jpeg" alt="" class="wp-image-1234"/><figcaption class="wp-element-caption">The optimal cutoff based on the top left approach</figcaption></figure>



<p>Note that in this case, the optimal cutoff is 3.365, with a sensitivity of 0.860 and specificity of 0.880.</p>



<h2 id="rtoc-10"  class="wp-block-heading">Extracting Necessary Values</h2>



<p>When presenting or publishing the values of AUC (area under the curve), sensitivity, specificity, etc., you can display them as follows.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; auc(roc1)
Area under the curve: 0.9536

# youden
&gt; coords(roc1, &quot;best&quot;, ret=c(&quot;threshold&quot;, &quot;sens&quot;, &quot;spec&quot;, &quot;ppv&quot;, &quot;npv&quot;))
          threshold sensitivity specificity       ppv       npv
threshold  3.912407        0.82        0.96 0.9534884 0.8421053

# topleft
&gt; coords(roc1, &quot;best&quot;, best.method=&quot;closest.topleft&quot;,　ret=c(&quot;threshold&quot;, &quot;sens&quot;, &quot;spec&quot;, &quot;ppv&quot;, &quot;npv&quot;))
          threshold sensitivity specificity      ppv       npv
threshold  3.364778        0.86        0.88 0.877551 0.8627451
</pre></div>


<h2 id="rtoc-11"  class="wp-block-heading">Comparing two ROC curves by overlapping them</h2>



<p>We have created sample data assuming two tests. Let&#8217;s see which test is better by comparing the two ROC curves.</p>



<h3 id="rtoc-12"  class="wp-block-heading">Overlay Two ROC Curves</h3>



<p>When overlapping two ROC curves, you can use the <code>lines()</code> function for the one that you want to add later, or simply use <code>plot(..., add=TRUE)</code> for the second curve. You can specify the color using <code>col</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# Use roc() to create objects for the two assays
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

# Use lines()
obj1 &lt;- plot(roc1,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)

# Use add = TRUE
obj1 &lt;- plot(roc1,
             col=&quot;red&quot;)
obj2 &lt;- plot(roc2,
             col=&quot;blue&quot;,
             add=TRUE)
</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/roc_two_assay_6.jpeg" alt="" class="wp-image-1239"/></figure>



<h3 id="rtoc-13"  class="wp-block-heading">Compare Two Assays</h3>



<p>o compare two assays, use <code>roc.test()</code>. By default, it compares the AUCs (area under the curve) using the DeLong method, but you can change it to the bootstrap method by specifying <code>method = "bootstrap"</code>. The p-value is displayed in the center using text(). You can also specify the title using <code>main=</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

obj1 &lt;- plot(roc1,
             main=&quot;Comparison&quot;,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)
obj &lt;- roc.test(obj1, obj2)
text(.5, .5, labels=paste(&quot;p-value =&quot;, format.pval(obj$p.value, 3)), 
     adj=c(0, .5))
</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/roc_comparison_7.jpeg" alt="" class="wp-image-1242"/></figure>



<p>If you just want to see the comparison results, <code>roc.test()</code> will provide you with the results.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; roc.test(roc1, roc2)

	DeLong's test for two correlated ROC curves

data:  roc1 and roc2
Z = 3.7621, p-value = 0.0001685
alternative hypothesis: true difference in AUC is not equal to 0
95 percent confidence interval:
 0.0969536 0.3078464
sample estimates:
AUC of roc1 AUC of roc2 
     0.9536      0.7512 
</pre></div>


<p>Finally, let&#8217;s add a legend.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

obj1 &lt;- plot(roc1,
             main=&quot;Comparison&quot;,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)

obj &lt;- roc.test(obj1, obj2)

# p values in the graph
text(.5, .5, labels=paste(&quot;p-value =&quot;, format.pval(obj$p.value, 3)), 
     adj=c(0, .5))

#legend
legend(&quot;bottomright&quot;, legend=c(&quot;Assay1&quot;, &quot;Assay2&quot;),
       col=c(&quot;red&quot;, &quot;blue&quot;), lty=1, lwd=2)
</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/roc_comparison_final_8-1.jpeg" alt="" class="wp-image-1251"/></figure>



<p>That was a helpful tutorial on how to draw ROC curves in R using the pROC package. Thank you for sharing!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/roc-curve-en/1304/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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初心者向け】自分でROC曲線(ROC curve)を書く</title>
		<link>https://brain-storm.space/roc-curve/1212/</link>
					<comments>https://brain-storm.space/roc-curve/1212/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Thu, 23 Mar 2023 10:11:56 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[pROC]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[ROC]]></category>
		<category><![CDATA[sentitivity]]></category>
		<category><![CDATA[specificity]]></category>
		<category><![CDATA[医療]]></category>
		<category><![CDATA[学会]]></category>
		<category><![CDATA[感度]]></category>
		<category><![CDATA[曲線]]></category>
		<category><![CDATA[特異度]]></category>
		<category><![CDATA[発表]]></category>
		<category><![CDATA[統計]]></category>
		<category><![CDATA[論文]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1212</guid>

					<description><![CDATA[ROC曲線の書き方を紹介します。RではROC曲線を書くのはとても簡単です。ROC曲線を描くためのライブラリはたくさんありますが、今回はよく使われていて使いやすいpROCライブラリでの書き方を紹介します。EZRなどでも書く]]></description>
										<content:encoded><![CDATA[
<p>ROC曲線の書き方を紹介します。RではROC曲線を書くのはとても簡単です。ROC曲線を描くためのライブラリはたくさんありますが、今回はよく使われていて使いやすいpROCライブラリでの書き方を紹介します。EZRなどでも書くことはできますが、自分で書ける方が色々と応用できます。</p>



<h3 id="rtoc-1"  class="wp-block-heading">サンプルデータを作る</h3>



<p>ここはサンプルデータを作るだけのところですので、不要な方は次の見出しに読み飛ばしてください。二つの検査、&#8221;assay1&#8243;と&#8221;assay2&#8243;でどの程度疾患の人を検出できるのかを検討してみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
set.seed(3)
condition &lt;- rep(c(&quot;healthy&quot;, &quot;disease&quot;), each = 50)
assay1 &lt;- c(rnorm(40, mean = 1, sd = 2), 
            rnorm(10, mean = 3, sd = 1), 
            rnorm(40, mean = 5, sd = 2), 
            rnorm(10, mean = 7, sd = 1))
assay2 &lt;- c(rnorm(30, mean = 1, sd = 3), 
            rnorm(20, mean = 2, sd = 2), 
            rnorm(30, mean = 3, sd = 3), 
            rnorm(20, mean = 4, sd = 2))

df = data.frame(condition, assay1, assay2)
</pre></div>


<p>ちなみにこれらの分布はこのようなものになります。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
library(beeswarm)
beeswarm(data = df, assay1 ~ condition, 
         col = c(&quot;red&quot;,&quot;blue&quot;), pch=19)
beeswarm(data = df, assay2 ~ condition,
         col = c(&quot;red&quot;, &quot;blue&quot;), pch=19)
</pre></div>


<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/dotplot_assay1.jpeg" alt="" class="wp-image-1220" width="400" height="400"/><figcaption class="wp-element-caption">Assay1のdotplot</figcaption></figure>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/dotplot_assay2.jpeg" alt="" class="wp-image-1221" width="400" height="400"/><figcaption class="wp-element-caption">Assay2のdotplot</figcaption></figure>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1212" data-theme="jin-child">
			<div id="rtoc-mokuji-title" class=" rtoc_left">
			<button class="rtoc_open_close rtoc_open"></button>
			<span>Contents</span>
			</div><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-1">サンプルデータを作る</a></li><li class="rtoc-item"><a href="#rtoc-2">pROCを使ってみる</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">pROCパッケージのインストール</a></li><li class="rtoc-item"><a href="#rtoc-4">1つのROC曲線を描く</a></li><li class="rtoc-item"><a href="#rtoc-5">パーセント表示にする</a></li><li class="rtoc-item"><a href="#rtoc-6">感度の信頼区間を表示する</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-7">カットオフに最適な点を表示する</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-8">Youden Indexによる最適なカットオフを表示する</a></li><li class="rtoc-item"><a href="#rtoc-9">Top Leftによる最適なカットオフを表示する</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-10">必要な数値を取り出す</a></li><li class="rtoc-item"><a href="#rtoc-11">2つのROC曲線を重ねて比較する</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-12">2つのROC曲線を重ねる</a></li><li class="rtoc-item"><a href="#rtoc-13">2つの検査について比較する</a></li></ul></li></ol></div><h2 id="rtoc-2"  class="wp-block-heading">pROCを使ってみる</h2>



<h3 id="rtoc-3"  class="wp-block-heading">pROCパッケージのインストール</h3>



<p>pROCパッケージをインストールして、起動します。</p>


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


<h3 id="rtoc-4"  class="wp-block-heading">1つのROC曲線を描く</h3>



<p>早速いってみましょう。まずデータフレームは次のようになっています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; head(df)
  condition     assay1     assay2
1   healthy -0.9238668  2.8520501
2   healthy  0.4149486 -0.2152325
3   healthy  1.5175764  4.1593113
4   healthy -1.3042638  2.8068527
5   healthy  1.3915657  4.0523835
6   healthy  1.0602479  2.8245020
</pre></div>


<p>ここからもいくつか方法はありますが、まずroc()を使って、その結果をオブジェクトに格納してみましょう。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
</pre></div>


<p>次のように&#8221;~&#8221;を使う書き方でも大丈夫です。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(df$condition ~ df$assay1, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
</pre></div>


<p>conditionの列は0, 1でもいけますが2値変数です。ciは信頼区間です。</p>



<p>plot()でROC曲線を描くことができます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1)
</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/roc_1.jpeg" alt="" class="wp-image-1224"/><figcaption class="wp-element-caption">Assay1</figcaption></figure>



<h3 id="rtoc-5"  class="wp-block-heading">パーセント表示にする</h3>



<p>好みにより軸をパーセント表示にすることができます。percent=TRUEを付け加えます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, percent=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
plot(roc1)
</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/roc_plot2_percent.jpeg" alt="" class="wp-image-1225"/></figure>



<h3 id="rtoc-6"  class="wp-block-heading">感度の信頼区間を表示する</h3>



<p>感度の信頼区間を計算するには ci.se()を使います。colは色を指定します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
plot(roc2)
rocCI &lt;- ci.se(roc2)
plot(rocCI, type=&quot;shape&quot;, col=&quot;lightblue&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/roc_ci_3.jpeg" alt="" class="wp-image-1227"/></figure>



<h2 id="rtoc-7"  class="wp-block-heading">カットオフに最適な点を表示する</h2>



<p>ROC曲線から大事なことは、カットオフに最も適した点を調べることです。最適なカットオフは何か、というところも大きなテーマですが、ここでは２つの方法を紹介します。&#8221;Youden Index&#8221;と&#8221;top left&#8221;です。簡単に説明すれば、Youden Indexは&#8221;<span class="marker">感度+特異度-1</span>&#8221; で計算され、その数値が一番高いところを選びます。また、top leftは<span class="marker">ROC曲線の一番左上に近い点</span>を抽出します。</p>



<h3 id="rtoc-8"  class="wp-block-heading">Youden Indexによる最適なカットオフを表示する</h3>



<p>この時にはplotの中に追記をしていきます。<span class="marker2">print.thres = &#8220;best&#8221;, print.thres.bes.metod = &#8220;youden&#8221;</span>とすると最適点が表示されます。また、<span class="marker">legacy.axes=TRUEにするとx軸を1-Specificityになります。</span></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1, main = &quot;ROC Curve&quot;,      
     identity = TRUE,
     print.thres = &quot;best&quot;,
     print.thres.best.method = &quot;youden&quot;,
     legacy.axes = TRUE)
</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/roc_youden_5.jpeg" alt="" class="wp-image-1231"/><figcaption class="wp-element-caption">Youden Index (J = 感度 + 特異度 &#8211; 1) による最適なカットオフ</figcaption></figure>



<p>この場合カットオフは3.912で感度が0.820、特異度は0.960です。</p>



<h3 id="rtoc-9"  class="wp-block-heading">Top Leftによる最適なカットオフを表示する</h3>



<p>top leftの場合は<span class="marker">print.thres.best.method=&#8221;closest.topleft&#8221;</span>にします。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1, main = &quot;ROC Curve&quot;,      
     identity = TRUE,
     print.thres = &quot;best&quot;,
     print.thres.best.method = &quot;closest.topleft&quot;
     legacy.axes = TRUE)

</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/roc_topleft_4.jpeg" alt="" class="wp-image-1234"/><figcaption class="wp-element-caption">topleftによる最適なカットオフ</figcaption></figure>



<p>この場合のカットオフは3.365、感度0.860、特異度0.880になっています。括弧内は特異度、感度の順番になっていることに注意をしてください。</p>



<h2 id="rtoc-10"  class="wp-block-heading">必要な数値を取り出す</h2>



<p>AUC(area under the curve)の値や感度、特異度などの数値を発表や論文で示す必要があります。その時には次のように表示できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; auc(roc1)
Area under the curve: 0.9536

# youden
&gt; coords(roc1, &quot;best&quot;, ret=c(&quot;threshold&quot;, &quot;sens&quot;, &quot;spec&quot;, &quot;ppv&quot;, &quot;npv&quot;))
          threshold sensitivity specificity       ppv       npv
threshold  3.912407        0.82        0.96 0.9534884 0.8421053

# topleft
&gt; coords(roc1, &quot;best&quot;, best.method=&quot;closest.topleft&quot;,　ret=c(&quot;threshold&quot;, &quot;sens&quot;, &quot;spec&quot;, &quot;ppv&quot;, &quot;npv&quot;))
          threshold sensitivity specificity      ppv       npv
threshold  3.364778        0.86        0.88 0.877551 0.8627451
</pre></div>


<h2 id="rtoc-11"  class="wp-block-heading">2つのROC曲線を重ねて比較する</h2>



<p>今回2つの検査を想定してサンプルデータを作りました。どちらの検査が優れているかみていきます。</p>



<h3 id="rtoc-12"  class="wp-block-heading">2つのROC曲線を重ねる</h3>



<p>２つのROC曲線を重ねる時には後から重ねる方をlines()にします。または単純にplot(&#8230;, add=TRUE)でも同じ結果になります。colで色を指定しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# ２つの検査について、roc()を使ってその結果をオブジェクトに格納する
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

# linesを使う
obj1 &lt;- plot(roc1,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)

# add = TRUEにする
obj1 &lt;- plot(roc1,
             col=&quot;red&quot;)
obj2 &lt;- plot(roc2,
             col=&quot;blue&quot;,
             add=TRUE)
</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/roc_two_assay_6.jpeg" alt="" class="wp-image-1239"/></figure>



<h3 id="rtoc-13"  class="wp-block-heading">2つの検査について比較する</h3>



<p>2つの検査について比較する時には<span class="marker">roc.test()を使います</span>。デフォルトではAUC(area under the curve)をDeLong法で比較しますが、method = &#8220;bootstrap&#8221;と指定すればブートストラップ法などにも変更できます。textで中央にp値を表示しています。また<span class="marker">タイトルはmain=で指定できます。</span></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

obj1 &lt;- plot(roc1,
             main=&quot;Comparison&quot;,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)
obj &lt;- roc.test(obj1, obj2)
text(.5, .5, labels=paste(&quot;p-value =&quot;, format.pval(obj$p.value, 3)), 
     adj=c(0, .5))
</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/roc_comparison_7.jpeg" alt="" class="wp-image-1242"/></figure>



<p>比較の結果を見るだけだったらroc.test()で結果が出てきます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; roc.test(roc1, roc2)

	DeLong's test for two correlated ROC curves

data:  roc1 and roc2
Z = 3.7621, p-value = 0.0001685
alternative hypothesis: true difference in AUC is not equal to 0
95 percent confidence interval:
 0.0969536 0.3078464
sample estimates:
AUC of roc1 AUC of roc2 
     0.9536      0.7512 
</pre></div>


<p>最後にlegendを入れてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

obj1 &lt;- plot(roc1,
             main=&quot;Comparison&quot;,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)

obj &lt;- roc.test(obj1, obj2)

# p values in the graph
text(.5, .5, labels=paste(&quot;p-value =&quot;, format.pval(obj$p.value, 3)), 
     adj=c(0, .5))

#legend
legend(&quot;bottomright&quot;, legend=c(&quot;Assay1&quot;, &quot;Assay2&quot;),
       col=c(&quot;red&quot;, &quot;blue&quot;), lty=1, lwd=2)
</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/roc_comparison_final_8-1.jpeg" alt="" class="wp-image-1251"/></figure>



<p>綺麗なROC曲線を書くことができました。<br>いかがでしたでしょうか。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/roc-curve/1212/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</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>
	</channel>
</rss>
