<?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>論文作成・統計｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/category/paper-statistics-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 16:54:45 +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>論文作成・統計｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>



<h2 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 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>



<a href="https://brain-storm.space/beeswarm_r/698/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/box_beeswarm8-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R超初心者向け】beeswarmでdotplotを箱ひげ図の上にのせる方法!</span><span class="blog-card-excerpt">beeswarmを使って、ドットプロットを簡単かつきれいに書く方法を紹介します。...</span></div></div></a>



<h3 class="wp-block-heading">ggplot2を用いてボックスプロットとドットプロットを作図する</h3>



<p>ggplot2を使うのに慣れた人向けの方法です。これは単純なドットプロットを描く方法で、いわゆるbeeswarm(蜂群図)ではありませんが、視覚的効果は十分にあります。</p>



<a href="https://brain-storm.space/rggplot2_boxplot_dotplot/620/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/box_dot_plot_label4-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【超初心者向け】無料のRとggplot2で箱ひげ図とドットプロットをきれいに書く方法!</span><span class="blog-card-excerpt">プレゼンテーションや論文に重要な図の作り方です. キレイな図なだけで人の目を引きます. Rではggplot2を使えばでキレイな図が描けます. この記事では次のような図をできるようになります....</span></div></div></a>



<h3 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>



<a href="https://brain-storm.space/ggbeeswarm/820/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/method_beeswarm-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】ggplot2でもbeeswarmで作図したい！ggbeeswarmを使った方法</span><span class="blog-card-excerpt">論文や発表で使えるggplot2でbeeswarmを描く方法を紹介します...</span></div></div></a>



<h2 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>



<a href="https://brain-storm.space/violin-plot/871/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/gg_violin_boxplot2-e1679658032838-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】ggplot2でバイオリンプロット(violin plot)を描く</span><span class="blog-card-excerpt">バイオリンプロットは複数のデータを比較する時に便利な方法です。ここでは無料のRを使ってキレイな図を描く方法を説明します。...</span></div></div></a>



<h2 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 class="wp-block-heading">散布図と近似直線を描く方法</h3>



<a href="https://brain-storm.space/rggplot_scatterplot/751/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/scatterplot_facet_grid3-e1621560578477.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者必見】ggplotを使ってきれいな図で散布図と近似直線を書く</span><span class="blog-card-excerpt">
相関の図を書くときに何のソフトを使っているでしょうか？



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


...</span></div></div></a>



<h2 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>



<a href="https://brain-storm.space/r_facet/843/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap7-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【Rテクニック】facet_gripとfacet_wrapの使い方とタイトルラベルを変えたい！</span><span class="blog-card-excerpt">ggplot2の機能にあるfacet_gridとfacet_wrapはビジュアルとしてとても有用です. ぜひ使いこなしましょう....</span></div></div></a>



<h2 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>



<a href="https://brain-storm.space/bargraph/1025/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/07/bargraph10-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【ggplot2】Rで棒グラフを作成する</span><span class="blog-card-excerpt">Rのggplot2を使って棒グラフを作図する方法を紹介します。...</span></div></div></a>



<h2 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>



<a href="https://brain-storm.space/ggplot2_errorbar/895/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar10-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】ggplot2で棒グラフと折れ線グラフにエラーバー(error bar)を書く</span><span class="blog-card-excerpt">エラーバーをどうやって付け加えるの？という疑問に無料のRを使って簡単に出来る方法を紹介します。エラーバーはggplot2を使えば簡単にできます！...</span></div></div></a>



<h2 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>



<a href="https://brain-storm.space/corrheatmap/942/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/06/cormat7-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R】ggplot2で相関行列ヒートマップを描く</span><span class="blog-card-excerpt">相関行列の相関係数を表にすることも多いと思います。しかし視覚に訴求する強力なツールとしてRで図を作成することができます。それの一つが相関行列です。Rではいくつものパッケージがあるので、そちらを使うこともできますが、シンプルにggplot2を使った方法を紹介します。...</span></div></div></a>



<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>



<a href="https://brain-storm.space/tidyr_gather_pivot_longer/784/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-christina-morillo-1181675-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】慣れると便利なtidyrのpivot_longerとgatherをやさしく解説！</span><span class="blog-card-excerpt">データ解析に欠かせないtidyrというパッケージですが、初心者のうちはなかなか使い方がわかりません。今回は横に広いデータを縦長のデータに変更するgatherまたはpivot_longerの使い方を日本一わかりやすく説明をします。...</span></div></div></a>



<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 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>



<a href="https://brain-storm.space/geom_rect/1191/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2023/03/geom_rect_fig7-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R簡単テクニック】背景に四角の領域(背景)をいれる</span><span class="blog-card-excerpt">Rの初心者に向けて、ステップバイステップでggplotのgeom_rectで四角い背景を入れる方法を紹介します。...</span></div></div></a>



<h2 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>



<a href="https://brain-storm.space/roc-curve/1212/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2023/03/roc_comparison_final_8-1-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="ROC曲線" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】自分でROC曲線(ROC curve)を書く</span><span class="blog-card-excerpt">RでROC曲線またはROC curveを書けるようになります。...</span></div></div></a>



<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>



<h2 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>



<a href="https://brain-storm.space/tidyr_gather_pivot_longer/784/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-christina-morillo-1181675-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】慣れると便利なtidyrのpivot_longerとgatherをやさしく解説！</span><span class="blog-card-excerpt">データ解析に欠かせないtidyrというパッケージですが、初心者のうちはなかなか使い方がわかりません。今回は横に広いデータを縦長のデータに変更するgatherまたはpivot_longerの使い方を日本一わかりやすく説明をします。...</span></div></div></a>



<h2 class="wp-block-heading">データの平均、標準偏差、標準誤差を算出する</h2>



<p>エラーバーのためには平均(mean)、標準偏差(sd)、標準誤差(se)を算出します。すでにこれらのデータがある場合にはここも読み飛ばしてください。</p>



<p>その算出方法も慣れれば簡単です！tidyverseに入っているdplyrを使っていきましょう。</p>



<h3 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 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 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 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 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 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>【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 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>



<h2 class="wp-block-heading">pROCを使ってみる</h2>



<h3 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 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 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 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 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 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 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 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 class="wp-block-heading">2つのROC曲線を重ねて比較する</h2>



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



<h3 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 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>



<h2 class="wp-block-heading">geom_rectの使い方</h2>



<h3 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 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 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 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 class="wp-block-heading">複数の領域を書きたい時</h2>



<h3 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 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>論文投稿tips【まとめ】</title>
		<link>https://brain-storm.space/papers_matome/1074/</link>
					<comments>https://brain-storm.space/papers_matome/1074/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Mon, 02 Aug 2021 01:00:11 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[science]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1074</guid>

					<description><![CDATA[論文投稿をはじめて行う方は右も左もわからない状態です。実際研究成果がようやく出たと思ってからより、論文を作り上げてから投稿するまでのほうが大変だと感じることもあるかもしれません。 論文投稿についてのtipsをそのテーマご]]></description>
										<content:encoded><![CDATA[
<p>論文投稿をはじめて行う方は右も左もわからない状態です。実際研究成果がようやく出たと思ってからより、論文を作り上げてから投稿するまでのほうが大変だと感じることもあるかもしれません。</p>



<p>論文投稿についてのtipsをそのテーマごとにわけて記載をしてみました。</p>



<h2 class="wp-block-heading">どの雑誌を選ぶか</h2>



<p>雑誌選びについてとても慎重に考える必要があります。難しいジャーナルからチャレンジしていくのか、比較的通りそうなところをはじめから選んでいくのか、その可能性、労力、時間や通ったときのインパクトについてなど様々な要素から検討をしていく必要があります。</p>



<a href="https://brain-storm.space/select_journal/1083/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/07/pexels-photo-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="fashion woman notebook pen" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">論文の雑誌選び</span><span class="blog-card-excerpt">論文をどの雑誌に投稿するかとても難しい問題です。その考え方を説明しました。...</span></div></div></a>



<h2 class="wp-block-heading">英文校正の前に</h2>



<p>論文は指導者のみではなく、他の共著者にも読んでもらうことが必要です。しっかり内容を読むかどうかはその共著者にもよるかもしれませんが、重要な指摘をもらえたり、間違いに気づいてもらえることもあります。当然ですが、投稿するための承諾なども必要です。</p>



<p>また研究者の同僚に英語のネイティブスピーカーがいればその人に英文をチェックしてもらうこともできますが、そのような環境の人もそれほど多くはないと思います。そのためそのような環境ではない人は論文の英語をしっかりチェックしてもらえる英文校正に出すことも必要です。</p>



<a href="https://brain-storm.space/paper1-3/333/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-photo-1205651-e1620082066223-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="photography of people graduating" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">論文ができるまで - 英文校正の前後にすべき3つのこと</span><span class="blog-card-excerpt">英文校正前にすべき2つと英文校正後にすべき１つのことを解説しました。初めて論文を書く人必見です。...</span></div></div></a>



<h2 class="wp-block-heading">カバーレターでdesk rejection率を下げる！</h2>



<p>　比較的よく見るのが多いのが、カバーレターをほぼ定型文で書いているケースです。これではせっかく頑張って書いた論文の中身すら読んでもらえないことがあります。少なくともeditorial regectになりにくいカバーレターを目指しましょう。</p>



<a href="https://brain-storm.space/cover-letter/382/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-photo-1262304-e1620190402815-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="silhouette photo of man throw paper plane" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【論文投稿】カバーレター(cover letter)でdesk rejection率を下げる方法</span><span class="blog-card-excerpt">カバーレターで損をしていませんか？カバーレターで損をしない方法を紹介します。...</span></div></div></a>



<h2 class="wp-block-heading">いよいよ投稿する投稿をしてから返事が来るまで</h2>



<p>投稿したあとに現在どのような進行状況かを投稿サイトから知ることができます。しかしあっという間にそのステータスが変わったり、あるいは全然変わらなかったり。進行状況はわかりますが、その表示は雑誌によっても異なっており、ヤキモキすることも少なくありません。投稿してからの流れを解説したのが、次の記事です。</p>



<a href="https://brain-storm.space/paper1/233/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-photo-2280571-e1619923618959-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="person holding laboratory flask" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【論文投稿】初投稿から最初の返事が返ってくるまで</span><span class="blog-card-excerpt">論文を投稿してから最初に返事が来るまでの流れとステータスの意味を解説しています。論文を書き上げてからもアクセプトをされるまでは長い道のりです。...</span></div></div></a>



<h2 class="wp-block-heading">ジャーナルから返事が来たら</h2>



<p>投稿した雑誌からいつかは返事がきます。待ちに待った返事を開いてどうだったでしょうか。喜びや安堵の気持ちを抱くか、落胆や絶望感を味わうか、大きく感情が揺さぶられる瞬間です。しかし、次に何をすべきか、気持ちを切り替えて行くしかありません。せっかく頑張って書いた論文です。よっぽど論文に問題がある場合を除き、お蔵入りにさせず、強い信念で論文を通しましょう。</p>



<a href="https://brain-storm.space/paper1-2/280/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-photo-4348401-e1620027489494-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="photo of woman writing on tablet computer while using laptop" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【初投稿】編集者からの返事が来たら</span><span class="blog-card-excerpt">論文投稿後、ジャーナルから返事をもらった際の返事別対応方法です。...</span></div></div></a>



<h2 class="wp-block-heading">リバイスの方法</h2>



<p>major revisionやminor revisionになった時、その改訂の仕方にはそれなりの作法があります。せっかく改訂すればアクセプトにするという段階ですから、これを無駄にしてしまうことはできません。うまくいかなければリジェクトになる可能性もありますので、手を抜かずに慎重に進めましょう。</p>



<a href="https://brain-storm.space/paper-revision/408/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-photo-3771074-e1620278183741-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="strict female teacher with book pointing at scribbled blackboard" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【論文投稿】Reviseの方法 - 改訂後に落とされないために必要な手順!</span><span class="blog-card-excerpt">駆け出し研究者のためのreviseの方向、改訂後に落とされないために気をつけるべきことを記載しています。...</span></div></div></a>



<h2 class="wp-block-heading">プレスリリース-論文に通ったあとに</h2>



<p>論文が通ったらプレスリリースをすることがあります。別にしなくても構いませんが、社会的にインパクトのありそうな研究だと考えているならば、プレスリリースをすることによって自分たちの研究を多くの人に知ってもらえるチャンスです。プレスリリースは論文がin pressの段階で行うのが効果的です。そのメリットと注意すべき点を記載しました。</p>



<a href="https://brain-storm.space/pressrelease/504/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-photo-4057663-e1620530720919-320x180.jpeg" class="blog-card-thumb-image wp-post-image" alt="news typewritten on white paper" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">研究をプレスリリースする3つのメリットと問題点</span><span class="blog-card-excerpt">プレスリリースとはメディアや一般の方に向けて、研究成果を発信することです。これにより、研究成果を広く知ってもらうためのものです。プレスリリースには多くのメリットがある一方で、その受け取り手の側には注意すべきことがあります。これについて説明をしていきます。...</span></div></div></a>



<div class="wp-block-jin-gb-block-blank-space blank-space" style="height:30px"></div>



<p>論文が通るまでには大変な苦労があります。特にはじめての頃はよくわからないことがたくさんあります。</p>



<p>この記事がお役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/papers_matome/1074/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>論文の雑誌選び</title>
		<link>https://brain-storm.space/select_journal/1083/</link>
					<comments>https://brain-storm.space/select_journal/1083/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Thu, 29 Jul 2021 05:48:35 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1083</guid>

					<description><![CDATA[論文をどの雑誌に投稿するかについては、とても難しい問題です。自分はこっちがいいと思っているのに、指導の先生は違う分野の雑誌に投稿するかもしれません。またある分野の雑誌に出し続けても全然通らなかったのに、分野を変えて出して]]></description>
										<content:encoded><![CDATA[
<p>論文をどの雑誌に投稿するかについては、とても難しい問題です。自分はこっちがいいと思っているのに、指導の先生は違う分野の雑誌に投稿するかもしれません。またある分野の雑誌に出し続けても全然通らなかったのに、分野を変えて出してみたら案外あっさりとアクセプトされた、なんてこともあります。</p>



<p>なかなかわかりづらい、論文がどのようにできていくのかということに関して、これまでの経験から論文をかきあげてからの流れを書いていきます。</p>



<p>論文の大半は英語で書きます。分野によるかもしれませんが、世界のスタンダードは英語なので、日本語で書いてもあまり評価はしてもらえません。過去には日本ではじめての発表だったはずの大発見が、日本語だったためその成果が認められていなかったことがいくつもあります。</p>



<p>研究をして、データを取って、原稿を書いて、図表を書いて、何度も改訂をして、苦労して書き上げた英語論文をいざ雑誌社に投稿しようとなります。ここまで数ヶ月から年単位の過程があります。</p>



<h2 class="wp-block-heading">その論文をどの系統の雑誌に出すかを考える</h2>



<figure class="wp-block-image size-small_size"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/girl-2573111_1280-640x360.jpg" alt="" class="wp-image-589"/></figure>



<div class="wp-block-jin-gb-block-chat-block balloon-box balloon-left clearfix has-f-48789-ballon has-ffebeb-bgballon"><div class="balloon-icon maru"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/04/6bd4c520116a47cd05483cccc4b405c9-e1619952965822.png"/></div><span class="icon-name">新米びーたん</span><div class="balloon-serif"><div class="balloon-content">
<p>わぁ、たいへんそうだなぁ！</p>
</div></div></div>



<p>多くの研究者より知名度や信頼性が高いといわれる雑誌から投稿しますが、どの分野の雑誌に投稿するかも重要です。より専門性の高い雑誌に出すのか、あるいは広く扱っている雑誌にだすのか。</p>



<p>たとえインパクトファクターが低くても専門性の高い雑誌に出せば、その分野の専門家たちにその記事を読まれることも多いかもしれませんし、そのジャーナルに乗っている、というだけでどういう分野の論文なのかが読者にもわかりやすいです。</p>



<p>一方で、より広い範囲のテーマを扱っている雑誌であれば、多くの読者に読まれる可能性もありますし、だれもが知っているような権威ある雑誌であれば、あの雑誌に通ったということで研究者としての箔がつく可能性もあります。</p>



<p>またオープンアクセスジャーナルに出すかどうかも決めたほうがいいです。オープンアクセスのメリットは世界中のだれでもが自分たちの論文をダウンロードが出来るようになるということです。そうでない論文は大学や研究所が購読契約をしていないと無料で読めません。購読契約のない論文一つに数千円も払わなくてはいけなければ読まれる数も少なくなります。そうすると引用されることが少なくなってしまいます。オープンアクセスのデメリットはアクセプトされたらだいたい30万円以上の費用がかかります。オープンアクセスのメリットは研究費などで出してもらえるのかどうか、投稿をする前にボスに確認しましょう。</p>



<p>超一流誌のScienceやNature、臨床医学系であればNew England Journal of MedicineやLancetという雑誌が有名です。この当たりは最難関ですので、よっぽど質の高い研究でないと通りません。</p>



<p>だいたい自分の論文の価値で、どの雑誌に合いそうか、あるいはどの程度の有名な雑誌からチャレンジをできそうか、などということを考えます。経験豊富な指導者の先生と相談することが大切です。</p>



<div class="wp-block-jin-gb-block-icon-box jin-icon-caution jin-iconbox"><div class="jin-iconbox-icons"><i class="jic jin-ifont-caution jin-icons"></i></div><div class="jin-iconbox-main">
<p>オープンアクセス費用を稼ぐ目的の質の低いハゲタカジャーナルに投稿するのは絶対にやめましょう。まともな査読がなく、業績に傷がつくだけです。</p>
</div></div>



<h2 class="wp-block-heading">投稿規定をみて、体裁を確認する</h2>



<figure class="wp-block-image size-small_size"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/checklist-2589418_1280-640x360.jpg" alt="" class="wp-image-590"/></figure>



<p>投稿する雑誌を決めたらまず投稿規定をみます。その雑誌のルールみたいなもので、文字数や図表の数、体裁などが事細かく書いてあります。書いた原稿がこれに沿っているか確認をして、違うところがあれば直します。</p>



<div class="wp-block-jin-gb-block-chat-block balloon-box balloon-left clearfix has-9-ddd-93-ballon has-d-1-f-8-c-2-bgballon"><div class="balloon-icon maru"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/04/3d0209af428738b78799159b4ce75ad9-e1619952954611.png"/></div><span class="icon-name"></span><div class="balloon-serif"><div class="balloon-content">
<p>せっかくたくさん書いたのに、字数や図表を減らさなくちゃいけないこともよくあります。</p>
</div></div></div>



<p>投稿規定をみて、字数やreferenceの数の関係で内容を大きく削らなくてはいけないなどがある場合、内容は変わってしまいそうなくらいであればいっそ投稿する雑誌を変えることも検討したほうがいいです。その雑誌の求めている論文と、投稿しようとしている論文がマッチしていない可能性が高いからです。</p>



<p>そしていざ投稿画面をひらきます。昔は郵送でやっていた時代もあったようですが、今の時代はすべてオンラインで完結します。雑誌社への投稿はScholarOneやEditorial Managerという名前の投稿システムが採用されているので、どの投稿画面は同じような感じです。</p>



<p>入力画面を開いて、書いてある英語にしたがって入力をしていきます。共著者(一緒に研究をした同僚やボス)の名前や所属、メールアドレスなどを入れていきます。あともう一息、これまでの苦労が蘇ってきます。これがいちいち大変ですが、仕方がありません。</p>



<div class="wp-block-jin-gb-block-icon-box jin-icon-caution jin-iconbox"><div class="jin-iconbox-icons"><i class="jic jin-ifont-caution jin-icons"></i></div><div class="jin-iconbox-main">
<p>投稿は一つの雑誌のみです。2つの雑誌に並行して出すことは絶対にやめましょう。</p>
</div></div>



<p>これまでの一つ一つだして、結果を待つという作業が研究者にとってとてもストレスなので、最近は出版社の系列雑誌のいくつか中から適切な雑誌にマッチするような仕組みも出てきています。またbioRxivのようなプレプリントサーバーで査読前論文を公開したりする流れもあったりします。もちろんこれにもメリットとデメリットがありますので、慎重に考えたほうがいいでしょう。</p>



<p>この記事がお役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/select_journal/1083/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</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[最もよく使われる棒グラフですが、その中でもいくつか使い分けが必要です。そのいくつかの種類の棒グラフの描き方を紹介します。 データの準備 まずはデータの準備をします。デモ用のデータとして最もよく使われるiris(アヤメ)デ]]></description>
										<content:encoded><![CDATA[
<p>最もよく使われる棒グラフですが、その中でもいくつか使い分けが必要です。そのいくつかの種類の棒グラフの描き方を紹介します。</p>



<h2 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 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 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 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 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 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 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 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 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 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 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 class="wp-block-heading">エラーバーをつける</h2>



<p>最後にエラーバーをつけます。</p>



<p>エラーバーをの付け方については、次のリンクで詳しく説明をしています。</p>



<a href="https://brain-storm.space/ggplot2_errorbar/895/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/06/errorbar10-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】ggplot2で棒グラフと折れ線グラフにエラーバー(error bar)を書く</span><span class="blog-card-excerpt">エラーバーをどうやって付け加えるの？という疑問に無料のRを使って簡単に出来る方法を紹介します。エラーバーはggplot2を使えば簡単にできます！...</span></div></div></a>



<h3 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 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>



<h2 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 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 class="wp-block-heading">相関行列の生成</h3>



<p>相関行列を生成してみます。</p>



<p>mtcarsのデータからSpearmanの順位相関係数をみてみます。またggplot2で使いやすいようにデータを変形していきます。相関はcorで計算をしており、その中のmethodでspearmanと指定しています。</p>



<p>縦のデータへの変換はpivot_longerを用いています。pivot_longerは次の記事に詳しい説明があります。</p>



<a href="https://brain-storm.space/tidyr_gather_pivot_longer/784/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-christina-morillo-1181675-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】慣れると便利なtidyrのpivot_longerとgatherをやさしく解説！</span><span class="blog-card-excerpt">データ解析に欠かせないtidyrというパッケージですが、初心者のうちはなかなか使い方がわかりません。今回は横に広いデータを縦長のデータに変更するgatherまたはpivot_longerの使い方を日本一わかりやすく説明をします。...</span></div></div></a>


<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 class="wp-block-heading">geom_tileを使って相関行列のヒートマップを描く</h2>



<p>geom_tile()を使うことで視覚化することができます。</p>



<p>次のようになります。</p>



<h3 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 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 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 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 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 class="wp-block-heading">三角行列で表示をしたい時</h2>



<p>上の図では対角線で対称になっていますが、余計な情報だと思うかもしれません。それであれば三角行列にしてみます。</p>



<p>ただし、ggplotで三角行列にするというより、データ自体を三角行列になるようにしていきます。</p>



<h3 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 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>



<h2 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>



<a href="https://brain-storm.space/ggbeeswarm/820/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/method_beeswarm-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】ggplot2でもbeeswarmで作図したい！ggbeeswarmを使った方法</span><span class="blog-card-excerpt">論文や発表で使えるggplot2でbeeswarmを描く方法を紹介します...</span></div></div></a>



<a href="https://brain-storm.space/tidyr_gather_pivot_longer/784/" class="blog-card"><div class="blog-card-hl-box"><i class="jic jin-ifont-post"></i><span class="blog-card-hl"></span></div><div class="blog-card-box"><div class="blog-card-thumbnail"><img src="https://brain-storm.space/wp-content/uploads/2021/05/pexels-christina-morillo-1181675-320x180.jpg" class="blog-card-thumb-image wp-post-image" alt="" decoding="async" width ="162" height ="91" /></div><div class="blog-card-content"><span class="blog-card-title">【R初心者向け】慣れると便利なtidyrのpivot_longerとgatherをやさしく解説！</span><span class="blog-card-excerpt">データ解析に欠かせないtidyrというパッケージですが、初心者のうちはなかなか使い方がわかりません。今回は横に広いデータを縦長のデータに変更するgatherまたはpivot_longerの使い方を日本一わかりやすく説明をします。...</span></div></div></a>



<p>それでは、バイオリンプロットを描いていきましょう！</p>



<h2 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 class="wp-block-heading">バイオリンに色をつける</h2>



<p>バイオリンに色を付ける場合、枠に色をつけるか、中を塗りつぶすかで指定の仕方を変えます。</p>



<h3 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 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 class="wp-block-heading">平均値や中央値を入れる</h2>



<p>バイオリンプロットに平均値や中央値を入れるのは、stat_summaryを使います。</p>



<h3 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 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 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 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>
		<item>
		<title>【Rテクニック】facet_gripとfacet_wrapの使い方とタイトルラベルを変えたい！</title>
		<link>https://brain-storm.space/r_facet/843/</link>
					<comments>https://brain-storm.space/r_facet/843/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Sat, 29 May 2021 03:29:46 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[facet_grid]]></category>
		<category><![CDATA[facet_wrap]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=843</guid>

					<description><![CDATA[facet_gripとfacet_wrapは複数グラフを一度に作成できるのでとても便利です！ またビジュアルとしてとても有用で、魅力的なグラフになります。 タイトルラベルを変えるのは少しテクニックがありますので、それを含]]></description>
										<content:encoded><![CDATA[
<p><span class="marker">facet_gripとfacet_wrap</span>は複数グラフを一度に作成できるのでとても便利です！</p>



<p>またビジュアルとしてとても有用で、魅力的なグラフになります。</p>



<p>タイトルラベルを変えるのは少しテクニックがありますので、それを含めて紹介します。</p>



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



<h2 class="wp-block-heading">facet_gridとfacet_wrapの使い方</h2>



<p>facet_gridとfacet_wrapはどちらもデータをグループごとに並べるものになります。いつも便利なiris dataを用いていきます。</p>



<h3 class="wp-block-heading">facet_gridを使ってみる！</h3>



<p>まずはデータの準備をします。</p>



<p>tidyverseを起動しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
#作業ディレクトリのセット
setwd(&quot;~/Rpractice/&quot;)

#libraryの起動
library(tidyverse)

#irisデータ
a &lt;- iris

&gt; head(a)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
&gt; 

</pre></div>


<p>まずはfacet_gridの使い方を見てみましょう！</p>



<p>まずは水平方向です。</p>



<p>facet_gridのカッコの中を(. ~ Species)で指定しています。</p>


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

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_grid1-1024x384.jpg" alt="" class="wp-image-848"/></figure>



<p>次は垂直方向です！</p>



<p>これは反対にfacet_grid(Species ~ .)で指定しています。</p>


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

</pre></div>


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



<p>このような感じになりました。</p>



<p>今回は１行または１列で表示しましたが、それぞれ垂直方向、水平方向を指定して複数の行列で表示することもできます。</p>



<h3 class="wp-block-heading">facet_wrapを使ってみる！</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = TRUE)+
  facet_wrap(~ Species)  #ドットは不要です

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="384" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap1-1024x384.jpg" alt="" class="wp-image-851"/></figure>



<p>２行にしてみましょう。</p>



<p>facet_wrapのカッコの中のncolまたはnrowで指定できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = TRUE)+
  facet_wrap(~ Species, nrow = 2)

</pre></div>


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



<h3 class="wp-block-heading">スケールを別々にする方法</h3>



<p>スケールが合わさっていると極端に偏ったグラフが出来てしまうことがあります。</p>



<p>そのときはscales = &#8220;free&#8221;を設定します。</p>



<div class="wp-block-jin-gb-block-icon-box jin-icon-caution jin-iconbox"><div class="jin-iconbox-icons"><i class="jic jin-ifont-caution jin-icons"></i></div><div class="jin-iconbox-main">
<p>ただし、逆に見にくくなることもあるので注意をしてください。</p>
</div></div>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap3-1024x384.jpg" alt="" class="wp-image-854" width="840" height="315"/></figure>



<h2 class="wp-block-heading">ファセットラベルの変更の仕方</h2>



<p>これを変更する一つの方法はデータを変更してしまうことです。</p>



<p><span class="marker">もう一つの方法として、labellerを指定します</span>！</p>



<p>早速やり方を見ていきましょう。</p>



<h3 class="wp-block-heading">labellerの設定をする</h3>



<p>facet_gridまたはfacet_wrapでlabeller = labellar()で指定をすることができます。</p>



<p>下記の方法は少しテクニックが必要ですが、真似をするだけで変更できます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
Att.labs &lt;- c(&quot;Att:Setosa&quot;, 
              &quot;Att:Versicolor&quot;, 
              &quot;Att:Virginica&quot;)
names(Att.labs) &lt;- c(&quot;setosa&quot;,
                      &quot;versicolor&quot;,
                      &quot;virginica&quot;)

ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(~ Species,  scales = &quot;free&quot;, 
             labeller = labeller(Species = Att.labs))

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="384" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap4-1024x384.jpg" alt="" class="wp-image-855"/></figure>



<p>またタイトルやサブタイトルもつけることができます</p>



<h3 class="wp-block-heading">タイトル、サブタイトルをつける！</h3>



<p>タイトルのラベルはlabs(title = )でしていできます。</p>



<p>また同様にlabsでサブタイトルやx軸、y軸のラベルを指定することができます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(~ Species,  scales = &quot;free&quot;, 
             labeller = labeller(Species = Att.labs)
  )+
  labs(title = &quot;Iris data analysis&quot;, subtitle = &quot;Sepal measurement&quot;,
       x = &quot;Length&quot;,
       y = &quot;Width&quot;)
</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="384" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap5-1024x384.jpg" alt="" class="wp-image-856"/></figure>



<p>フォントサイズを整えます。またtheme()でその大きさをそれぞれ指定できます。</p>



<p>レジェンドもあんまり役に立っていないので、なくしちゃいましょう。その時はlegend.position = &#8220;none&#8221;とすることで表示をなくします。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(. ~ Species,  scales = &quot;free&quot;, 
             labeller = labeller(Species = Att.labs))+
  labs(title = &quot;Iris data analysis&quot;, 
       subtitle = &quot;Sepal measurement&quot;,
       x = &quot;Length&quot;, y = &quot;Width&quot;)+
  theme(axis.text.x = element_text(vjust = 1, size = 16, 
                                   hjust = 1, face = &quot;bold&quot;),
        axis.text.y = element_text(hjust = 1, size = 16),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.title = element_text(size=16),
        legend.text = element_text(size=16),
        strip.text.x = element_text(size = 20),
        plot.title = element_text(size=22),
        plot.subtitle = element_text(size=20),
        legend.position = &quot;none&quot;)

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="410" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap6-1024x410.jpg" alt="" class="wp-image-857"/></figure>



<p>スケールメモリを統一したほうが実際は見やすいことが多いです。</p>



<p>最後にスケールメモリを統一してみましょう。</p>



<p>theme_classic()で背景を白くしてみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
ggplot(a, aes(x = Sepal.Length, y = Sepal.Width, 
              color = Species)) +
  geom_point(size = 3, alpha = .5)+
  geom_smooth(method = lm, se = FALSE)+
  facet_wrap(. ~ Species,
             labeller = labeller(Species = Att.labs))+
  labs(title = &quot;Iris data analysis&quot;, 
       subtitle = &quot;Sepal measurement&quot;,
       x = &quot;Length&quot;, y = &quot;Width&quot;)+
  theme_classic() +
  theme(axis.text.x = element_text(vjust = 1, size = 16, 
                                   hjust = 1, face = &quot;bold&quot;),
        axis.text.y = element_text(hjust = 1, size = 16),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.title = element_text(size=16),
        legend.text = element_text(size=16),
        strip.text.x = element_text(size = 20),
        plot.title = element_text(size=22),
        plot.subtitle = element_text(size=20),
        legend.position = &quot;none&quot;)

</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2021/05/facet_wrap7-1024x512.jpg" alt="" class="wp-image-935" width="768" height="384"/></figure>



<p>それぞれを比較するにはとても見やすくなったと思います。</p>



<p>facet_gridとfacet_wrapはとても便利ですよね！</p>



<p>お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/r_facet/843/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
