R语言ggplot绘制线图

2017-11-17  本文已影响74人  x2yline

如题,绘制5组数据的线图(2个对照,3个实验)


# 输入数据
control_1 <- as.numeric(strsplit("81.06 62.23   67.75   77.68   77.84   75.27   72.31   72.31   71.30   68.77   67.01   66.74",
                      "\\s")[[1]])

control_2 <- as.numeric(strsplit("64.32 45.94   51.70   50.80   52.14   56.72   63.01   63.01   61.74   63.32   58.83   61.32", 
                      "\\s")[[1]])
treat_1 <- as.numeric(strsplit("91.48   61.04   69.40   63.75   63.34   68.97   66.77   44.60   49.79   50.45   56.15   42.72",
                    "\\s")[[1]])
treat_2 <- as.numeric(strsplit("96  64.05   69.92   67.84   65.90   65.92   70.23   55.57   50.38   53.80   50.45   50.30",
                    "\\s")[[1]])
treat_3 <- as.numeric(strsplit("48.21   48.21   51.69   45.42   55.23   60.19   64.61   45.42   36.10   35.21   30.94 22.31",
                    "\\s")[[1]])
time <- c(-40, -10, 0, 5, 15, 30, 60, 60.2, 65, 75, 90, 120)

# 绘图数据框
data_raw <- data.frame(bp=c(control_1[!is.na(control_1)],
                            control_2[!is.na(control_2)],
                            treat_1[!is.na(treat_1)], 
                            treat_2[!is.na(treat_2)], 
                            treat_3[!is.na(treat_3)]),
                       time=time,
                       differ=c(rep(c("control"), 12*2), rep("treat", 12*3)),
                       groups=rep(c("control_1", "control_2", "treat_1", "treat_2", "treat_3"), each=12))


# ggplot绘图
library(ggplot2)

# 构造一个函数,改变facet分面的标签
labeli <- function(variable, value){
  names_li <- list("control"="持续缺血组", "treat"="缺血再灌注组")
  return(names_li[value])
}

p <- ggplot(data_raw, aes(time, bp, group=groups, color=groups))+ 
  facet_wrap(~differ, labeller=labeli)+
  geom_line(aes(x = time, y = bp), lwd=1, alpha=1)+
  geom_point()+ylab("血压/mmHg")+xlab("")+
  scale_x_continuous(breaks=time[c(1, 2,  6, 7, 11)],
                     limits=c(-40, 130), 
                     labels=c("正常", "结扎", "30min", "再灌", "30min"))+
  geom_vline(xintercept=c(-10, 60), lwd=0.7, color="gray10", alpha=0.4, lty=2)
  
# 改变坐标轴的字体
p+theme_minimal()+theme(axis.text.x=element_text(face="bold", size=12), 
                        axis.text.y=element_text(face="bold", size=12), 
                        axis.title.y=element_text(size=13, face="bold"))+
  theme(text=element_text(family="SimHei"))+
  # 自定义图例
  scale_colour_discrete(name  ="组别",
                       breaks=c("control_1", "control_2", "treat_1", "treat_2", "treat_3"),
                       labels=c("持续缺血组1", "持续缺血组2", "缺血再灌注组4", "缺血再灌注组1", "缺血再灌注组3"))+
# 自定义facet分面标签的字体、颜色和背景
  theme(strip.text.x = element_text(size = 15, colour = "red", face="bold", angle = 360),strip.background=element_blank())
# theme(strip.background = element_blank(), strip.text = element_blank())


最后的结果图如下:

再灌实验结果.png

经过修饰后:

再灌实验结果2.png
上一篇 下一篇

猜你喜欢

热点阅读