0

I'm plotting some points on a map of the US like this:

gmed2 <- ggplot() 
gmed2 <- gmed2 + geom_map(data = us, map = us, aes(x = long, y = lat, 
  map_id = region), fill = "white", color = "black", alpha = 0.7) 
gmed2 <- gmed2 + geom_map(data = states_map, map = states_map, 
  aes(x = long, y = lat, map_id = region), fill = "white", color = "black")
gmed2 <- gmed2 + coord_map("perspective", 2.5, orientation = c(30, -100, 0))
gmed2 <- gmed2 + geom_point(data = meddat, aes(x = Long_Dec, y = Lat_Dec, 
  fill = Task), shape = 21, alpha = 0.5)
gmed2 <- gmed2 + scale_fill_brewer(palette = "Set1", 
  name = "Principal Mission", 
  labels = c("A", "B", "C", "D", "E", "F", "G", "H", "I"))
gmed2 <- gmed2 + theme_classic()  
gmed2 <- gmed2 + theme(legend.box.spacing = unit(-55, "pt"), 
  legend.position = "right",
  axis.line = element_blank(), axis.text.x = element_blank(),
  axis.text.y = element_blank(), axis.ticks = element_blank(), 
  legend.title = element_text(face = "bold"), 
  axis.title.x = element_blank(),
  axis.title.y = element_blank())
gmed2 <- gmed2 + theme(plot.title = element_text(size = 12, face = "bold"),
    legend.title = element_text(size = 10), 
    legend.text = element_text(size = 9))
gmed2

This works great - I get the map (with states) and the points appear colored by the factor variable Task.

I decided to add another data set on top of this one; it is shorter, but it uses the same variable names. In this case, I wanted to add their positions as open squares colored using the same (Brewer Set1) color scale. I added the following lines to the code above:

gmed2 <- gmed2 + geom_point(data = govdat, aes(x = Long_Dec, y = Lat_Dec, 
  color = Task), shape = 0, size = 3, alpha = 0.8)
gmed2 <- gmed2 + scale_color_brewer(palette = "Set1", guide = FALSE) 

I set guide = FALSE since the color scale should be the same (color and fill in this case).

The plot generates, but the colors for the two sets of data do not match (i.e., what is a red circle in the initial plot should now have a red open square around it, but it doesn't). I understand that the second set of data is more limited, so the Task variable doesn't contain all of the possible values that are in the original set.

I tried setting group = Task in both geom_ statements, but that didn't help. I also tried adding a more detailed scale_color_brewer statement that matched the scale_fill_brewer statement to no avail:

gmed2 <- gmed2 + scale_color_brewer(palette = "Set1", 
  name = "Principal Mission", 
  labels = c("A", "B", "C", "D", "E", "F", "G", "H", "I"))

What am I missing?

stefan
  • 90,330
  • 6
  • 25
  • 51
Karl Wolfschtagg
  • 425
  • 2
  • 10
  • 1
    Try `scale_fill_brewer(...., aesthetics = c("fill", "color"))` and drop `scale_color_brewer`. Using the `aesthetics=` the same scale is applied to both color and fill. – stefan Apr 04 '23 at 17:34
  • Awesome! If you submit this as an answer, I'll mark it solved. – Karl Wolfschtagg Apr 04 '23 at 18:49

0 Answers0