2015-04-17 39 views
0

它已被問到,但我找不到確切的答案。我想知道以下是否可行。在註釋()中插入自定義文本 - R不起作用

我在ggplot2庫中有一堆散點圖數據,每個數據都有不同的平均值和標準偏差以及觀察次數。我想在每個不同的情節之上打印這些信息。因此,我正在計算並希望打印帶註釋的人。我在做什麼如下:

m_axl5 <- as.character(round(mean(newdata$axl5), 5)) 
sd_axl5 <- as.character(round(sd(newdata$axl5), 5)) 
txt_m <- paste ("mean:", m_axl5) 
txt_sd <- paste ("stdev:", m_axl5) 
txt <-paste (txt_m, txt_sd, sep = '\n') 
axl5 <- ggplot (newdata, aes(y=axl5, x= row.names(newdata))) + geom_point(position = position_jitter(w=0.1, h=0), colour= "red")+ 
    ggtitle("Axle 5 Weight - Match Error Difference") + xlab("") + ylab("Axle 5 Weight Match Error") 
axl5 + theme(axis.ticks = element_blank(), axis.text.x = element_blank()) +annotate("text", x = 1, y = 10, label = txt) 

每當我做到這一點,它給了我下面的錯誤:

Error: Incompatible lengths for set aesthetics: label 

我找不到定製標籤這種方式或註釋文本的方式作爲我研究的結果在ggplot上。如果有人能夠幫助我,或者至少指導我,如果這件事早點完成的話,我會感到非常高興。

謝謝。

回答

0

而不是使用註釋的(),你可以做一些這樣的事

plot.title = 'Axle 5 Weight - Match Error Difference' 
plot.subtitle = paste(txt_m, txt_sd, sep = '-') 

ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), "")))) 

參考:Add dynamic subtitle using ggplot

+0

哇哦謝謝!從來沒有想過用這種方式搜索Google!大幫忙:) – kukushkin