2016-02-05 5 views
1

Я пытаюсь реплицировать Fivethirtyeight's Tarantino movie plot. Этот график использует ggplot2's dotplot. Данные можно найти here.Значения оси Y по оси ggplot2

Мой код выглядит следующим образом:

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)) 

Результат выглядит следующим образом:

enter image description here

Участок имеет несколько вопросов:

  • Y оси неуправляем. Я могу использовать coord_fixed(ratio=X); но это не очень помогает.
  • Точки не одного размера. Я попытался использовать dotsize; но он не работает.
  • Не удалось сохранить фон grey в легендах.
+1

Ваш код не очень DRY. Используйте 'facet_grid', и ggplot будет поддерживать масштабирование. Однако у него есть другой взгляд, который вам может понадобиться, чтобы заставить вас входить в то, что вы хотите. – alistaire

+0

@alistaire, что означает «СУХИЕ». Он очень похож на мой код: P Согласитесь с вами о 'facet_grid'. – Mist

+0

Что вы имеете в виду с неуправляемой осью y? Вы сами устанавливаете лимиты ... – Heroka

ответ

3

Вот подход:

# 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') 

Что вы получите:

dotplot of Tarantino films

+0

Спасибо. При попытке репликации у меня появляется следующее сообщение об ошибке: «Ошибка в FUN (X [[i]], ...): Тема« текст »имеет свойство NULL: margin, debug Кроме того: Предупреждающее сообщение: 'axis .ticks.margin 'устарел. Пожалуйста, установите вместо свойства 'margin' значение 'axis.text'. Я обновил пакет 'ggthemes'; но я получаю ту же ошибку. –

+0

Нечетный. Какую версию 'ggplot2' вы используете? В версии 2.0.0 появились существенные изменения, которые могут возникнуть в декабре. – alistaire

+0

Из 'sessionInfo()': 'R version 3.2.3 (2015-12-10) Платформа: x86_64-apple-darwin13.4.0 (64-разрядная версия) Выполняется под: OS X 10.11 (El Capitan)' и ' ggplot2_2.0.0' и 'ggthemes_3.0.1'. –

Смежные вопросы