Complex Maps in R Using ggplot2 -
I recently need to create a map using United statistical areas for America - how can I share this I want to share it. This applies only if someone wants to use a zip code or other division (county, congressional districts, etc.)
1) You need to get shaped shapes by any program to make a map - they basically define polygons that make up shapes (state, zip, CSA, etc.). Census Bureau in America
Provides those people (I'm sorry, but they are so big that I can not provide data for an example) . Open them and keep them in the directory of your choice. Make sure that you set it as a work directory using setwd ()
2) In the package maptools, use the ReadShapeSpatial files in R Need to import into the spatial dataframe object. In my case I read 2 files, the state to attract the metro areas and the state to attract the CSA file. For simplicity, I created an ID field in each, it is not necessary. Please note rgeos as well, tries to use gpoclib by default maptools but there are some licensing issues, rgeos are wiping the deer those would require
(Maptools) is required (rgeos) cbsa_bdry = readShapeSpatial ("tl_2013_us_csa.shp") cbsa_bdry @ data $ id & lt; - as.character (cbsa_bdry @ data $ geoed) state_bdry = readShapeSpatial ("tl_2013_us_state.shp") state_bdry @data $ id & lt; - as.character (state_bdry @ data $ STUSPS) 3) The file must be firm to use in the giglot - as far as I can tell that it is creating datafom to plot from the spatial data set. I do not understand this but it is needed. Here you use the ID field - in my case state and the csa number cbsa_map_f
Fortified files at this point can be mapped using ggplot, although generally you want to have some other data To be mapped, for example, a variable of interest that defines the color size (it can be said that the party which governs the governor's mansion, or average income, or whatever directs your analysis)
4) I would like to spatial dataframe for this example Receiving portion will merge back, I am in this case I do not merge any other data. Please note, I have learned in a rigorous way that due to sorting using merge the data could be bad, if you use a merge then make sure that the data has not been resolved as part of it, I'm using to connect with that reason.
is required (plyr) state_map_df & lt; -join (state_map_f, state_bdry @data, = "id") state_map_df $ state & lt; 5) Are your charts - you need to attract layers in order, the layer below can obscure the first, then the second, or the other, to simplify the simplicity. I'm just drawing Texas for tx & lt; -subset (cbsa_map_df, state == "Texas") TX1 & lt; - Subset (state_map_df, state == "Texas") is required (ggplot2) (mapproj) sample & lt; - ggplot (zero) + geom_polygon (data = TX1, AES (long, lat, group = group), color = "blue", filling = "light brown") + coord_map (projection = "mercator" ) + Theme_bw () Sample & lt; - sample + geom_polygon (data = tx, aes (long, lat, filling = name, group = group), color = "black") + theme (legend.position = "bottom", axis.text.x = element_blank () , Axis.text.y = element_blank (), axis.ticks = element_blank ()) sample & lt; - Sample + guide (filling = guide_legend (title = "MSA name", Nrow = 3, title.position = "top", title.hjust = 0.5, title.theme = element_text (face = "bold", angle = 0) )) Sample & lt; - Sample + scale_x_continuous ("") + scale_y_continuous ("") sample
I'm not passing datafrom to call the initial gplot to open the chart because for each level data Uses a different dataframe. If I had merged the data to use on a constant scale, I would use that for the CAS portion at fill =
Comments
Post a Comment