绘制ggplot相位图高级标识

来源:互联网 发布:linux grep命令详解 编辑:程序博客网 时间:2024/06/15 20:33

受师弟询问启发做的ggplot箱线图高级标识



rm(list = ls())
library(ggplot2)
phdfig23 = read.table("PhD_fig23.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
phdfig23_long = with(phdfig23,
                     rbind( data.frame( lesion=group, stimulus="CS+", approaches=CSplus, sem=SEMCSplus ),
                            data.frame( lesion=group, stimulus="CS-", approaches=CSminus, sem=SEMCSminus ) )
)

q = ggplot(data = phdfig23_long, aes(x=lesion, y=approaches, fill=stimulus, width=0.5) ) + # width here determines bar spacing
  # ... see http://stats.stackexchange.com/questions/6204/how-to-increase-the-space-between-the-bars-in-a-bar-plot-in-ggplot2
  geom_errorbar(aes(ymin=approaches, ymax=approaches+sem, width = 0.2), position=position_dodge(width=0.8)) +
  geom_bar(stat="identity", position=position_dodge(width=0.8)) + # WORKAROUND, as above
  geom_bar(stat="identity", position=position_dodge(width=0.8), colour="black", legend=FALSE) + # WORKAROUND, as above
  scale_fill_manual(values=c("grey80", "white")) +
  scale_x_discrete("Group", limits=c("sham", "ACCX")) + # a way to specify the order of factors directly
  scale_y_continuous("Number of approaches", expand=c(0,0), limits = c(0, 17), breaks=seq(0, 16, by=2)) +
  geom_segment(aes(x=0.8, y=15.3, xend=0.8, yend=16)) + #从这一步开始做线,起始位置为(0.8,15.3)拉伸到(0.8,16)就是横坐标不变,增坐标从15.3拉高到16
  geom_segment(aes(x=0.8, y=16, xend=1.2, yend=16)) +   #同理起始位置是(0.8,16)拉伸到(1.2,16)纵坐标不变,横坐标从0.8拉伸到1.2
  geom_segment(aes(x=1.2, y=6, xend=1.2, yend=16)) +    #同理起始位置是(1.2,6)拉伸到(1.2,16)横坐标不变,纵坐标从6拉伸到16
  annotate("text", x=1, y=15.5, label="***") +          #这里开始填写你需要些的东西,注意横坐标在0.8-1.2范围内,纵坐标在15.3-16之间
  theme_bw() +  #以下labs为图形各个参数的配置
  labs(title = "Autoshaping probe test",
       plot.title = element_text(face="bold", size=14),
       axis.title.x = element_text(face="bold", size=12),
       axis.title.y = element_text(face="bold", size=12, angle=90),
       panel.grid.major = element_blank(),
       panel.grid.minor = element_blank(),
       axis.text.y=element_text(angle=90, hjust=0.5), # rotate the Y axis text anticlockwise 90 degrees, and centre it (0 left, 0.5 centre, 1 right)
       legend.title = element_blank(),
       legend.position = c(0.85,0.85),
       legend.key.size = unit(1.5, "lines"),
       legend.key = element_rect() # provides a border to the coloured squares in the legend
  )
print(q)

原创粉丝点击