Libraries
#install.packages("prettydoc")
library(jsonlite)
library(dplyr)
library(ggplot2)
The API used for this assignment is for the most popular articles.
#most viewed articles in the past 30 days
most_popular <- fromJSON("https://api.nytimes.com/svc/mostpopular/v2/viewed/30.json?api-key=PznXIQ8GgeAC84XdXYy34uS8IGQhqqyA")
#create a dataframe that only includes the details of the most viewed articles
most_populardf <- most_popular$results
head(most_populardf)
If we look at the Section column in the dataframe we can see the most viewed section of the NY Times is the U.S. section. We also see that the most viewed subsection from this group is Politics.
#most viewed section
most_populardf %>%
group_by(section) %>%
count(subsection, sort = TRUE)
We can also look at one of the facets included in the data. It looks like it is a description facet and when we use ggplot we can see that United States Politics and Government is the top facet.
#create dataframe of the facet we want to analyze
facet1 <- as.data.frame(unlist(most_populardf$des_facet), stringsAsFactors = FALSE)
# rename column to make it easier to plot
facet1 <- facet1 %>%
rename(des = 'unlist(most_populardf$des_facet)')
head(facet1)
facet1 %>%
count(des, sort = TRUE) %>%
top_n(10, des) %>%
ggplot(aes(des,n)) +
geom_col() +
coord_flip()