PMassicotte / gtrendsR

R functions to perform and display Google Trends queries
355 stars 112 forks source link

plot() doesn't show line plot any more #263

Closed agilebean closed 6 years ago

agilebean commented 6 years ago

For the last few weeks, the plot function does not show the expected result any more. Is this related to Google API changes? Would be grateful for a fix!

social.life <-  gtrends(c("talk", "conversation", 
                          "smartphone overuse"))
social.life %>% plot

renders image

instead of a line plot.

interesting: for some reason, this seems to depend on the search terms! this works:

basketball <- gtrends(c("nhl", "nba", "nfl"))
basketball %>% plot

gives - as expected - this line plot: image

PMassicotte commented 6 years ago

Looking at your first graph, it seems that Y values are factors or strings.

agilebean commented 6 years ago

why would anything different happen if i pass the search terms exactly in the same fashion by c("a","b","c")?

So I have a different hunch for the root cause: If a search term has average hits around 1, the plot() function switches to horizontal line plot. This must be a condition which could be changed, can't it?

PMassicotte commented 6 years ago

No, it is because the returned data was parsed as text instead of numeric because of the <1 value. You can convert the vector to numeric and plot it again.

agilebean commented 6 years ago

That makes a lot of sense. Thanks for the tip! Any chance this conversion will be integrated into the plot function?

I will figure it out, but so far, this try didn't work:

social.life <- gtrends(c("talk", "conversation", "smartphone")) %T>% plot
social.life$hits <- social.life %>% .[[1]] %>% .$hits %>% 
    lapply(function(x) if (x=="<1") x <- 0 else x) %>% 
    as.numeric()
PMassicotte commented 6 years ago
library(gtrendsR)

social.life <-  gtrends(c("talk", "conversation",
                          "smartphone overuse"))
#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

#> Warning in is.na(object): is.na() applied to non-(list or vector) of type
#> 'NULL'

social.life$interest_over_time$hits <- as.numeric(social.life$interest_over_time$hits)
#> Warning: NAs introduced by coercion

plot(social.life)
#> Warning: Removed 2 rows containing missing values (geom_path).

Created on 2018-03-12 by the reprex package (v0.2.0).

agilebean commented 6 years ago

Ah - the $hits was within the interest_over_time object... Thanks a lot!!