-1

I am trying to color coded my map with hex colors. Vietnam, Philippines, Indonesia: Hex - #404E57 Mongolia, Pakistan, Bangladesh, Myanmar, Sri Lanka: Hex - #A0A7AB. I want other countries to be grey and only the listed countries are color coded accordingly. What line of code should I add to do that, since I am new to ggplot map. Thank you for your time.

enter image description here

Here is my code:

ia_countries = read_csv('country_ia_3.csv') %>% 
  mutate(iso_a3 = countrycode(sourcevar = country, origin = 'country.name', destination = 'iso3c'))

ip_map = ne_countries(scale = "medium", returnclass = "sf") %>% 
 right_join(ia_countries, by = "iso_a3")

ggplot() +
  geom_sf(data = ne_countries(scale = "medium", returnclass = "sf"), fill = 'grey95', colour = 'grey40') +
  geom_sf(data = ip_map, colour = 'grey40') +
  coord_sf(xlim = c(60,140), ylim = c(-15,50)) +
  labs(x = NULL, y = NULL) +
    geom_label_repel(data = ia_countries, aes(
    x = long,
    y = lat,
    label = country
  )) +
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    legend.position = "bottom"
)
warmsoda
  • 57
  • 1
  • 9
  • Could you provide more details on where you've tried your hex code numbers? You should put them in quotations for your fills. For example: geom_sf(data = ne_countries(scale = "medium", returnclass = "sf"), fill = '#404E57', colour = '#A0A7AB') If possible, could you provide a minimal reproducible example with data that we have access to? See https://stackoverflow.com/help/minimal-reproducible-example – Alexander Christensen Feb 16 '22 at 19:21
  • @AlexanderChristensen I want other countries to be grey and only those listed countries to be color coded. Sorry I'm new to all this. – warmsoda Feb 16 '22 at 19:24
  • 2
    One possibility is to add a column to the data where you have these countries listed in a data frame and specify the colors directly. The colors that you want to be grey are grey, the others can be the hex colors. Then, when you specify `fill = COLOR_COLUMN`, you can specify the column name with the colors – Alexander Christensen Feb 16 '22 at 19:35

1 Answers1

0

You could try doing this: In line 2, in place of fill='grey95'

Write: fill=ifelse(country=='Pakistan' || country=='Mongolia' || country=='Sri..., '#A0A7AB', ifelse(country=='Indonesia'|| country..., '#404E57', 'grey95')).

Not totally certain though will work as never tried.

SavPhill
  • 636
  • 6
  • 24