2017-04-20 35 views
0

我通常在ggplot文本對象的位置美學中使用無限值​​,以使標籤出現在繪圖的角落,而不管數據比例如何。在製作多面板數字時,我主要使用這種方法,在每個面板中應該有字母來識別圖例中的每個面板。但是,如果我希望標籤出現在左側或底部,這似乎不適用於日誌比例,因爲明顯轉換日誌(-Inf)會返回NaN。有這個簡單的解決辦法嗎?我可以做一個很長的解決方法,但我希望有更容易的事情。例子是以下:如何使文本出現在ggplot的角上,並且具有對數刻度

notlogdata <- data.frame(x = 1:3,y = 1:3) 
ggplot(notlogdata, aes(x = x,y = y)) + 
    geom_point() + 
    geom_text(data = data.frame(x = -Inf, y = Inf, l = 'a'), aes(label = l), hjust = -0.5, vjust = 1) 

logdata <- data.frame(x = 10^(1:3), y = 10^(1:3)) 
ggplot(logdata, aes(x = x,y = y)) + 
    geom_point() + 
    geom_text(data = data.frame(x = -Inf, y = Inf, l = 'a'), aes(label = l), hjust = -0.5, vjust = 1) + 
    scale_x_log10() + 
    scale_y_log10() 

與未轉化的軸線首先情節細出現: plot with untransformed axes

的第二曲線不具有標籤,並返回警告:

警告消息:
1 :自我$ trans $ transform(x):產生的NaN
2:刪除了包含缺失值(geom_text)的1行。

+1

您已定義'X = -Inf,Y = Inf'然後使用'scale_x_log10'。你不能採用'inf'的'log'。 –

+1

請參閱[cowplot包](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html),將多個ggplots與標籤放在一起 – zx8754

回答

0

似乎不可能在對數轉換軸上使用負定位美學,包括-Inf。下面是使用cowplot溶液通過zx8754所建議

library(cowplot) 

notlogdata <- data.frame(x = 1:3,y = 1:3) 
notlogplot <- ggplot(notlogdata, aes(x = x,y = y)) + 
    geom_point() 

logdata <- data.frame(x = 10^(1:3), y = 10^(1:3)) 
logplot <- ggplot(logdata, aes(x = x,y = y)) + 
    geom_point() + 
    scale_x_log10() + 
    scale_y_log10() 

plot_grid(notlogplot, logplot, labels=c('a','b'), hjust=-8) 

此輸出以下情節:

cowplot image

1
annotation_custom(gTree(children=gList(textGrob("a", hjust=0,x=0,vjust=1,y=1)))) 

enter image description here