我正在嘗試繪制帶有方面的圖并在每個方面添加注釋。
我希望注釋看起來像“趨勢的p =0.001”,就像這個影像這樣的情節。

我繪制的圖如下所示:

ann_text <- data.frame(x = rep(5,6), y = rep(1,6),
conf.low = rep(5,6), conf.high = rep(5,6),
term = c('A', 'B', 'C', 'D', 'E', 'F'),
pvalue = c("==0.04", "<0.001", "==0.999", "==0.999", "==0.99", "==0.99"))
ann_text$term <- factor(ann_text$term)
p <- ggplot(ann_text, aes(x = x, y = y))
facet_wrap(.~term)
geom_text(data = ann_text,
aes(label=paste0(rep('italic(p)~fo~trend', 6), pvalue)),
parse = TRUE, size = 7)
p
我想我快到了,但是注釋中缺少“r”,因為當我添加“r”時,它會引發以下錯誤訊息。
Error in parse(text = text[[i]]) : <text>:1:14: unexpected '~'
1: italic(p)~for~
^
我怎么解決這個問題?
uj5u.com熱心網友回復:
在單引號運算式中使用雙引號來指定您不想解釋為代碼的字串。順便說一句,您不需要使用,rep()因為它paste0()是矢量化的,并且會將該'italic(p)~"for trend"'部分應用于存在的多個pvalue元素。所以,像這樣:
p <- ggplot(ann_text, aes(x = x, y = y))
facet_wrap(.~term)
geom_text(data = ann_text,
aes(label=paste0('italic(p)~"for trend"', pvalue)),
parse = TRUE, size = 7)
p
uj5u.com熱心網友回復:
一些想法:
我認為,for在這種情況下,它被識別為函式、命令或運算子之類的東西。
而 R 等待某事 la:for == bla-bla, for(bla-bla)等等...
怎么解決:
我試圖用拉丁語 unicodef o r字符解決這個問題。但它沒有幫助。
但是我們有西里爾小寫字母 O,哈哈。它有幫助!;)
您修改后的代碼:
ann_text <- data.frame(x = rep(5,6), y = rep(1,6),
conf.low = rep(5,6), conf.high = rep(5,6),
term = c('A', 'B', 'C', 'D', 'E', 'F'),
pvalue = c("==0.04", "<0.001", "==0.999", "==0.999", "==0.99", "==0.99"))
ann_text$term <- factor(ann_text$term)
p <- ggplot(ann_text, aes(x = x, y = y))
facet_wrap(.~term)
geom_text(data = ann_text,
aes(label=paste0(rep('italic(p)~f\U043Er~trend', 6), pvalue)),
parse = TRUE, size = 7)
p
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/396755.html
