3

I'd like to make a map where the states are colored according to one criteria, and crosshatched (or somehow otherwise differentiated) according to a different criteria. So far what I have is

library(ggmap)
library(mapdata)
library(ggplot2)
library(dplyr)
region<-c("california","nevada","oregon","washington")
var1<-c(0,1,0,1)
var2<-c(3,4,4,3)
my_data<-data.frame(region, var1, var2)
all_states_map <- map_data("state")
Total <- inner_join(all_states_map,my_data, by = "region")
Total$var1<-as.factor(Total$var1)
my_map <- ggplot() + geom_polygon(data=Total, aes(x=long, y=lat, group = Total$group, fill=Total$var1),colour="white",
                         show.legend=TRUE) + scale_fill_manual(values=c("blue","gray"))

So now my states are color coded based on whether var1 is 0 or 1. Is there a way to overlay something on top of that to crosshatch states where var2 is 3, and leave alone the states where var2 is 4?

Edit: what I'd like is something like this:my map

user5457414
  • 137
  • 3
  • 7
  • You might have to do something like this example: http://stackoverflow.com/questions/21677489/fill-geospatial-polygons-with-pattern-r – Mike H. May 08 '16 at 21:42

3 Answers3

2

Is this something close to what you'd like to see?

ggplot() + geom_polygon(data=Total, aes(x=long, y=lat, group = group, 
                        fill = paste(var1, var2, sep = ", ")), colour="white") + 
           scale_fill_manual(values=c("blue","gray", "green", "red")) + 
           labs(fill = "(var1, var2)")

enter image description here

Another option is you set the transparency as another dimension of the plot, so it would be like, which might be closer to what you want?

ggplot() + geom_polygon(data=Total, aes(x=long, y=lat, group = group, 
                        fill = var1, alpha = as.factor(var2)), colour="white") + 
           scale_fill_manual(values=c("blue","red")) +
           scale_alpha_manual(values = c(0.5, 1)) + 
           labs(alpha = "var2", fill = "var1") 

enter image description here

Psidom
  • 209,562
  • 33
  • 339
  • 356
  • I might end up needing to do something like that, but it's not exactly what I want. I edited the question with an example. I'm looking for a way to have the two different things look independent. – user5457414 May 08 '16 at 20:55
2

This might not be ideal but maybe helps in some way?

library(ggmap)
library(mapdata)
library(ggplot2)
library(dplyr)
library(sp)
region<-c("california","nevada","oregon","washington")
var1<-c(0,1,0,1)
var2<-c(3,4,4,3)
my_data<-data.frame(region, var1, var2)
all_states_map <- map_data("state")
Total <- inner_join(all_states_map,my_data, by = "region")
Total$var1<-as.factor(Total$var1)

randomPoints <- NULL

# For each region find a set of points inside each state
for(i in region){
  sub <- subset(Total, region == i)
  sub.sr = SpatialPolygons(list(Polygons(list(Polygon(sub[,1:2])), "a")))

  smple <- as.data.frame(spsample(sub.sr, n = 100, "regular"))

  temp <- data.frame(x = smple[,1], y = smple[,2], region = i, var1 = unique(sub$var1), var2 = unique(sub$var2))
  randomPoints <- rbind(randomPoints, temp)
}

ggplot() + 
  geom_polygon(data=Total, aes(x=long, y=lat, group = group, fill=var1), color = "white") + 
  geom_point(data = randomPoints, aes(x = x, y = y, shape = as.factor(var2)), size = 1) + 
  scale_shape_manual(values = c(1, 2))

enter image description here

royr2
  • 2,239
  • 15
  • 20
1

It looks like there is no easy solution to this problem in ggplot. One way of differentiating could be to use different contours around the polylines like in this example:

ggplot() + 
  geom_polygon(data=Total, aes(x=long, y=lat, group = Total$group, 
                              fill=Total$var1,colour=as.factor(var2)),
                              ,size=3,show.legend=TRUE) + 
  scale_fill_manual(values=c("blue","gray"))+
  scale_color_manual(values=c("black",NA))

contour example1

But as you can see this results in an inconsistent line width between California and the other states because of the polygon plotting order. We can fix this in a quick and dirty way by increasing the groupnumber of the states where var2 equals 3:

Total2<-Total%>%
  mutate(group=ifelse(var2==3,group+100,group))

ggplot() + 
  geom_polygon(data=Total2, aes(x=long, y=lat, group = Total2$group, 
                               fill=Total2$var1,colour=as.factor(Total2$var2)),
               ,size=3,show.legend=TRUE) + 
  scale_fill_manual(values=c("blue","gray"))+
  scale_color_manual(values=c("black",NA))

contour example2

If you really want to go for the crosshatching you may have to abandon ggplot for this one:

Fill Geospatial polygons with pattern - R

Community
  • 1
  • 1
Jeroen Boeye
  • 580
  • 4
  • 18