[英]Plot a legend outside of the plotting area in base graphics?
正如標題所說:使用基本圖形時,如何在繪圖區域之外創建一個圖例?
我考慮過擺弄layout
並生成一個空的 plot 以僅包含圖例,但我會對僅使用基本圖形工具的方法感興趣,例如par(mar = )
在 plot 右側獲得一些空間為傳奇。
這里有一個例子:
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
legend(1,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
產生:
但如前所述,我希望圖例位於繪圖區域之外(例如,在圖形/繪圖的右側。
沒有人提到對legend
使用負inset
值。 這是一個示例,其中圖例位於圖的右側,並與頂部對齊(使用關鍵字"topright"
)。
# Random data to plot:
A <- data.frame(x=rnorm(100, 20, 2), y=rnorm(100, 20, 2))
B <- data.frame(x=rnorm(100, 21, 1), y=rnorm(100, 21, 1))
# Add extra space to right of plot area; change clipping to figure
par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
# Plot both groups
plot(y ~ x, A, ylim=range(c(A$y, B$y)), xlim=range(c(A$x, B$x)), pch=1,
main="Scatter plot of two groups")
points(y ~ x, B, pch=3)
# Add legend to top right, outside plot region
legend("topright", inset=c(-0.2,0), legend=c("A","B"), pch=c(1,3), title="Group")
inset=c(-0.2,0)
的第一個值可能需要根據圖例的寬度進行調整。
也許您需要的是par(xpd=TRUE)
來使事物能夠在繪圖區域之外繪制。 因此,如果使用bty='L'
進行主圖繪制,則bty='L'
的右側將有一些空間。 通常,這會被裁剪到繪圖區域,但是可以執行par(xpd=TRUE)
並進行一些調整,就可以得到一個盡可能遠的圖例:
set.seed(1) # just to get the same random numbers
par(xpd=FALSE) # this is usually the default
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
# this legend gets clipped:
legend(2.8,0,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
# so turn off clipping:
par(xpd=TRUE)
legend(2.8,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
除了已經提到的ondes(使用layout
或par(xpd=TRUE)
)之外,另一種解決方案是在整個設備上用透明圖覆蓋您的圖,然后在其中添加圖例。
訣竅是在整個繪圖區域上覆蓋一個(空)圖形,並在其中添加圖例。 我們可以使用par(fig=...)
選項。 首先,我們指示R在整個繪圖設備上創建一個新繪圖:
par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)
需要設置oma
和mar
,因為我們想讓繪圖的內部覆蓋整個設備。 需要new=TRUE
來防止R啟動新設備。 然后我們可以添加空圖:
plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
現在我們准備添加圖例:
legend("bottomright", ...)
將在設備的右下角添加圖例。 同樣,我們可以將圖例添加到頂部或右邊距。 我們唯一需要確保的是原始圖的邊距足夠大以容納圖例。
把所有這些都放到一個函數中;
add_legend <- function(...) {
opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0),
mar=c(0, 0, 0, 0), new=TRUE)
on.exit(par(opar))
plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
legend(...)
}
還有一個例子。 首先創建圖,確保底部有足夠的空間來添加圖例:
par(mar = c(5, 4, 1.4, 0.2))
plot(rnorm(50), rnorm(50), col=c("steelblue", "indianred"), pch=20)
然后添加圖例
add_legend("topright", legend=c("Foo", "Bar"), pch=20,
col=c("steelblue", "indianred"),
horiz=TRUE, bty='n', cex=0.8)
導致:
我喜歡這樣:
par(oma=c(0, 0, 0, 5))
plot(1:3, rnorm(3), pch=1, lty=1, type="o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch=2, lty=2, type="o")
legend(par('usr')[2], par('usr')[4], bty='n', xpd=NA,
c("group A", "group B"), pch=c(1, 2), lty=c(1,2))
唯一需要進行的調整就是將正確的邊距設置得足夠寬以容納圖例。
但是,這也可以自動化:
dev.off() # to reset the graphics pars to defaults
par(mar=c(par('mar')[1:3], 0)) # optional, removes extraneous right inner margin space
plot.new()
l <- legend(0, 0, bty='n', c("group A", "group B"),
plot=FALSE, pch=c(1, 2), lty=c(1, 2))
# calculate right margin width in ndc
w <- grconvertX(l$rect$w, to='ndc') - grconvertX(0, to='ndc')
par(omd=c(0, 1-w, 0, 1))
plot(1:3, rnorm(3), pch=1, lty=1, type="o", ylim=c(-2, 2))
lines(1:3, rnorm(3), pch=2, lty=2, type="o")
legend(par('usr')[2], par('usr')[4], bty='n', xpd=NA,
c("group A", "group B"), pch=c(1, 2), lty=c(1, 2))
對不起,我要復活舊線程,但是今天我遇到了同樣的問題。 我發現的最簡單的方法如下:
# Expand right side of clipping rect to make room for the legend
par(xpd=T, mar=par()$mar+c(0,0,0,6))
# Plot graph normally
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
# Plot legend where you want
legend(3.2,1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
# Restore default clipping rect
par(mar=c(5, 4, 4, 2) + 0.1)
在這里找到: http : //www.harding.edu/fmccown/R/
最近,我發現了一個非常簡單有趣的功能,可以在想要的繪圖區域之外打印圖例。
在圖的右側繪制外邊緣。
par(xpd=T, mar=par()$mar+c(0,0,0,5))
創建圖
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
添加圖例,只需使用locator(1)函數,如下所示。 然后,您只需在加載以下腳本后單擊所需的位置即可。
legend(locator(1),c("group A", "group B"), pch = c(1,2), lty = c(1,2))
試試吧
我只能提供已經指出的布局解決方案的示例。
layout(matrix(c(1,2), nrow = 1), widths = c(0.7, 0.3))
par(mar = c(5, 4, 4, 2) + 0.1)
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
par(mar = c(5, 0, 4, 2) + 0.1)
plot(1:3, rnorm(3), pch = 1, lty = 1, ylim=c(-2,2), type = "n", axes = FALSE, ann = FALSE)
legend(1, 1, c("group A", "group B"), pch = c(1,2), lty = c(1,2))
在我看來,添加另一個非常優雅的簡單替代方案。
您的情節:
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
傳說:
legend("bottomright", c("group A", "group B"), pch=c(1,2), lty=c(1,2),
inset=c(0,1), xpd=TRUE, horiz=TRUE, bty="n"
)
結果:
這里,圖例的第二行僅添加到您的示例中。 反過來:
inset=c(0,1)
-在(x,y)方向上按圖例區域的比例移動圖例。 在這種情況下,圖例位於"bottomright"
位置。 它在x方向上移動0個繪圖區域(因此保持在“右側”),並在y方向上(從底部到頂部)移動1個繪圖區域。 碰巧它出現在情節上方。 xpd=TRUE
讓圖例出現在繪圖區域之外。 horiz=TRUE
指示生成水平圖例。 bty="n"
-擺脫圖例邊界框的樣式細節。 將圖例添加到側面時同樣適用:
par(mar=c(5,4,2,6))
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
legend("topleft", c("group A", "group B"), pch=c(1,2), lty=c(1,2),
inset=c(1,0), xpd=TRUE, bty="n"
)
在這里,我們只是調整了圖例位置,並在圖的右側添加了額外的邊距空間。 結果:
您可以使用Plotly R API (使用代碼)或通過將GUI拖動到所需的圖例來執行此操作。
這是一個例子。 圖形和代碼也在這里 。
x = c(0,1,2,3,4,5,6,7,8)
y = c(0,3,6,4,5,2,3,5,4)
x2 = c(0,1,2,3,4,5,6,7,8)
y2 = c(0,4,7,8,3,6,3,3,4)
通過將x和y值之一分配給100或-100,可以將圖例放置在圖形之外。
legendstyle = list("x"=100, "y"=1)
layoutstyle = list(legend=legendstyle)
以下是其他選項:
list("x" = 100, "y" = 0)
表示外部右下角 list("x" = 100, "y"= 1)
右上角外 list("x" = 100, "y" = .5)
右外中 list("x" = 0, "y" = -100)
左下方 list("x" = 0.5, "y" = -100)
居中 list("x" = 1, "y" = -100)
在右下方 然后回應。
response = p$plotly(x,y,x2,y2, kwargs=list(layout=layoutstyle));
撥打電話時,使用圖形繪制出一個URL。 您可以通過調用browseURL(response$url)
更快地訪問它,這樣它將為您打開瀏覽器中的圖形。
url = response$url
filename = response$filename
這給了我們這張圖。 您也可以從GUI內移動圖例,然后圖形將相應縮放。 全面披露:我在Plotly團隊中。
嘗試在過去使用過的layout()
,只需在下面創建一個空圖,適當地縮放到1/4左右,然后手動將圖例部分放入其中即可。
這里有一些有關legend()
較舊問題,應該可以幫助您入門。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.