Not able to show data on choropleth map with ggplot2

Hi everyone,

I am working on some data visualisations for a project. I have very limited experience with R so any help will be greatly appreciated. I have run the code below. I have gotten a map but no colours and Co2 Emission data for the countries.


library(readr)
library(plotly)
library(ggplot2)

View(Co2Country3)

Co2_Country_Graph4=plot_geo(Co2Country3,
                            locationmode ="country", 
                            frame= ~Year)%>%
  add_trace(location = ~Country, 
            z =~Co2_Emissions,
            reversescale =T,
            color=~Co2_Emissions) %>%
  layout(geo = list(showcountries = TRUE, scope ='world'))


Co2_Country_Graph4


Co2_Country_Graph4

My data looks like this.
head(Co2Country3)

 head(Co2Country3)
# A tibble: 6 x 4
  Country        Year Co2_Emissions Codes
  <chr>         <dbl>         <dbl> <chr>
1 China          1990         2874. CHN  
2 United States  1990         5543. USA  
3 India          1990         1009. IND  
4 Russia         1990         2885. ROU  
5 Indonesia      1990         1257. IDN  
6 Brazil         1990         1642. BRA  

See the FAQ: How to do a minimal reproducible example reprex for beginners. It's difficult to reverse engineer a problem without data and the identity of the packages containing the functions being use.

Thank you for this suggestion, I have edited it!

Hello! I was able to make your code work by changing Country to Codes and location to locations. I added some dummy data so you can see the slider work.

library(tibble)
library(plotly)
library(ggplot2)

Co2_Country_Graph3 <-
  tibble::tribble(
           ~Country, ~Year, ~Co2_Emissions, ~Codes,
            "China", 1990L,          2874L,  "CHN",
    "United States", 1990L,          5543L,  "USA",
            "India", 1990L,          1009L,  "IND",
           "Russia", 1990L,          2885L,  "ROU",
        "Indonesia", 1990L,          1257L,  "IDN",
           "Brazil", 1990L,          1642L,  "BRA",
            "China", 1991L,          2267L,  "CHN",
    "United States", 1991L,          4506L,  "USA",
            "India", 1991L,          4211L,  "IND",
           "Russia", 1991L,          4742L,  "ROU",
        "Indonesia", 1991L,          2278L,  "IDN",
           "Brazil", 1991L,          4860L,  "BRA"
    )

Co2_Country_Graph4 <-
  plot_geo(Co2_Country_Graph3,
           locationmode = "country",
           frame = ~ Year) %>%
  add_trace(
    locations = ~ Codes,
    z =  ~ Co2_Emissions,
    reversescale = T,
    color =  ~ Co2_Emissions
  ) %>%
  layout(geo = list(showcountries = TRUE, scope = 'world'))

Co2_Country_Graph4
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.