I am new to geo-mapping and wanted to produce a hexagonal heatmap over a geographic map of Canada.
I have latiude and longitude within my original data set, and I even have a variable I wish to use to produce the "heat" of each hexagonal tile placed on the map, but I am encountering 2 problems.
1) it appears as if there is a vertical translation applied to each coordinate.
2) I do not know how to show "heat" of another variable and not show density for the heatmap.
My data is given as follows:
map<-get_map(location='Canada', zoom=3, maptype = "terrain",
source='google',color='color')
dat<-data.frame(cbind(c(47.00735, 47.36228, 47.40399, 48.08666,47.57196, 47.63742),
c(-52.95892, -53.29399, -52.79987, -52.89429, -53.27813, -52.92653),
c(86301.14, 1017390.34, 2662332.67, 473139.73, 8251899.99, 167512.52)))
names(dat)<-c('Longitude','Latitude','Rating')
Now the interesting thing is, when I run the following code, I get a hexagonal heat map with respect to density:
ggplot(dat, aes(x=Longitude,y=Latitude,color=Rating))+
stat_binhex(na.rm=TRUE)+
scale_fill_gradientn(colours=c("red","blue"),na.value=NA)
I get this output:
What I would like is a hexagonal heatmap where heat is shown by the Rating field.
Next, when I add a google map layer with the following code:
ggmap(map) +
ggplot(dat, aes(x=Longitude,y=Latitude,color=Rating))+
stat_binhex(na.rm=TRUE)+
scale_fill_gradientn(colours=c("red","blue"),na.value=NA)
I get the following error: Error: Don't know how to add o to a plot
If anyone know hows to remedy these issues, it would be greatly appreciated. Thank you!
Related
I have drawn an imaginary map for fun, and I thought it would be even more fun to make a DEM for the imaginary country using R, just to add to the visualization. :)
Basically, I now have a png with the outline of the country. Just only with a black border, the rest is left white.
My plan was to create a raster from it, so I could eventually add values to every pixel in the map. Since it's a imaginary map, it doesn't have a coordinate system. I just set the extent as it is as png:
drawing <- raster('drawing.png')
extent(drawing) <- c(0, 388, 0, 348)
Then I create a dataframe with the points of which eventually the DEM is calculated from. Basically, I just randomly select pixel locations within the country (name them x and y) and assign a random value as height. For this I use a simple excel sheet:
samples <- read_excel('samples.xlsx')
coordinates(samples) <- ~x+y
spatlocations <- SpatialPoints(samples)
vectlocations <- vect(samples)
Then, I thought, I could create a map using Inverse Distance Weighting (IDW), just to get a nice smooth gradient looking map. So it would possibly something like this:
result <- idw(unsampled, samples)
However, I fail to do this. I for example don't really know how to exactly work with the raster I created from the png, and the same for the sample points.
As you maybe can see, I am what you could consider a beginner :).
Is this possible at all? Is this methodology correct? How can I achieve such a map? Thanks in advance!
I'm using the map_data('world2') data in ggplot2 with geom_map to plot some data, but I've run into a bit of an issue. I've divided up my regions into 3 longitude ranges (40E-140E, 140E-100W,100W-40E). As you can see, it doesn't fit neatly either into the (180W,180E) or (0,360) longitude ranges of the two world maps, so when I try to plot the data with a longitude axis that has a range encompassing the data, I get a blank portion of map on the right.
Is there a way to plot an extra slice of the map (i.e. 0->40E if using 'world2') adjacent to longitude 359? I tried manually adjusting the longitude values by doing the following:
m<-map_data('world2')
m$long<-ifelse((m$long>=0 & m$long<=40),m$long+360,m$long)
ggplot(data=longdata)+
coord_fixed(xlim=c(40,400),ylim=c(-90,90)) +
geom_map(data= m, map = m, aes(map_id=region))
However, the resulting map is an ungodly mess, likely because the polygons are now all screwed up. Any thoughts?
AHA!
I just needed to modify m to the following:
m<-map_data('world2',wrap=c(40,400))
I'm trying to create a good heat map using Krigging for missing values.
I have the following data, that contains all the values that have been measured for RLevel.
I followed the following link that tells how to use krigging. https://rpubs.com/nabilabd/118172
This is the following code I wrote. Before these steps, I had removed all the values from my DieData that
needed values to be tested. The values that need to be tested are refered as die.data.NAValues in my code.
#**************************************************CODE*****************
#Step3: Convert to SpatialPointsDataFrame Object
coordinates(die.data) = ~X+Y
#Step 4: Get the prediction Grid
coordinates(die.data.NAValues)=~X+Y
#Using autokride method
kr = autoKrige(RLevel, die.data, die.data.NAValues,nmax=20)
predicted_die_values <- kr$krige_output
predicted_die_model <- kr$var_model
#Get Predictions. Plot the predicted on heat map.
g <- gstat(NULL,"RLevel",RLevel~1,die.data, model=predicted_die_model,nmax=1)
predictedSet <- predict(g,newdata=die.data,BLUE=TRUE)
#Plot the krigging graph
predicted_die_values %>% as.data.frame %>% ggplot(aes(x=X,y=Y)) + geom_tile(aes(fill=v1.pred))+coord_equal() +scale_fill_gradient(low="yellow",high="red")+scale_x_continuous()+scale_y_continuous()+theme_bw()
When I plot the graph, I get the following image from the values that have been tested by the KRIGING METHOD.
My question is how can I show a good heat map with predicted points from KRIG and from the points already have. I want my graph to show something like this from the link above I had posted.
Description about my dataset: My original dataset including NA values that have not been tested contains around 55057 points. When I take out NA values and use that are my prediction grid, I get 390 points. Majority of the values for RLevel are within 30's range except around 100-200 points are above 100.
Can anyone help me out or give me guidance of how to produce a good heatmap?
I have a set of coordinates X and Y for my points and used the deldir to create determine and plot the Voronoi Polygons. (I've used this tutorial here)
This is my plot: (sorry that its so small, but you get the idea).
I need to determine the area of each polygon. How can I do that?
I looked up in the deldirpackage page and couldnt find anything related to the Voronoi polygons, only about other
Based on the reference manual (https://cran.r-project.org/web/packages/deldir/index.html), the output of the deldir function is a list. One of the list element, summary, is a data frame, which contains a column called dir.area. This is the the area of the Dirichlet tile surrounding the point, which could be what you are looking for.
Below I am using the example from the reference manual. Use $ to access the summary data frame.
library(deldir)
x <- c(2.3,3.0,7.0,1.0,3.0,8.0)
y <- c(2.3,3.0,2.0,5.0,8.0,9.0)
dxy1 <- deldir(x,y)
dxy1$summary
I have a matrix, say cmat <- matrix(c(0,0,1,0,1,1,0,1,1),3,3) and I would like to plot the exact contours of the "region" with containing ones.
When using a contourplot(cmat) what I get is lines that define a sort of "smoothed" contour rather than following exactly the tiles. What I would like is a graph made only out of vertical and horizontal lines.
Does anyone know a function performing that?
Thanks in advance,
Renzu