2011-03-11 10 views
37

Zrobiłem fabułę z 3 milionami punktów i zapisałem ją jako PNG. Zajęło mi to kilka godzin i chciałbym uniknąć ponownego rysowania wszystkich punktów.Jak wykreślić za pomocą png jako tło?

enter image description here

Jak mogę wygenerować nowy spisek, który ma tę PNG jako tło?

+2

Nigdy go nie użyłem, ale pakiet 'png' może mieć to, czego szukasz: http://cran.r-project.org/web/packages/png/png.pdf – Chase

+1

Byłoby dobrze dodać trochę t do twoich punktów, dzięki czemu możesz lepiej zobaczyć ich dystrybucję. Jak w spisku gęstości. – Rodrigo

+1

Zobacz http://stackoverflow.com/a/42611002/15485 –

Odpowiedz

77

Spróbuj tego:

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") 

Poniżej znajduje się działka.

enter image description here

15

Podczas użytkownika @ bill_080 odpowiedź bezpośrednio odpowiedzi na pytanie, czy to jest naprawdę to, co chcesz? Jeśli chcesz na to spisać, musisz dokładnie wyrównać swoje układy współrzędnych. Zobacz np. Houston Crime Map jak to zrobić z ggplot2.

Dla twojego problemu wydaje mi się, że może być łatwiejsze rozwiązanie: binning, czyli kończenie 2d histogramów.

> 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 współpracuje bezpośrednio z siatki i ggplot2, ale centrum współrzędne pojemniki są w [email protected] i [email protected], więc można też wykreślić wynik w grafice bazowych. Z dużej liczby pojemników, można uzyskać szybki wersji oryginalnej fabuły:

> 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

ale łatwo można mieć kolory kodujące gęstość:

> 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