2011-03-11 75 views
37

我做了一個陰謀以3萬點並將其保存爲PNG。花了幾個小時,我想避免重新繪製所有要點。如何用PNG作爲背景情節?

enter image description here

怎樣才能具有此PNG作爲背景的新陰謀?

+2

從來沒有使用過,但包'png'可能你以後:http://cran.r-project.org/web/packages/png/png.pdf – Chase

+1

這將是很好的補充一些牛逼透視你的觀點,所以你可能會看到他們的分佈更好。就像在密度圖中一樣。 – Rodrigo

+1

看一看http://stackoverflow.com/a/42611002/15485 –

回答

77

試試這個:

library(png) 

#Replace the directory and file information with your info 
ima <- readPNG("C:\\Documents and Settings\\Bill\\Data\\R\\Data\\Images\\sun.png") 

#Set up the plot area 
plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y") 

#Get the plot information so the image will fill the plot box, and draw it 
lim <- par() 
rasterImage(ima, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4]) 
grid() 
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="white") 

下面是劇情。

enter image description here

15

雖然@ bill_080的回答直接回答你的問題,這真的是你想要的嗎?如果你想繪製這個,你必須仔細對齊你的座標系。見例如Houston Crime Map ggplot2如何做到這一點。

對於您的問題,在我看來,有可能是一個簡單的解決方案:合併,即ceating 2D直方圖。

> df <- data.frame (x = rnorm (1e6), y = rnorm (1e6)) 
> system.time (plot (df)) 
     User  System verstrichen 
    54.468  0.044  54.658 
> library (hexbin) 
> system.time (binned <- hexbin (df, xbins=200)) 
     User  System verstrichen 
     0.252  0.012  0.266 
> system.time (plot (binned)) 
     User  System verstrichen 
     0.704  0.040  0.784 

enter image description here

hexbin直接與晶格和GGPLOT2,但倉的中心座標是[email protected][email protected],所以你也可以繪製結果的基礎圖形。隨着高箱數,你會得到你的原創情節的快速版本:

> system.time (plot ([email protected], [email protected], pch = 20, cex=0.4)) 
     User  System verstrichen 
     0.780  0.004  0.786 

enter image description here

,但你可以很容易地有顏色編碼的密度:

> plot ([email protected], [email protected], pch = 20, cex=0.4, col = as.character (col)) 

> col <- cut ([email protected], 20) 
> levels (col) <- grey.colors (20, start=0.9, end = 0) 
> plot ([email protected], [email protected], pch = 20, cex=0.4, col = as.character (col)) 

enter image description here