構建數(shù)據(jù)
data = data.frame(TN = sample(c("Tumor","Normal"),300,replace=T),
variable = sample(c("g1","g2","g3"),300,replace=T),
value = rnorm(300, mean = 50, sd = 10))
開始畫圖
library(ggplot2)
library(ggbeeswarm)
ggplot(data, aes(x = variable, y = value, color = TN)) +
geom_beeswarm(#cex = 2,
#priority = "density",
shape = 23,
cex = 1,
corral = "gutter", #捕獲逃逸點方式
corral.width = 0.3, #組內(nèi)寬度
dodge.width = 0.7) + #組間寬度
#添加中位數(shù),上椅文、下四分位數(shù)指示線
stat_summary(data = data,
position = "dodge",#dodge 類似柱狀圖中讓分組并排
mapping = aes(x = variable, y = value,
group=TN), #關鍵參數(shù) group####
fun.y = median, fun.ymin = median, fun.ymax = median,
geom = 'crossbar',
width = 0.7, #控制橫線寬度
size = 0.3,#控制橫線粗線
color = 'black') +
stat_summary(data = data,
position = "dodge",#dodge,identity
mapping = aes(x = variable, y = value,
group=TN),#關鍵參數(shù) group####
fun.data = function(x) median_hilow(x, 0.5),
geom = 'errorbar',
width = 0.7, #控制橫線寬度
color = 'black')+
scale_color_manual(values = c("blue", "red")) +
scale_y_continuous(position = "left") +
#coord_flip() +
#scale_x_discrete(limits = rev(level)) +
theme(axis.line = element_line(colour = "black"),
panel.background = element_blank(),
axis.ticks = element_line(),
axis.text = element_text(color = "black")) +
labs(x = " " , y = "")
結果