首页 > 解决方案 > 使用交互式文本绘图不使用 geom_line()

问题描述

我一直在争论包“gapminder”中的数据,并制作了一个交互式的情节图。但是 geom_line() 不起作用。有人知道为什么以及如何解决这个问题吗?下面的代码在使用 geom_point() 时运行良好,但一旦更改为 geom_line(),它就不再工作,并给出一个空白画布并且没有错误消息。 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")

标签: rggplot2textplotlygeom

解决方案


推荐阅读