Jak rysować z png jako tłem? [duplikat]
To pytanie ma już odpowiedź tutaj:
- nakładanie danych na obraz tła 3 odpowiedzi
Zrobiłem fabułę z 3 milionami punktów i zapisałem ją jako PNG. Zajęło to kilka godzin i chciałbym uniknąć ponownego wyciągania wszystkich punktów.
Jak mogę wygenerować nowy wykres, który ma ten PNG jako tło?
2 answers
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ę Wykres.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-11 20:07:36
Podczas gdy odpowiedź @bill_080 bezpośrednio odpowiada na twoje pytanie, czy naprawdę tego chcesz? Jeśli chcesz to wykreślić, musisz dokładnie wyrównać swoje układy współrzędnych. Zobacz np. Houston Crime Map Jak można to zrobić z ggplot2.
Dla Twojego problemu, wydaje mi się, że może być łatwiejsze rozwiązanie: binning, czyli ceating histogramów 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
Hexbin działa bezpośrednio z kratką i ggplot2, ale współrzędne środkowe pojemników są w binned@xcm
i binned@ycm
, więc można również wykreślić wynik w grafice bazowej. Z dużą liczbą koszy, otrzymujesz szybką wersję oryginalnej fabuły:
> system.time (plot (binned@xcm, binned@ycm, pch = 20, cex=0.4))
User System verstrichen
0.780 0.004 0.786
Ale można łatwo mieć kolory kodujące gęstość:
> plot (binned@xcm, binned@ycm, pch = 20, cex=0.4, col = as.character (col))
> col <- cut (binned@count, 20)
> levels (col) <- grey.colors (20, start=0.9, end = 0)
> plot (binned@xcm, binned@ycm, pch = 20, cex=0.4, col = as.character (col))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-16 08:01:01