2016-08-08 173 views
0

這是一個帶有兩個分組條形圖的facet_wrap圖。我在下面的文本中附上了我的問題和代碼。多級分組條形圖

enter image description here

Q1:如何在指定位置標註星號三聯(更好的可自動定位的位置沒有我的計算/規格),如情節繪製的?試過但沒有工作:

ann_text1=data.frame(ligand=c(2), uptake=c(24), kd=factor("p", levels=c("p","q"))) 
tp=t+geom_text(data=ann_text1,label="***", size=8) 

問題2:如何在條形標題中使用希臘字母(測試版)? 試圖在標籤中整合表達式參數=但沒有奏效。

t$cell=factor(t$cell, levels = unique(t$cell), labels = c("WT","beta-DKO")) 

這是我試過的代碼的完整版本。請通過這個link導航輸入文件。

library(ggplot2) 
library(Hmisc) 
library(gridExtra) 
library(grid) 

t=read.csv(file.choose(), header = TRUE) 
t$ligand = factor(t$ligand, levels = unique(t$ligand)) 
t$kd=factor(t$kd, levels = unique(t$kd)) 
t$cell=factor(t$cell, levels = unique(t$cell), labels = c("WT","beta-DKO")) 

tbar=ggplot(t, aes(kd, uptake, fill=ligand)) + 
    stat_summary(fun.y = mean, geom = "bar", position="dodge", color = "black", size=0.3, width = 0.6) + 
    stat_summary(fun.data = mean_se, geom = "errorbar", position = position_dodge(width = 0.6), size=0.3, width = .2) + 
    scale_fill_manual(name="ligand", labels=c("-L", "+L"), values=c("gray20", "gray80"))+ 
    theme_bw()+ 
    scale_y_continuous(expand = c(0,0), limit = c(0, 120), breaks = seq(0, 110, 20))+ 
    ylab("% of T")+ 
    facet_wrap(~cell, nrow=1)+ 
    theme(legend.direction="horizontal",legend.position=c(0.88,0.92))+ 
    theme(legend.text=element_text(size=9))+ 
    theme(legend.key.size=unit(0.25,"cm")) 

回答

0

您可以添加stat_summary(geom="text")按照此線程:Annotation above bars:

library(ggplot2) 
library(Hmisc) 
library(gridExtra) 
library(grid) 

t=read.csv("https://dl.dropboxusercontent.com/u/1204710/test.csv", header = TRUE) 
t$ligand = factor(t$ligand, levels = unique(t$ligand)) 
t$kd=factor(t$kd, levels = unique(t$kd)) 
t$cell=factor(t$cell, levels = unique(t$cell), labels = c("WT","beta-DKO")) 
## parametrise dodging width; add vertical shift to '***' label 
width <- 0.6 
vshift <- 5 
dodgewidth <- position_dodge(width=width) 
## identify bars where '***' label has to be placed using a factor 
t$mylabel <- "" 
t$mylabel[with(t, ligand=="y" & cell=="WT" & kd=="p")] <- "***" 
t$mylabel[with(t, ligand=="y" & cell=="beta-DKO" & kd=="q")] <- "***" 
t$mylabel <- factor(t$mylabel) 

tbar <- ggplot(t, aes(kd, uptake, fill=ligand)) + 
    stat_summary(fun.y = mean, geom = "bar", position="dodge", color = "black", size=0.3, width = width) + 
    stat_summary(fun.y = function(v) mean(v)+vshift, geom = "text", position=dodgewidth, aes(label=mylabel)) + 
    stat_summary(fun.data = mean_se, geom = "errorbar", position = position_dodge(width = width), size=0.3, width = .2) + 
    scale_fill_manual(name="ligand", labels=c("-L", "+L"), values=c("gray20", "gray80"))+ 
    theme_bw()+ 
    scale_y_continuous(expand = c(0,0), limit = c(0, 120), breaks = seq(0, 110, 20))+ 
    ylab("% of T")+ 
    facet_wrap(~cell, nrow=1)+ 
    theme(legend.direction="horizontal",legend.position=c(0.88,0.92))+ 
    theme(legend.text=element_text(size=9))+   
    theme(legend.key.size=unit(0.25,"cm")) 

print(tbar)