Troubleshooting ggplot()/maps() Runtime Issue - r
Alright, so I'm struggling a bit in creating this map. The following code gives me this map, which is the map that I really want to use.
map(database= "world", ylim=c(15,90), xlim=c(-180,-24), fill = TRUE, projection = 'gilbert')
This is the code I used to save the map information.
map.dat <- map_data(map(database= "world", ylim=c(15,90), xlim=c(-180,-24), fill = TRUE, projection = 'gilbert'))
Now, when I run the following code, it gives me the error 'Error in eval(expr, envir, enclos) : object 'group' not found'. I'm not sure what that means.
ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region)) +
geom_polygon() +
geom_point(data = basindf, aes(x = basindf$latitude, y = basindf$longitude)) +
theme(legend.position = "none")
I had set 'group = NULL' and 'fill = NULL' and that seems to allow me to plot, but it only displays this, which is not what I want. The map is gone!
What can I do to fix this? Also, I want to move away from the points and create lines. How would I be able to make lines based on a certain id?
EDIT: Seems that some of you needed basindf to troubleshoot. I've added the first 20 lines below.
"","id","year","month","date","basin","latitude","longitude","wind speed"
"1","1902276N14266",1902,"October",1902-10-03,"EP",-93.8,14,30
"2","1902276N14266",1902,"October",1902-10-03,"EP",-94,14.5,30
"3","1902276N14266",1902,"October",1902-10-03,"EP",-94.2,15,30
"4","1902276N14266",1902,"October",1902-10-03,"EP",-94.3,15.5,30
"5","1902276N14266",1902,"October",1902-10-04,"EP",-94.4,16,30
"6","1902276N14266",1902,"October",1902-10-04,"EP",-94.5,16.5,30
"7","1902276N14266",1902,"October",1902-10-04,"EP",-94.6,17,30
"8","1902276N14266",1902,"October",1902-10-04,"EP",-94.7,17.5,30
"9","1902276N14266",1902,"October",1902-10-05,"EP",-94.8,18,30
"10","1902276N14266",1902,"October",1902-10-05,"EP",-94.9,18.5,30
"11","1902276N14266",1902,"October",1902-10-05,"NA",-94.9,18.7,35
"12","1902276N14266",1902,"October",1902-10-05,"NA",-94.7,18.8,45
"13","1902276N14266",1902,"October",1902-10-06,"NA",-94.4,18.9,55
"14","1902276N14266",1902,"October",1902-10-06,"NA",-94,19.1,60
"15","1902276N14266",1902,"October",1902-10-06,"NA",-93.7,19.3,65
"16","1902276N14266",1902,"October",1902-10-06,"NA",-93.3,19.5,75
"17","1902276N14266",1902,"October",1902-10-07,"NA",-92.9,19.7,85
"18","1902276N14266",1902,"October",1902-10-07,"NA",-92.5,20,90
"19","1902276N14266",1902,"October",1902-10-07,"NA",-92,20.3,90
"20","1902276N14266",1902,"October",1902-10-07,"NA",-91.5,20.7,90
You have two main problems.
First, the error you are getting is because you are sepecufying aes() in the ggplot() call which means that those values inherit to all layers. That means it's trying to set a group= in the geom_point layer as well but you do not have groups for that layer. You can disable the inherited aesthetics with
ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region)) +
geom_polygon() +
geom_point(data = basindf, aes(x = basindf$latitude, y = basindf$longitude), inherit.aes=FALSE) +
theme(legend.position = "none")
or you can sepecy the aes per layer
ggplot(map.dat) +
geom_polygon(aes(x=long, y=lat, group=group, fill=region)) +
geom_point(data = basindf, aes(x = basindf$latitude, y = basindf$longitude)) +
theme(legend.position = "none")
Your other problem is that you transformed your map data with a projection but not your point data.
You can transform your data with mapproj so they are both on the same scale
ggplot(map.dat) +
geom_polygon(aes(x=long, y=lat, group=group, fill=region)) +
geom_point(data = data.frame(mapproject(basindf$latitude, basindf$longitude, "gilbert")), aes(x = x, y = y)) +
theme(legend.position = "none")
This gives
The reason it was not working was because you set global aes parameters in the first call to aes, and ggplot2 was looking for group and region in the geom_points call to group and fill the points.
This technically works:
library(maps)
library(ggplot2)
ggplot() +
geom_polygon(data = map.dat, aes(x =long, y = lat, group = group, fill = region)) +
geom_point(data = basindf, aes(x = latitude, y = longitude)) +
theme(legend.position = "none")
You can see your map in the bottom right, very tiny. You want to rescale your map to lat/long, or your data to whatever you have in your map.
EDIT see the answer from #MrFlick for plot rescaling.
Related
Choropleth Map of State Population
Trying to create a choropleth map showing state population also labeling capital cities. I had two data frame initially but was not able not add ggplot 1 to ggplot 2, so I combined two data frames together, part of the table looks like this: basically trying to combines these two images together: and I've written ggplot(spr, aes(long, lat)) + borders("state") + geom_point() + coord_quickmap() +geom_label_repel(aes(label = city), size = 2) + geom_polygon(aes(long, lat, group = capital, fill = pcls),color = "grey") + coord_map("bonne", parameters=45) +ggthemes::theme_map() + scale_fill_brewer(palette = "Reds") but map looks off: i think it's the polygon part is throwing me off but not sure what to do about it.
You'll need shapefiles, or at least have the borders known to map the data to. In keeping with your question from the other day, you can still use state. scale_fill_brewer is designed for use with discrete variables. Use scale_fill_gradientn, specifying brewer.pal. Add the capitals layer in there as desired. library(ggplot2) library(usmap) library(maps) library(ggrepel) library(ggthemes) us <- map_data("state") # get the data to plot and map data to data(statepop) pops <- statepop pops$full <- tolower(pops$full) ggplot() + geom_map(data = us, map = us, aes(long, lat, map_id = region), fill = "#ffffff", color = "#ffffff", size = 0.15) + geom_map(data = pops, map = us, aes(fill = pop_2015, map_id = full), size = 0.15) + coord_map("bonne", parameters=45) + scale_fill_gradientn(colors = brewer.pal(9, "Reds")) + #adjust the number as necessary borders("state") + ggthemes::theme_map()
R ggplot function error
My name is Venus , I am a beginner of data mining and use R. Right now, I need fill my datas to map and show it with different colours to see the different level. I have install ggplot library and input my data with longtitue and latitude My code like this > ggplot(target_map, aes(long, lat, group = group, fill = Target.rate)) + + geom_polygon(colour = alpha("black", 1/2), size = 0.2) + + geom_polygon(data = needregion, colour ="white", fill = NA)+ + scale_fill_brewer(palette = "PuRd") I do this from an example, but I don't know why R always return this result: Error: Aesthetics must be either length 1 or the same as the data (19584): x, y, group, fill So, Could you give me some suggest what wrong with me? Thank you a lot
Cleaning up a map using geom_tile
Thanks to help from some users on this site, I was able to get a nice map plot for some data using geom_point. (Get boundaries to come through on states) However, now I'm trying to clean it up as I have more years to plot and want to make sure the plot is working and providing good information. After some further research, it seems the geom_tile would actually be better for this as it would shy away from points and use a gradient. The problem I'm running into is getting the code to work with geom_tile. It isn't plot anything and I'm not sure why. Here's the dataset : https://www.dropbox.com/s/0evuvrlm49ab9up/PRISM_1895_db.csv?dl=0 Here's the original code with geom_points : PRISM_1895_db <- read.csv("/.../PRISM_1895_db.csv") regions<- c("north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas", "illinois", "indiana", "wisconsin") ggplot() + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + geom_point(data = PRISM_1895_db, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA) And here is the code I've been trying, but none of the data is showing up. ggplot() + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + geom_tile(data = PRISM_1895_db, aes(x = longitude, y = latitude, fill = APPT), alpha = 0.5, color = NA) geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)
geom_tile needs your x and y values to be sampled on an regular grid. It needs to be able to tile the surface in rectangles. So your data is irregularly sampled, it's not possible to divide up the raw data into a bunch of nice tiles. One option is to use the stat_summary2d layer to divide your data into boxes and calculate the average APPT for all points in that box. This will allow you to create regular tiles. For example ggplot() + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + stat_summary2d(data=PRISM_1895_db, aes(x = longitude, y = latitude, z = APPT)) + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA) which produces you can look at other options to control this bin sizes if you like. But as you can see it's "smoothing" out the data by taking averages inside bins.
Get boundaries to come through on states
Edit 7 : After quite a bit of help, I've been able to get a map that is getting close to the results I need. But I still need to have the state boundaries come through on the map, but I can't figure it out. In order to make a reproducible example that would be appropriate I need to link to the data set since the dput is so large. To make things easy, I subset only three states, but where the boundary lines do not show up. I would like to be able to have the boundary lines come through the plot as white lines, like they are on the rest of the map. Thanks for your help. Dataset : https://www.dropbox.com/s/0evuvrlm49ab9up/PRISM_1895_db.csv?dl=0 Rep Code : PRISM_1895_db <- read.csv("PRISM_1895_db.csv") regions<- c("north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas", "illinois", "indiana", "wisconsin") ggplot() + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), col="white") + geom_point(data = PRISM_1895_db2, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) Graph :
The order in which you draw the layers matters. If you want the while lines on top, you'll need to add them last. And if you want the black shapes in the background, you need them first. So basically you need to split up the states into two draws: the background and the outline. ggplot() + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + geom_point(data = PRISM_1895_db, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) + geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA) which produces
Why does coord_map produce a weird output?
I'm trying to draw a world map using ggplot. My code is in my gist file. The output is correct when I don't use coord_map but very strange when I use coord_map : ggplot(data = test, aes(fill = x)) + geom_map(aes(map_id = id), map =world.ggmap, color = "white") + expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + theme_tufte() + coord_map() ggplot(data = test, aes(fill = x)) + geom_map(aes(map_id = id), map =world.ggmap, color = "white") + expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + theme_tufte() I've got the same error when I use data from the maps package : library(ggplot2) library(maps) world <- map_data("world") ggplot() + geom_map( data=world, aes(x=long, y=lat, group = group, map_id = region),colour="white", fill="grey10", map = world ) + coord_map() Does anyone has an answer ?
I had a similar problem before, due to longitude values outside the range [-180,180]. In your example the data do not have this problem but my trick seems to work also here. In my case I just used 'xlim' to exclude the problematic data. This solution seems to work in your case also (I used the code from your gist): map+coord_map(xlim=c(-180,180)) It produces the following map: There is still a problem with Antarctica, you can also consider clipping it too if you don't need this area: map+coord_map(xlim=c(-180,180), ylim=c(-60, 90))
Another solution is to use wrld_simpl from maptools instead, but it retains issues with Antarctica. require(maptools) require(ggmap) md <- map_data("world2") data(wrld_simpl) ggplot(wrld_simpl, aes(group = group, x = long, y = lat)) + geom_map() + coord_map()
I'm sure that is quite late but the same problem is still happening in ggplot. If you're trying to zoom-in use the following approach. ggplot()+...+xlim(c(-100, -25))+ ylim(c(-60, 20)) Good luck!