|||
网上看到 YGC 网友在博客上用 R 语言绘制的一幅柱形图,该图有一个特点,就是两个柱形图之间有一个星号标记并有线条将两个柱形图连接。看了其公布的代码,摘抄如下,并作了些许改动。
Normal <- c(0.83, 0.79, 0.99, 0.69)
Cancer <- c(0.56, 0.56, 0.64, 0.52)
m <- c(mean(Normal), mean(Cancer))
s <- c(sd(Normal), sd(Cancer))
d <- data.frame(V = c("Normal", "Cancer"), mean = m, sd = s)
d$V <- factor(d$V, levels = c("Normal", "Cancer"))
library(ggplot2)
p <- ggplot(d, aes(V, mean, fill = V, width = 0.5))
p <- p + geom_errorbar(aes(ymin = mean, ymax = mean + sd, width = 0.2),
position = position_dodge(width = 0.8)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8),
colour = "black") +
scale_fill_manual(values = c("grey80", "white")) +
theme_bw() + theme(legend.position = "none") + xlab("") + ylab("") +
theme(axis.text.x = element_text(face = "bold", size = 12),
axis.text.y = element_text(face = "bold", size = 12),
panel.grid = element_blank()) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1.2),
breaks = seq(0, 1.2, by = 0.2)) +
geom_segment(aes(x = 1, y = 0.98, xend = 1, yend = 1.1)) +
geom_segment(aes(x = 2, y = 0.65, xend = 2, yend = 1.1)) +
geom_segment(aes(x = 1, y = 1.1, xend = 1.45, yend = 1.1)) + #与源代码有差异,使得“*”在连线中间
geom_segment(aes(x = 1.55, y = 1.1, xend = 2, yend = 1.1)) +
annotate("text", x = 1.5, y = 1.1, label = "*")
ggsave(plot = p, filename = "2.jpeg")
图形显示如下:
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 10:20
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社