banner



How To Draw A Lake On A Map

In 1 recent project I needed to describe several maps and visualize different kinds of geographical information on it. I establish the combination of R/ggplot/maps bundle extremely flexible and powerful, and produce nice looking map based visualizations.

Hither is a short tutorial,monospace font indicates the lawmaking you lot need to run in R. Y'all probably need some basic agreement of R to piece of work through this tutorial.

  • Install R. Download from http://cran.r-projection.org/
  • Install required R packages: maps and ggplot2.

install.packages("maps")
install.packages("ggplot2")

  • Now import the libraries, load the US map information, and draw a map with all states.


library(ggplot2)
library(maps)
#load us map data
all_states <- map_data("state")
#plot all states with ggplot
p <- ggplot()
p <- p + geom_polygon( information=all_states, aes(x=long, y=lat, group = group),colour="white", fill="grey10" )
p

  • If y'all merely desire a subset of states, subset the all_states dataframe and redraw the plot.


states <- subset(all_states, region %in% c( "illinois", "indiana", "iowa", "kentucky", "michigan", "minnesota","missouri", "north dakota", "ohio", "southward dakota", "wisconsin" ) )
p <- ggplot()
p <- p + geom_polygon( data=states, aes(x=long, y=lat, group = group),colour="white", fill="grey10" )
p

  • Prepare a geographical dataset, which contains the data y'all desire to visualize onto the map. Bespeak the geographical location of each data signal in terms of longitude and latitude. As an example, download this file and save it as "geo.csv" into your working directory, and load information technology into R.


mydata <- read.csv("geo.csv", header=TRUE, row.names=i, sep=",")

  • In the dataset, there are 39 universities in the midwest region, we desire to visualize all these schools on the map, put a label on some of the schools, and we want to make the size of each dot proportional to the full number of enrollment in the school, and we want a fable, with the name "Total enrollment". Sounds like a bit complicated, huh? Merely it's just two lines of more code.


p <- ggplot()
p <- p + geom_polygon( data=states, aes(x=long, y=lat, group = group),colour="white" )
p <- p + geom_point( data=mydata, aes(ten=long, y=lat, size = enrollment), color="coral1") + scale_size(name="Total enrollment")
p <- p + geom_text( data=mydata, hjust=0.five, vjust=-0.five, aes(x=long, y=lat, label=characterization), colour="gold2", size=iv )
p

For those of you who knows R but are not familiar with ggplot, the catch is the size=enrollment pick in the geom_point function. This choice sets the size of the dots proportional to the number of enrollment in the dataset.

  • Now we can run across that at that place are so many schools in Chicago that they actually overlapped with each other, we desire to jitter the dots a fleck so we tin can see them ameliorate. And then I changed geom_point role to geom_jitter and used position pick to command the magnitude of jittering.


p <- ggplot()
p <- p + geom_polygon( information=states, aes(ten=long, y=lat, group = group),colour="white" )
p <- p + geom_jitter( data=mydata, position=position_jitter(width=0.v, height=0.five), aes(10=long, y=lat, size = enrollment, color="coral1")) + scale_size(name="Full enrollment")
p <- p + geom_text( data=mydata, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, label=label), colour="gold2", size=4 )
p

Some schools are jittered so much that they are in the lake now, but….. you get my point.

So what if yous want to change the colors of schools to bespeak some other factors, such equally What state are they in?

  • Use coloroption to change the colors of the dots.


p <- ggplot()
p <- p + geom_polygon( data=states, aes(x=long, y=lat, group = grouping),colour="white" )
p <- p + geom_jitter( information=mydata, position=position_jitter(width=0.v, superlative=0.v), aes(10=long, y=lat, size = enrollment,colour=state)) + scale_size(proper name="Total enrollment")
p <- p + geom_text( data=mydata, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, characterization=label), color="gold2", size=iv )
p


Now you can run across a really colorful map… if y'all don't similar the colors, you can change the color scale using the scale_color_brewer function, or manually choose the colour using scale_colour_manual function in ggplot.

So this is just a very unproblematic illustration of the incredible ability and flexibility of ggplot2 using maps as an example. While the grammar of ggplot2 seems a bit mysterious the first time you run into information technology, it is actually built on the then called "grammar of graphics". It basically allows yous to build your own plot layer by layer, with accented control of each single element in the plot.

For professional plotting, I highly recommendggplot2.  http://had.co.nz/ggplot2/

Source: https://uchicagoconsulting.wordpress.com/2011/04/18/how-to-draw-good-looking-maps-in-r/

Posted by: davisbrounally.blogspot.com

0 Response to "How To Draw A Lake On A Map"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel