Radi_tech’s blog

Radiological technologist in Japan / MRI / AI / Deep learning / MATLAB / R / Python

【R】症例数順にならべた横棒グラフを描く

f:id:radi_tech:20220117081027p:plain
症例数を可視化するには横棒グラフが便利

CSVは↓の感じ
今回は脳外科の急患の内訳を使う

bar plot r
bar plot
# bar plot

library(ggplot2)
library(ggsci)

dat <- read.csv("/Users/Desktop/Book1.csv",header=TRUE)
attach(dat)


p <- ggplot(dat,aes(x = reorder(x = disease, X = n, FUN = mean), y = n), fill = disease)
p <- p + geom_bar(stat = "identity")

#横棒へ
p <- p + coord_flip()

#凡例消す
p <- p + theme(legend.position = "none")

#titleなど余分なものをけす
p <- p + theme(axis.title.x = element_blank(),axis.ticks.x = element_blank())
p <- p + theme(axis.title.y = element_blank(),axis.ticks.y = element_blank())
p <- p + labs(x = "", y = "") 
p <- p + theme_classic()
p
〜

出来上がりはこんな感じ

r barplot
r barplot