'Is it possible to scrape data from an interactive graph?
Is there an R code I could use to extract the data from 'https://www.politico.eu/europe-poll-of-polls/austria/' specifically for the "Freedom Party of Austria"?
Solution 1:[1]
The graph is constructed from data transmitted to your browser in json format from an XHR call. Using developer tools in your browser, you can find the url of the json and read it directly.
The url you need is:
url <- "https://www.politico.eu/wp-json/politico/v1/poll-of-polls/AT-parliament"
You get a wealth of data from this json, including several tables that might be of interest. They can be put in an R list with:
all_data <- jsonlite::fromJSON(url)
The data you want can be extracted and put into a tidy data frame like this:
df <- all_data$trends$kalman
df <- cbind(df[1], df$parties)
df <- tidyr::pivot_longer(df, -1)
df$date <- as.Date(df$date)
head(df)
#> # A tibble: 6 x 3
#> date name value
#> <date> <chr> <dbl>
#> 1 2013-09-29 SPOE 26.8
#> 2 2013-09-29 OEVP 24.0
#> 3 2013-09-29 FPOE 20.5
#> 4 2013-09-29 GRUENE 12.4
#> 5 2013-09-29 TS 5.73
#> 6 2013-09-29 NEOS 4.96
And to show this is what we want, let's plot the various parties over time:
library(ggplot2)
ggplot(df, aes(date, value, color = name)) +
geom_line() +
theme_bw()
#> Warning: Removed 6634 row(s) containing missing values (geom_path).

Created on 2022-03-08 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |
