0

I have been playing wrangling the data in the package "gapminder" and produced an interactive plotly plot. But geom_line() does not work. Does someone know why and how to fix this issue? The code below works well-using geom_point() but once it changed to geom_line() it no longer works and gives a blank canvas and no error message. https://rpubs.com/stepminer/829742

    library(tidyverse)   # includes ggplot2
library(plotly)      # interactive charts

# The dataset is provided in the "gapminder" library

library(gapminder)
data1 <- gapminder %>%  
 filter(country ==  "Cuba")
data2 <- gapminder %>% 
 filter(country ==  "Haiti")
data3 <- gapminder %>% 
 filter(country ==  "Jamaica")
data4 <- gapminder %>% 
 filter(country ==  "Dominican Republic")

data_all<- bind_rows(data1,data2,data3,data4)
data_ts<- data_all %>% select (-continent, -lifeExp, -pop) 
data_ts

p <- data_ts %>%
 mutate(gdpPercap=round(gdpPercap,0))%>%
 mutate(text = paste("Country: ", country, 
                     "\nGdp per capita: ", gdpPercap, sep="")) %>%
 ggplot(aes(x=year,y=gdpPercap, text=text)) +
 geom_line(aes(color=country)) +
 labs(
  title = "GDP Per capita evolution in the Caribbean ",
  subtitle = "Source:gapminder",
  x = "Year",
  y = "GDP Per Capita" )+
 theme_minimal() +
 theme(legend.position="none")

ggplotly(p, tooltip="text")
  • 1
    Haven't run your code yet, but I'd guess you need a `group` argument in your `aes` somewhere; otherwise `geom_line` may not know how to connect one point to the next – camille Nov 01 '21 at 15:43
  • @camille is right. You get a warning: `geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?` It gets resolved by adding `group = country` in the `aes`. – mihagazvoda Nov 01 '21 at 19:11

0 Answers0