res <- gtrends(
  keyword = c("corona", "virus", "pandemic", "vaccine"),
               geo = "US",
               time = "today 12-m")
res$interest_by_region %>%
  arrange(desc(hits)) %>%
  head(5)
##               location hits  keyword geo gprop
## 1           California  100   corona  US   web
## 2               Hawaii  100    virus  US   web
## 3        Massachusetts  100 pandemic  US   web
## 4           New Jersey  100  vaccine  US   web
## 5 District of Columbia   99  vaccine  US   web
res$interest_by_city %>%
  arrange(desc(hits)) %>%
  head(5)
##       location hits  keyword geo gprop
## 1       Corona  100   corona  US   web
## 2  Los Angeles  100    virus  US   web
## 3 Philadelphia  100 pandemic  US   web
## 4      Seattle  100  vaccine  US   web
## 5    San Diego   96    virus  US   web
res$interest_over_time %>%
  arrange(desc(hits)) %>%
  head(5)
##         date hits keyword geo       time gprop category
## 1 2020-06-21    9  corona  US today 12-m   web        0
## 2 2020-06-28    9  corona  US today 12-m   web        0
## 3 2020-01-19    9   virus  US today 12-m   web        0
## 4 2020-02-09    9   virus  US today 12-m   web        0
## 5 2020-03-15   82   virus  US today 12-m   web        0
res <- gtrends("pandemic",
               geo = "US",
             time = "all")
 
state <- map_data("state")

res$interest_by_region %>%
  mutate(region = tolower(location)) %>%
  filter(region %in% state$region) %>%
  select(region, hits) -> my_df
 
my_map = ggplot() +
  geom_map(data = state,
           map = state,
           aes(x = long, y = lat, map_id = region),
           fill="#ffffff", color="#ffffff", size=0.15) +
  geom_map(data = my_df,
           map = state,
           aes(fill = hits, map_id = region),
           color="#ffffff", size=0.15) +
  scale_fill_continuous(low = 'lightblue', high = 'darkred') +
  theme(panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank())
## Warning: Ignoring unknown aesthetics: x, y
my_map

class(my_map)
## [1] "gg"     "ggplot"
trace1 <- list(
  uid = "f179b7", 
  type = "choropleth", 
  zmax = 100, 
  zmin = 51, 
  z = c("100", "96", "95", "89", "89", "89", "88", "87", "86", "85", "85", "85", "84", "83", "82", "81", "81", "80", "78", "78", "77", "77", "76", "76", "75", "75", "74", "74", "74", "74", "73", "73", "73", "73", "72", "71", "70", "70", "69", "69", "68", "68", "67", "67", "66", "64", "64", "64", "63", "51"), 
  colorbar = list(title = ""), 
  colorscale = list(c(0, "rgb(220,220,220)"),list(0.2, "rgb(245,195,157)"),list(0.4, "rgb(245,160,105)"),list(1, "rgb(178,10,28)")), 
  locationmode = "USA-states", 
  locations = c("PA", "WV", "MA", "ME", "AZ", "AR", "NH", "NV", "DE", "OH", "VT", "MD", "CT", "IN", "NM", "MI", "NJ", "RI", "CO", "MT", "ND", "WI", "IA", "UT", "NC", "NV", "KY", "AL", "MS", "ID", "WA", "NY", "SD", "MN", "MO", "WY", "SC", "CA", "OR", "TN", "OK", "IL", "LA", "FL", "NE", "KS", "GA", "VA", "TX", "DC")
)
data <- list(trace1)
layout <- list(
  geo = list(scope = "usa"), 
  title = "<b>Interest in Pandemic</b><br><br>Source: Google Trends", 
  width = 800, 
  height = 500, 
  autosize = FALSE
)
p <- plot_ly()
p <- add_trace(p, uid=trace1$uid, type=trace1$type, zmax=trace1$zmax, zmin=trace1$zmin, z=trace1$z, colorbar=trace1$colorbar, colorscale=trace1$colorscale, locationmode=trace1$locationmode, locations=trace1$locations)
p <- layout(p, geo=layout$geo, title=layout$title, width=layout$width, height=layout$height, autosize=layout$autosize)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
p
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning: `marker.color` does not currently support multiple values.
res <- gtrends(
 keyword = c("corona", "virus", "pandemic", "vaccine"),
               geo = "US",
             time = "all")

plot.gtrends.silent <- function(x) {
  df <- x$interest_over_time
  df$date <- as.Date(df$date)
  df$hits <- if(typeof(df$hits) == 'character'){
    as.numeric(gsub('<','',df$hits))
    } else {
    df$hits
    }
 
  df$legend <- paste(df$keyword, " (", df$geo, ")", sep = "")
 
  p <- ggplot(df, aes_string(x = "date", y = "hits", color = "legend")) +
    geom_line() +
    xlab("Date") +
    ylab("Search hits") +
    ggtitle("Interest over time") +
    theme_bw() +
    theme(legend.title = element_blank())
 
  invisible(p)
}
 
my_plot <- plot.gtrends.silent(res)
 
my_plot +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  theme(legend.position = "right")

my_interactive_plot = ggplotly(my_plot)
my_interactive_plot