[英]R ggplot: geom_tile lines in pdf output
我正在構造一個使用geom_tile的圖,然后將其輸出到.pdf(使用pdf(“ filename”,...))。 但是,當我這樣做時,.pdf結果中會出現細線(如一個人所說的條紋)。 我已附上一張顯示問題的圖像。
谷歌搜索讓這個線程 ,但那里唯一的真正建議是嘗試將size = 0傳遞給geom_tile,但我沒有效果。 關於如何解決這些問題的任何建議? 我想用它作為論文中的一個數字,但是那樣行不通。
最小代碼:
require(ggplot2)
require(scales)
require(reshape)
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
v <- ggplot(volcano3d, aes(x, y, z = z))
pdf("mew.pdf")
print(v + geom_tile(aes(fill=z)) + stat_contour(size=2) + scale_fill_gradient("z"))
發生這種情況是因為geom_tile
中的圖塊的默認colour
似乎是白色。
要解決此問題,您需要使用與fill
相同的方式將colour
映射到z。
print(v +
geom_tile(aes(fill=z, colour=z), size=1) +
stat_contour(size=2) +
scale_fill_gradient("z")
)
嘗試使用geom_raster
:
pdf("mew.pdf")
print(v + geom_raster(aes(fill=z)) + stat_contour(size=2) + scale_fill_gradient("z"))
dev.off()
我的環境質量很好。
我無法在計算機(Windows 7)上重現該問題,但我記得這是某些配置列表中討論的問題。 Brian Ripley(如果我還記得的話)推薦了
CairoPDF("mew.pdf") # Package Cairo
繞過這個
為了給這只貓蒙皮,並增加過多細節,此代碼將R圖像分解為一個四邊形網格(如rgl
所用),然后顯示了柵格圖與“平鋪”或“平鋪”之間的差異rect”劇情。
library(raster)
im <- raster::raster(volcano)
## this is the image in rgl corner-vertex form
msh <- quadmesh::quadmesh(im)
## manual labour for colour scaling
dif <- diff(range(values(im)))
mn <- min(values(im))
scl <- function(x) (x - mn)/dif
這是傳統的R“圖像”,它為每個像素繪制一個小圖塊或“ rect()”。
list_image <- list(x = xFromCol(im), y = rev(yFromRow(im)), z = t(as.matrix(im)[nrow(im):1, ]))
image(list_image)
速度很慢,盡管它在后台調用了“ rect()”的來源,但我們也無法設置邊框顏色。 使用'useRaster = TRUE'可以使用'rasterImage'獲得更有效的繪圖時間,控制插值以及最終文件大小。
現在,讓我們再次繪制圖像,但是通過為每個像素顯式調用rect。 (“ quadmesh”可能不是最簡單的演示方式,在我心中只是新鮮的)。
## worker function to plot rect from vertex index
rectfun <- function(x, vb, ...) rect(vb[1, x[1]], vb[2,x[1]], vb[1,x[3]], vb[2,x[3]], ...)
## draw just the borders on the original, traditional image
apply(msh$ib, 2, rectfun, msh$vb, border = "white")
現在使用“ rect”重試。
## redraw the entire image, with rect calls
##(not efficient, but essentially the same as what image does with useRaster = FALSE)
cols <- heat.colors(12)
## just to clear the plot, and maintain the plot space
image(im, col = "black")
for (i in seq(ncol(msh$ib))) {
rectfun(msh$ib[,i], msh$vb, col = cols[scl(im[i]) * (length(cols)-1) + 1], border = "dodgerblue")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.