title: Creating colour gradients based on observations in a ggplot2 world map
body: <p>I have code as follows, that creates a world map, where the countries light up green when they are present in the data.</p>
  <pre><code># List of countries
  library(countrycode)
  library(ggplot2)
  library(rvest)
  library(data.table)
  library(dplyr)
  library(tidyverse)
  
  # Loads world map
  worldmap &lt;- map_data(&quot;world&quot;)
  # Vector of countries in the European Union
  European_Union &lt;- c(&quot;Austria&quot;, &quot;Belgium&quot;, &quot;Bulgaria&quot;, &quot;Croatia&quot;, &quot;Republic of Cyprus&quot;, &quot;Czech Republic&quot;, &quot;Denmark&quot;, &quot;Estonia&quot;, &quot;Finland&quot;, &quot;France&quot;, &quot;Germany&quot;, &quot;Greece&quot;, &quot;Hungary&quot;, &quot;Ireland&quot;, &quot;Italy&quot;, &quot;Latvia&quot;, &quot;Lithuania&quot;, &quot;Luxembourg&quot;, &quot;Malta&quot;, &quot;Netherlands&quot;, &quot;Poland&quot;, &quot;Portugal&quot;, &quot;Romania&quot;, &quot;Slovakia&quot;, &quot;Slovenia&quot;, &quot;Spain&quot;, &quot;Sweden&quot;)
  # Unique world map for every data source
  worldmap_AMIS_policy &lt;- worldmap
  url &lt;- 'http://statistics.amis-outlook.org/policy/doc/query_download/Bulk_AllData.zip'
  download.file(url, &quot;AMIS_policy.zip&quot;)
  unzip(&quot;AMIS_policy.zip&quot;)
  AMIS_policy_dat &lt;- read.csv(&quot;M:/My Documents/Bulk_AllData/Bulk_AllData_policy.csv&quot;)
  # Keep unique country names
  vec_AMIS_policy &lt;- unique(AMIS_policy_dat$Country_Name)
  
  # Add all EU countries if EU is available
  if (&quot;EU&quot; %in% vec_AMIS_policy | &quot;European Union&quot; %in% vec_AMIS_policy) {
      vec_AMIS_policy &lt;- append(vec_AMIS_policy, European_Union)
  }
  
  # Set colors
  #&gt; Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Ascension Island, Azores, Barbuda, Canary Islands, Chagos Archipelago, Grenadines, Heard Island, Madeira Islands, Micronesia, Saba, Saint Martin, Siachen Glacier, Sint Eustatius, Virgin Islands
  worldmap_AMIS_policy &lt;- mutate(worldmap_AMIS_policy, region = countryname(region), fill = ifelse(region %in% countryname(vec_AMIS_policy), &quot;green&quot;, &quot;lightgrey&quot;))
  
  # Use scale_fiil_identity to set correct colors
  ggplot(worldmap_AMIS_policy, aes(long, lat, fill = fill, group=group)) + 
    geom_polygon(colour=&quot;gray&quot;) + ggtitle(&quot;Map of World&quot;) + 
    ggtitle(&quot;Availability of AMIS Policy Data - Monthly&quot;) +
    scale_fill_identity()
  </code></pre>
  <p><a href="https://i.stack.imgur.com/qdiF4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qdiF4.png" alt="enter image description here" /></a></p>
  <p>For the next step, I would like to use the observation per country to make gradations in the colouring (many observations darker and lower amount of observations lighter)</p>
  <pre><code># Count the number of observations by country
  setDT(AMIS_policy_dat)[PolicyType_Name==&quot;Export measures&quot;, if_1_is_export:=1]
  setDT(AMIS_policy_dat)[PolicyType_Name==&quot;Import measures&quot;, if_1_is_export:=0]
  policy_count &lt;- setDT(AMIS_policy_dat)[, .(count = .N, var = sum(if_1_is_export)), by = Country_Name]
  </code></pre>
  <p>To get what I want, I think I need to use <code>geom_count</code>, like in <a href="https://stackoverflow.com/questions/45883097/color-points-by-their-occurrence-count-in-ggplot2-geom-count">this link</a>.</p>
  <p>The problem is that I am not sure how to combine this <code>+ geom_count(aes(color = ..n..))</code> code with the ggplot code I already have.</p>
  <p>Could anyone show me where to start?</p>