2017-03-09 82 views
1

我有兩個第一列描述點座標(X,Y),而下兩列存儲值的數據框。如何繪製XYZ點並取決於R中Z值的點大小?

我想繪製它們(在地圖上)作爲點,但我希望點的大小取決於值(例如Z1值)。

如何在R中做到這一點?

我知道有一個rasterFromXYZ命令,但我不知道對應點。

我的樣本數據如下:

myData <- structure(list(X = c(20.688, 18.905, 19.086, 21.135, 21.979, 
22.495), Y = c(52.387, 53.747, 52.495, 53.466, 54.093, 52.879 
), Z1 = c(1050L, 359L, 424L, 393L, 1478L, 573L), Z2 = c(48L, 
38L, 20L, 150L, 138L, 120L)), .Names = c("X", "Y", "Z1", "Z2" 
), row.names = c(NA, 6L), class = "data.frame") 

回答

2

使用ggmap庫,你可以這樣做:

# set up the sample dataset 
myData <- structure(list(X = c(20.688, 18.905, 19.086, 21.135, 21.979, 
           22.495), Y = c(52.387, 53.747, 52.495, 53.466, 54.093, 52.879 
           ), Z1 = c(1050L, 359L, 424L, 393L, 1478L, 573L), Z2 = c(48L, 
                         38L, 20L, 150L, 138L, 120L)), .Names = c("X", "Y", "Z1", "Z2" 
                         ), row.names = c(NA, 6L), class = "data.frame") 

# load the ggmap library 
library(ggmap) 

# set the part of the world to show 
location <- c(lon = mean(myData[, "X"]), lat = mean(myData[, "Y"])) 

# choose map type 
maptype = "terrain" 

# get the map from google (choose zoom you like, or let it choose manually) 
map <- get_map(location = location, source = "google", 
       maptype = maptype, crop = FALSE, zoom = 6) 

# print the map 
ggmap(map) + 
     # add the points, where X is longitude, Y latitude and size is calculated based on Z1 (you might choose some other scaling of Z1) 
     geom_point(aes(x = X, y = Y, size = 1+Z1/sum(Z1)), data = myData) 
+0

謝謝!我能夠在同一圖上使用陰謀(從光柵包)?我需要繪製顯示國家邊界的矢量文件(.shp)。 – matandked

+0

您可以創建一個柵格對象,然後使用inset_raster或inset_ggmap來繪製它 – ira

+0

另外,如果您只想使用'raster'包,則可以使用該包創建您的地圖,然後使用R基本圖形中的函數'points'繪製各種尺寸的點。 – ira

2

您可以ggplot與大多數繪圖工具做到這一點,例如:

ggplot(myData,aes(x=X,y=Y)) + 
geom_point(aes(size = sqrt(Z1), alpha = .5))