2

This very particular question has been driving me crazy! I have a ggplot dot plot that I'm trying to recreate from this website: https://fivethirtyeight.com/features/complete-catalog-curses-deaths-quentin-tarantino-films/

I've recreated everything except for the annotations. I know that I have to create a data frame in order to annotate on multiple facets, but dot plots don't have a variable y-axis so the following attempt shows an error (I've provided all of my code since it's fairly short):

library(fivethirtyeight) library(ggplot2) library(dplyr) library(grid) library(tidyr) head(fivethirtyeight::tarantino)

movie <- c("Reservoir Dogs", "Pulp Fiction", "Jackie Brown", "Kill Bill: Vol. 1", "Kill Bill: Vol. 2", "Inglorious Basterds", "Django Unchained")
start_year <- c(1992,1994,1997,2003,2004,2009,2012)
year_info <- data.frame(movie,start_year)
tarantino_df <-fivethirtyeight::tarantino %>%
  mutate(profane, profane = ifelse(profane == T, "Profanity", "Death"))%>%
  left_join(year_info)%>%
  unite(movie, movie, start_year, sep = ", ")

tarantino_df$movie <- factor(tarantino_df$movie, levels = c("Reservoir Dogs, 1992", "Pulp Fiction, 1994", "Jackie Brown, 1997", "Kill Bill: Vol. 1, 2003", "Kill Bill: Vol. 2, 2004", "Inglorious Basterds, 2009", "Django Unchained, 2012"))
tail(tarantino_df,100)
ann_text<-data.frame(minutes_in=c(25,15),movie=c("Reservoir Dogs, 1992", "Pulp Fiction, 1994"),label=c("Label 1","Label 2"))
tarantino_plot <- ggplot(tarantino_df, aes(x = minutes_in, fill = profane)) + 
      geom_dotplot(binwidth = 1, method = "dotdensity", stackratio = 1.2, aes(color = profane), dotsize = 0.5,      stackgroups = TRUE, binpositions="all", label.select = list(top.up = 10, top.down = 4)) + 
      facet_wrap(~movie, ncol = 1, shrink = TRUE) + 
      scale_color_manual(values =  c("red", "black"))+
      scale_fill_manual(values =  c("red", "black"))+ 
      guides(color = guide_legend(reverse = TRUE))+
      guides(fill = guide_legend(reverse = TRUE))+
      theme(legend.position=c(0.05,1.1), legend.title =element_blank(), plot.margin=unit(c(1.5,1,1.5,1.2),"cm"))+
      scale_y_continuous(breaks=c(0, 20), limits = c(0, 20))+ 
      scale_x_continuous(expand = c(0, 0), limits = c(0, 165))+
      theme(panel.spacing = unit(2, "lines"))+
      theme(strip.text = element_text(hjust = 0),strip.text.x = element_text(size = 10))+
      labs(title = "The complete obscene guide to Tarantino", subtitle = "Time stamp of every instance of profanity and each death in feature films directed by Quentin Tarantino\n\n\n\n\n", x = NULL, y = NULL)+
      geom_text(data = ann_text,label=ann_text$label)

tarantino_plot

This is the error that I get:

Error in FUN(X[[i]], ...) : object 'profane' not found
Clare C
  • 21
  • 2
  • Welcome to Stack Overflow! You should provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). To be specific, we need to get a subset of your of your `tarantino_df` which reproduces the error. When sharing that, please use `dput(subset_of_df)` so the structure of the dataset is also shared. – M-- Oct 15 '20 at 15:35
  • The speific error arises because you mapped `profane` on fill which is not in your df. But your right. Even fixing this will not solve your issue as there is no variable to be mapped on y. But maybe https://stackoverflow.com/questions/44991607/how-do-i-label-the-dots-of-a-geom-dotplot-in-ggplot2 helps with that. – stefan Oct 15 '20 at 16:25

0 Answers0