by Max Woolf
This notebook is licensed under the MIT License. If you use the code or data visualization designs contained within this notebook, it would be greatly appreciated if proper attribution is given back to this notebook and/or myself. Thanks! :)
In [1]:
options(warn = -1)
# IMPORTANT: This assumes that all packages in "Rstart.R" are installed,
# and the fonts "Source Sans Pro" and "Open Sans Condensed Bold" are installed
# via extrafont. If ggplot2 charts fail to render, you may need to change/remove the theme call.
source("Rstart.R")
sessionInfo()
Out[1]:
In [2]:
df <- read_delim("~/Downloads/omdb1215/omdbMovies.txt", "\t", col_types="iccicccccccidi_c_____")
In [3]:
print(df %>% head())
print(nrow(df))
In [4]:
df_tomatoes <- read_delim("~/Downloads/omdb1215/tomatoes.txt", "\t", col_types="i_diiiicidi_cc_c")
In [5]:
print(df_tomatoes %>% head())
print(nrow(df_tomatoes))
In [6]:
df <- df %>% left_join(df_tomatoes, by="ID")
rm(df_tomatoes)
In [7]:
print(df %>% head())
In [8]:
parseRuntime <- function(x) {
if (is.na(x) | x=="") {return (NA)}
return(strsplit(x, " ")[[1]][1])
}
parseRuntime("4 min")
Out[8]:
In [9]:
df <- df %>% mutate(Runtime = as.numeric(sapply(Runtime, parseRuntime)))
In [10]:
print(df %>% select(Runtime) %>% head())
Parse BoxOffice
the same way, but it is tricker and needs a look at the data.
In [11]:
print(df %>% select(BoxOffice) %>% filter(BoxOffice!='') %>% head())
In [12]:
parseBoxOffice <- function(x) {
unit <- 0
if (is.na(x) | x=="") {return (NA)}
if (substr(x, nchar(x), nchar(x)) == "k") {unit <- 10^3}
else {unit <- 10^6}
number <- as.numeric(substr(x,2,nchar(x)-1))
return(number * unit)
}
parseBoxOffice("$0.3M")
parseBoxOffice("$51.0k")
Out[12]:
Out[12]:
In [13]:
df <- df %>% mutate(BoxOffice = as.numeric(sapply(BoxOffice, parseBoxOffice)))
Save the processed data for later so that it can be reimported easily!
In [ ]:
write.csv(df, "movies-processed.csv", row.names=F)
In [14]:
df_box <- df %>% select(Title, Meter, BoxOffice) %>%
filter(!is.na(Meter), !is.na(BoxOffice)) %>%
arrange(desc(BoxOffice))
print(df_box %>% head())
print(nrow(df_box))
print(cor(df_box$Meter, log10(df_box$BoxOffice)))
In [15]:
plot <- ggplot(df_box, aes(x=Meter, y=BoxOffice)) +
annotate(geom="rect", xmin=60, xmax=100, ymin=min(df_box$Meter), ymax=Inf, fill="#1a1a1a", alpha=0.1) +
geom_point(alpha=0.2, stroke=0, size=2, color="#e74c3c") +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=seq(0,100, by=10), labels=paste0(seq(0,100, by=10), "%"), limits=c(0,100)) +
labs(x="Rotten Tomatoes Tomatometer Score", y="Movie Box Office Gross",
title="Scatterplot of Box Office Gross vs. RT Tomatometer") +
geom_smooth(color="#1a1a1a", method="lm")
max_save(plot, "box-office-rating-1", "IMDb and Rotten Tomatoes")
In [16]:
plot <- ggplot(df_box, aes(x=Meter, y=BoxOffice)) +
annotate(geom="rect", xmin=60, xmax=100, ymin=min(df_box$Meter), ymax=Inf, fill="#1a1a1a", alpha=0.1) +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=seq(0,100, by=10), labels=paste0(seq(0,100, by=10), "%"), limits=c(0,100)) +
stat_density2d(aes(fill= ..level..), geom="polygon", color="#e74c3c", size=0.2) +
scale_fill_gradient(low="#eeeeee", high="#e74c3c") +
labs(x="Rotten Tomatoes Tomatometer Score", y="Movie Box Office Gross",
title="Contour Map of Box Office Gross vs. RT Tomatometer")
max_save(plot, "box-office-rating-2", "IMDb and Rotten Tomatoes")
In [17]:
df_box <- df %>% select(Title, userMeter, BoxOffice) %>%
filter(!is.na(userMeter), !is.na(BoxOffice)) %>%
arrange(desc(BoxOffice))
print(df_box %>% head())
print(nrow(df_box))
print(cor(df_box$userMeter, log10(df_box$BoxOffice)))
In [18]:
plot <- ggplot(df_box, aes(x=userMeter, y=BoxOffice)) +
geom_point(alpha=0.2, stroke=0, size=2, color="#27ae60") +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=seq(0,100, by=10), labels=paste0(seq(0,100, by=10), "%"), limits=c(0,100)) +
labs(x="Rotten Tomatoes Audience Score", y="Movie Box Office Gross",
title="Scatterplot of Box Office Gross vs. RT Audience") +
geom_smooth(color="#1a1a1a", method="lm")
max_save(plot, "box-office-rating-3", "IMDb and Rotten Tomatoes")
In [19]:
plot <- ggplot(df_box, aes(x=userMeter, y=BoxOffice)) +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=seq(0,100, by=10), labels=paste0(seq(0,100, by=10), "%"), limits=c(0,100)) +
stat_density2d(aes(fill= ..level..), geom="polygon", color="#27ae60", size=0.2) +
scale_fill_gradient(low="#eeeeee", high="#27ae60") +
labs(x="Rotten Tomatoes Audience Score", y="Movie Box Office Gross",
title="Contour Map of Box Office Gross vs. RT Audience")
max_save(plot, "box-office-rating-4", "IMDb and Rotten Tomatoes")
In [20]:
df_box <- df %>% select(Title, imdbRating, BoxOffice) %>%
filter(!is.na(imdbRating), !is.na(BoxOffice)) %>%
arrange(desc(BoxOffice))
print(df_box %>% head())
print(nrow(df_box))
print(cor(df_box$imdbRating, log10(df_box$BoxOffice)))
In [21]:
plot <- ggplot(df_box, aes(x=imdbRating, y=BoxOffice)) +
geom_point(alpha=0.2, stroke=0, size=2, color="#d35400") +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=1:10, limits=c(0,10)) +
labs(x="IMDb User Rating", y="Movie Box Office Gross",
title="Scatterplot of Box Office Gross vs. IMDb User Rating") +
geom_smooth(color="#1a1a1a", method="lm")
max_save(plot, "box-office-rating-5", "IMDb and Rotten Tomatoes")
In [22]:
plot <- ggplot(df_box, aes(x=imdbRating, y=BoxOffice)) +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=1:10, limits=c(0,10)) +
stat_density2d(aes(fill= ..level..), geom="polygon", color="#d35400", size=0.2) +
scale_fill_gradient(low="#eeeeee", high="#d35400") +
labs(x="IMDb User Rating", y="Movie Box Office Gross",
title="Contour Map of Box Office Gross vs. IMDb User Rating")
max_save(plot, "box-office-rating-6", "IMDb and Rotten Tomatoes")
In [27]:
df_box <- df %>% select(Title, Metacritic, BoxOffice) %>%
filter(!is.na(Metacritic), !is.na(BoxOffice)) %>%
arrange(desc(BoxOffice))
print(df_box %>% head())
print(nrow(df_box))
print(cor(df_box$Metacritic, log10(df_box$BoxOffice)))
In [24]:
plot <- ggplot(df_box, aes(x=Metacritic, y=BoxOffice)) +
geom_point(alpha=0.2, stroke=0, size=2, color="#8e44ad") +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar) +
scale_x_continuous(breaks=seq(0,100, by=10)) +
labs(x="Metacritic Score", y="Movie Box Office Gross",
title="Scatterplot of Box Office Gross vs. Metacritic") +
geom_smooth(color="#1a1a1a", method = "lm")
max_save(plot, "box-office-rating-7", "IMDb and Rotten Tomatoes")
In [30]:
plot <- ggplot(df_box, aes(x=Metacritic, y=BoxOffice)) +
fte_theme() +
scale_y_log10(breaks=10^(3:9), labels=dollar, limits=c(10^3,10^9)) +
scale_x_continuous(breaks=seq(0,100, by=10), limits=c(0,100)) +
stat_density2d(aes(fill= ..level..), geom="polygon", color="#8e44ad", size=0.2) +
scale_fill_gradient(low="#eeeeee", high="#8e44ad") +
labs(x="Metacritic Score", y="Movie Box Office Gross",
title="Contour Map of Box Office Gross vs. Metacritic Score")
max_save(plot, "box-office-rating-8", "IMDb and Rotten Tomatoes")
Copyright (c) 2016 Max Woolf
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.