2013-11-28 21 views
2

我在x軸上有一個帶有對數刻度的圖形。嘗試創建一個插入圖不起作用,但如果比例更改爲線性,則顯得很好。有沒有辦法解決這個問題,還是ggplot的限制?如何讓ggplot2顯示一個插圖,當主人有一個日誌規模?

這工作:

p = qplot(1:10, 1:10) 
g = ggplotGrob(qplot(1, 1)) 
p + annotation_custom(grob = g, xmin = 3, xmax = 6, ymin = 6, ymax = 10) 

這不:

p = qplot(1:10, 1:10, log='x') 
g = ggplotGrob(qplot(1, 1)) 
p + annotation_custom(grob = g, xmin = 3, xmax = 6, ymin = 6, ymax = 10) 

回答

5

隨着一個對數刻度,只需使用指數而不是絕對值值來指定座標。所以,在這個例子中,使用0 < X < 1,因爲規模從1E0運行到1E1:

p = qplot(1:10, 1:10, log='x') 
g = ggplotGrob(qplot(1, 1)) 
p + annotation_custom(grob = g, xmin = 0.3, xmax = 0.9, ymin = 6, ymax = 10) 
+1

對於通用的解決方案:http://stackoverflow.com/a/37857548/956338(但這個答案真的幫助我開始) – pallevillesen

3

首先,我也用GGPLOT2來繪製數刻度插圖情節有問題。

但是,在使用grid格式的viewport之前,我已經做了一些工作。

viewport描述:

These functions create viewports, which describe rectangular regions on a graphics device and define a number of coordinate systems within those regions.

基本上你可以重疊在一個又一個陰謀,一個對另一個...

(1)您可以取消命令,所以你可以輸出到一個PNG容易或使用dev.copy2**

(2)x,y,寬度,高度可以指定爲unit對象,更多關於grid :: unit的信息,請點擊here

require(grid) 
require(ggplot2) 
p = qplot(1:10, 1:10, log="x") 
g = qplot(0.1 , 0.1) 
vp1 <- viewport(width = 0.3, 
       height = 0.3, 
       x = 0.4, 
       y = 0.7) 
vp2 <- viewport(width = 0.3, 
       height = 0.3, 
       x = 0.8, 
       y = 0.3) 
#png("text.png") 
print(p) 
print(g, vp = vp1) 
print(g, vp = vp2) 
#dev.off() 

viewport

3

隨着數比例,X被解釋爲0到1:

p = qplot(1:10, 1:10, log='x') 
g = ggplotGrob(qplot(1, 1)) 
p + annotation_custom(grob = g, xmin = 0.3, xmax = 0.9, ymin = 6, ymax = 10) 

所以只是使它按比例

enter image description here

相關問題