疫学と医療統計学と遺伝学と時々、大学院生活

疫学を専門とする大学院生の研究に関する備忘録的ページ。

R package"VennDiagram"でベン図を書く

今日はベン図を描きたいと思います。
(今回は例として、東海三県での多い名字30位についてベン図を書いてみようと思う)

パッケージの読み込み

library(VennDiagram)

まずはベン図で書きたいそれぞれのカテゴリに含まれる要素を書き出す

aichi <- c("鈴木","加藤","伊藤","山田","近藤","山本","佐藤","田中","渡辺","水野","中村","杉浦","林","小林","石川","竹内","森","吉田","高橋","後藤","柴田","山口","安藤","服部","神谷","長谷川","太田","榊原","岡田","木村")
gifu <- c("加藤","伊藤","山田","林","渡辺","田中","高橋","後藤","鈴木","森","佐藤","水野","安藤","吉田","中島","清水","高木","山本","中村","今井","浅野","小林","古田","岩田","小川","堀","田口","大野","河合","山口")
mie <- c("伊藤","山本","中村","田中","鈴木","加藤","小林","水谷","森","山口","佐藤","西村","中西","服部","前田","中川","山下","林","渡辺","松本","浜口","山田","清水","橋本","近藤","大西","谷口","村田","長谷川","東")

これを今度はリストにする(これで準備は完了!!)

list <- list(Aichi=aichi, Gifu=gifu, Mie=mie)

最後はvenn.diagram関数を使用する

venn.diagram(list, filename="Aichi_Gifu_Mie.jpg", fill=c(2,3,4), alpha=0.4, lty=3)

完成したのがこちら

f:id:ryosukefujii0320:20161008084953p:plain

全ての県で共通している名字はこちら

intersect(intersect(aichi, gifu), mie)
# [1] "鈴木" "加藤" "伊藤" "山田" "山本" "佐藤" "田中" "渡辺" "中村" "林"   "小林" "森"   "山口"

こんな具合です。他にもベン図を描くパッケージ及び関数はあるようなので下記のサイトなどをご参照ください。
ベン図 | Rでベン図を描く方法(venn,venneuler,venn.diagram)



20161007
RF