简体   繁体   中英

Meaningless Y-axis values in ggplot2 dot plot

I am trying to replicate Fivethirtyeight's Tarantino movie plot . This plot is using ggplot2's dotplot . The data can be found here .

My code is following:

tara <- read.csv("tarantino.csv")
dim(tara)
names(tara)
table(tara$movie)

rd <- subset(tara, movie=="Reservoir Dogs")
du <- subset(tara, movie=="Django Unchained")
ib <- subset(tara, movie=="Inglorious Basterds") 
jb <- subset(tara, movie=="Jackie Brown")
kb1 <- subset(tara, movie=="Kill Bill: Vol. 1")
kb2 <- subset(tara, movie=="Kill Bill: Vol. 2")
pf <- subset(tara, movie=="Pulp Fiction")



plot1 <- ggplot(rd, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() + theme(legend.position="none") +
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  ggtitle(" RESERVIOR DOGS") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")
plot2 <- ggplot(pf, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" PULP FICTION") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")
plot3 <- ggplot(du, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" DJANGO UNCHAINED") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot4 <- ggplot(ib, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" INGLORIOUS BASTARDS") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot5 <- ggplot(jb, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" JACKIE BROWN") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot6 <- ggplot(kb1, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" KILL BILL: VOL 1") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot7 <- ggplot(kb2, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="none")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  ggtitle(" KILL BILL: VOL 2") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("minutes")





library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(7, 1)))
vplayout <- function(x, y)
  viewport(layout.pos.row = x, layout.pos.col = y)
print(plot1, vp = vplayout(1, 1))
print(plot2, vp = vplayout(2, 1))
print(plot3, vp = vplayout(3, 1))
print(plot4, vp = vplayout(4, 1))
print(plot5, vp = vplayout(5, 1))
print(plot6, vp = vplayout(6, 1))
print(plot7, vp = vplayout(7, 1))

The output looks like this:

在此输入图像描述

The plot has several issues:

  • Y axis is uncontrollable. I can use coord_fixed(ratio=X) ; but it's not helping much.
  • The dots are not same size. I have tried to use dotsize ; but it's not working.
  • Failed to keep background grey in the legends.

Here's an approach:

# download data
download.file('https://raw.githubusercontent.com/fivethirtyeight/data/master/tarantino/tarantino.csv', '~/Desktop/tarantino.csv', method = 'curl')

# read in data
tara <- read.csv('~/Desktop/tarantino.csv')

library(ggplot2)
library(ggthemes)     # has theme_fivethirtyeight, which will save a lot of work

ggplot(tara, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth = 1, method = "histodot") +
  theme_fivethirtyeight() + 
  ylim(0, 20) +
  ggtitle('Deaths and Swearing in Tarantino Films') +
  theme(strip.text = element_text(hjust = 0)) +     # left justify facet titles
  ylab("") +
  xlab("") + 
  # facet_wrap defaults to titles on top. The labeller capitalizes movie titles.
  facet_wrap(~movie, ncol = 1, labeller = labeller(movie = toupper))

# bin width/dot size is CRAZY SENSITIVE to aspect ratio. width 4:height 5:binwidth 1 lines up with your y-axis
ggsave('~/Desktop/Rplot.png', width = 8, height = 10, units = 'in')

What you get:

塔伦蒂诺电影的情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM