如何在每个情节的顶部而不是在ggplot的情节内绘制文字?

时间:2015-11-10 18:04:19

标签: r ggplot2

我有这样的df:

Name <- c('A','A','A','A','A','A','A','A','A','A')
Measurement <- c('Length','Length','Length','Length','Length','Length','Length','Length','Length','Length')
Meas_Avg <- c(0.125,0.150,0.122,0.177,0.145,0.135,0.180,0.133,0.174,0.149)
Tool <- c('Tool1','Tool3','Tool2','Tool3','Tool3','Tool1','Tool1','Tool2','Tool2','Tool1')

df <- data.frame(Name,Measurement,Meas_Avg,Tool)
pvalue <- 0.056

我有一个文本“pvalue”,我在绘图时手动定位在ggplot内。虽然这对于一个情节来说似乎很好,但是如果我将其用于其余的情节,这种做法并不正确,因为情节中的值会发生变化,并且箱图会在整个绘图区域内移动。这是我的ggplot函数

ggplot(data = df, aes(x=Tool, y=Meas_Avg)) + 
  theme(axis.text=element_text(size=14)
        , axis.title=element_text(size=14,face="bold")) +
  geom_boxplot(outlier.colour = "red", outlier.size=3, outlier.shape=15, fill="gray95", colour="blue") + 
  geom_point(alpha=0.6, position=position_jitter(w=0.05, h=0.0), aes(colour=Tool), size=5) + 
  facet_grid(Measurement~Name,scales = "free", space = "free") +
  scale_color_manual(values = c("red", "magenta", "dodgerblue2","green","blue","orange","black","violet")) +
  scale_size_area() +
  geom_text(data = pval_TAP_5, aes(label=paste("pvalue= ",pvalue), x=2.0, y=0.25),size=7) +
  theme(strip.text.x = element_text(size = 15, colour = "red",face="bold")
        ,strip.text.y = element_text(size = 15, colour = "red",face="bold"))

获得

enter image description here

我想在图表的顶部绘制文本“pvalue = 0.056”(在这种情况下,在“A”之上,而不是在内部手动执行。如何实现?

1 个答案:

答案 0 :(得分:1)

您可以尝试重命名Name列,如下所示:

df$Name <- paste0(df$Name, "\n pvalue = ", pvalue)

enter image description here