0
votes

When I insert scale_fill_brewer(palette="Set1") before a call to geom_point(), plotting with ggplotly results in the legend keys appearing inside parentheses (see included image of the ggplotly plot).

Executing gp (=ggplot(...)) displays the legend keys correctly, but the scale_fill_brewer colors appear off.

I have tried some other variations, but cannot make ggplotly display just the keys.

I appreciate specific help with this issue, but also I would also like to learn an efficient way of mastering the experimental behavior of ggplot2 and plotly.

library(tidyverse)
library(ggplot2)
library(plotly)
library(RColorBrewer)

gp<-ggplot(IMDB)+
  aes(x=runtime,y=rating, size=votes,fill=genre,
      label0="-------",label1=rank, label2=title, label4=runtime, label5=genre, label6=rating, label7=metascore, label8=votes, label9=gross, label10=director, label11=actor)+
  labs(title="Point Plot of the Top 100 Films of 2016\nsized by votes and color filled by genre")+
  theme(plot.title = element_text(hjust=0,size=10))+
  guides(size=FALSE)+ # supress legend for size/votes
  scale_fill_brewer(palette = "Set1")+
geom_point()+
           scale_color_brewer(palette = "Set1")+
  geom_rug(inherit.aes = FALSE,
           data=IMDB,
           aes(x=runtime,y=rating,color=genre))
ggplotly(gp)
# gp # ggplotting directly displays legend keys correctly, but the fill colors are off. Rug colors appear correctly.

enter image description here

Output of dput(IMDB):

## structure(list(runtime = c(123L, 128L, 114L, 117L, 76L, 151L, 
## 108L, 133L, 107L, 147L, 139L, 108L, 111L, 106L, 116L, 144L, 115L, 
## 132L, 116L, 132L, 82L, 118L, 102L, 123L, 109L, 145L, 110L, 103L, 
## 100L, 161L, 106L, 116L, 107L, 134L, 116L, 122L, 106L, 118L, 82L, 
## 86L, 88L, 118L, 114L, 116L, 117L, 146L, 127L, 96L, 96L, 97L, 
## 141L, 108L, 137L, 92L, 106L, 144L, 127L, 89L, 123L, 86L, 102L, 
## 118L, 113L, 112L, 127L, 94L, 161L, 100L, 128L, 130L, 98L, 88L, 
## 89L, 99L, 129L, 97L, 113L, 120L, 121L, 87L, 120L, 120L, 102L, 
## 108L, 81L, 103L, 132L, 124L, 97L, 106L, 111L, 96L, 99L, 118L, 
## 111L, 104L, 97L, 111L, 107L, 107L), title = c("Suicide Squad", 
## "La La Land", "War Dogs", "Split", "Batman: The Killing Joke", 
## "Batman v Superman: Dawn of Justice", "Deadpool", "Rogue One: A Star Wars Story", 
## "Moana", "Captain America: Civil War", "Hacksaw Ridge", "Sing", 
## "Moonlight", "Me Before You", "Arrival", "X-Men: Apocalypse", 
## "Doctor Strange", "Fantastic Beasts and Where to Find Them", 
## "Ghostbusters", "The Magnificent Seven", "Terrifier", "Lion", 
## "31", "Warcraft: The Beginning", "Colossal", "The Handmaiden", 
## "The Legend of Tarzan", "10 Cloverfield Lane", "This Beautiful Fantastic", 
## "Silence", "Your Name.", "The Nice Guys", "Deepwater Horizon", 
## "The Conjuring 2", "Nocturnal Animals", "Star Trek Beyond", "The Invisible Guest", 
## "Captain Fantastic", "Hush", "The Autopsy of Jane Doe", "Don't Breathe", 
## "Train to Busan", "The Huntsman: Winter's War", "Passengers", 
## "The Neon Demon", "A Cure for Wellness", "Hidden Figures", "Nerve", 
## "Sully", "The Boy", "The Lost City of Z", "Zootopia", "Manchester by the Sea", 
## "Trolls", "The Jungle Book", "13 Hours", "Gods of Egypt", "Sausage Party", 
## "Jason Bourne", "The Shallows", "Hell or High Water", "The Bad Batch", 
## "Criminal", "The Girl on the Train", "Miss Peregrine's Home for Peculiar Children", 
## "Below Her Mouth", "Dangal", "Bad Moms", "The Accountant", "Joker", 
## "Mike and Dave Need Wedding Dates", "Within", "Blair Witch", 
## "London Has Fallen", "Now You See Me 2", "Collateral Beauty", 
## "Billy Lynn's Long Halftime Walk", "Allegiant", "Inferno", "The Secret Life of Pets", 
## "The Love Witch", "Independence Day: Resurgence", "Dirty Grandpa", 
## "Pride and Prejudice and Zombies", "Lights Out", "The Great Wall", 
## "Miss Sloane", "Allied", "Swiss Army Man", "Hail, Caesar!", "Why Him?", 
## "Café Society", "Raw", "Jack Reacher: Never Go Back", "The Choice", 
## "The Edge of Seventeen", "Finding Dory", "The Girl with All the Gifts", 
## "Central Intelligence", "Resident Evil: The Final Chapter"), 
##     rating = c(6, 8, 7.1, 7.3, 6.4, 6.5, 8, 7.8, 7.6, 7.8, 8.1, 
##     7.1, 7.4, 7.4, 7.9, 6.9, 7.5, 7.3, 5.2, 6.9, 5.6, 8, 5.1, 
##     6.8, 6.2, 8.1, 6.2, 7.2, 6.9, 7.1, 8.4, 7.4, 7.1, 7.3, 7.5, 
##     7.1, 8.1, 7.9, 6.6, 6.8, 7.1, 7.5, 6.1, 7, 6.2, 6.4, 7.8, 
##     6.5, 7.4, 6, 6.6, 8, 7.8, 6.5, 7.4, 7.3, 5.4, 6.2, 6.6, 6.3, 
##     7.6, 5.3, 6.3, 6.5, 6.7, 5.5, 8.4, 6.2, 7.3, 8.4, 6, 5.1, 
##     5, 5.9, 6.5, 6.8, 6.2, 5.7, 6.2, 6.5, 6.2, 5.2, 5.9, 5.8, 
##     6.3, 5.9, 7.5, 7.1, 7, 6.3, 6.2, 6.6, 7, 6.1, 6.6, 7.3, 7.3, 
##     6.6, 6.3, 5.5), votes = c(550883L, 450506L, 159584L, 377291L, 
##     44553L, 587788L, 841926L, 491520L, 234017L, 605293L, 380950L, 
##     120237L, 236770L, 182392L, 533256L, 367689L, 537649L, 375703L, 
##     191059L, 172085L, 10156L, 187970L, 16173L, 229495L, 52891L, 
##     85032L, 153302L, 267207L, 7117L, 88096L, 146084L, 248107L, 
##     136894L, 203215L, 212438L, 213721L, 92178L, 165583L, 84318L, 
##     81586L, 194333L, 116080L, 89986L, 319209L, 75171L, 73144L, 
##     176890L, 105593L, 210414L, 70913L, 69082L, 394123L, 220764L, 
##     64677L, 245385L, 106472L, 97376L, 162877L, 194206L, 110592L, 
##     182036L, 22110L, 53440L, 155522L, 144371L, 8236L, 130355L, 
##     104226L, 238906L, 2596L, 76853L, 4921L, 38053L, 130485L, 
##     222893L, 81235L, 19560L, 98296L, 142786L, 169305L, 10095L, 
##     157922L, 104334L, 47400L, 104161L, 115003L, 55163L, 129396L, 
##     94867L, 113806L, 91951L, 64369L, 51964L, 125540L, 31275L, 
##     91233L, 219421L, 48430L, 143871L, 76892L), genre = c("Action", 
##     "Comedy", "Biography", "Horror", "Animation", "Action", "Action", 
##     "Action", "Animation", "Action", "Biography", "Animation", 
##     "Drama", "Drama", "Drama", "Action", "Action", "Adventure", 
##     "Action", "Action", "Horror", "Biography", "Horror", "Action", 
##     "Comedy", "Drama", "Action", "Drama", "Comedy", "Drama", 
##     "Animation", "Action", "Action", "Horror", "Drama", "Action", 
##     "Crime", "Comedy", "Horror", "Horror", "Crime", "Action", 
##     "Action", "Drama", "Horror", "Drama", "Biography", "Adventure", 
##     "Biography", "Horror", "Biography", "Animation", "Drama", 
##     "Animation", "Adventure", "Action", "Action", "Animation", 
##     "Action", "Drama", "Action", "Drama", "Action", "Crime", 
##     "Adventure", "Drama", "Action", "Comedy", "Action", "Comedy", 
##     "Comedy", "Horror", "Horror", "Action", "Action", "Drama", 
##     "Action", "Action", "Action", "Animation", "Comedy", "Action", 
##     "Comedy", "Action", "Drama", "Action", "Drama", "Action", 
##     "Comedy", "Comedy", "Comedy", "Comedy", "Drama", "Action", 
##     "Drama", "Comedy", "Animation", "Drama", "Action", "Action"
##     ), rank = 1:100, metascore = c(40L, 93L, 57L, 62L, NA, 44L, 
##     65L, 65L, 81L, 75L, 71L, 59L, 99L, 51L, 81L, 52L, 72L, 66L, 
##     60L, 54L, NA, 69L, 35L, 32L, 70L, 84L, 44L, 76L, 51L, 79L, 
##     79L, 70L, 68L, 65L, 67L, 68L, NA, 72L, 67L, 65L, 71L, 72L, 
##     35L, 41L, 51L, NA, 47L, 74L, 58L, 74L, 42L, 78L, 78L, 96L, 
##     56L, 77L, 48L, 25L, 66L, 58L, 59L, 88L, 62L, 36L, 48L, 57L, 
##     42L, NA, 60L, 51L, NA, 51L, 47L, 28L, 46L, 23L, 53L, 33L, 
##     42L, 61L, 82L, 32L, 21L, 45L, 58L, 42L, 64L, 60L, 64L, 72L, 
##     39L, 64L, 81L, 47L, 26L, 77L, 77L, 67L, 52L, 49L), gross = c(325.1, 
##     151.1, 43.02, 138.29, 3.78, 330.36, 363.07, 532.18, 248.76, 
##     408.08, 67.21, 270.4, 27.85, 56.25, 100.55, 155.44, 232.64, 
##     234.04, 128.34, 93.43, NA, 51.74, 0.78, 47.37, 3.02, 2.01, 
##     126.64, 71.9, 0.04, 7.1, 5.02, 36.26, 61.43, 102.47, 10.64, 
##     158.85, NA, 5.88, NA, NA, 89.22, 2.13, 48.39, 100.01, 1.33, 
##     8.11, 169.61, 38.56, 125.07, 35.82, 8.58, 341.27, 47.7, 153.71, 
##     364, 52.85, 31.15, 97.69, 162.43, 55.12, 26.86, 0.18, 14.27, 
##     75.4, 87.24, NA, 12.39, 113.26, 86.26, NA, 46.01, NA, 20.78, 
##     62.68, 65.08, 30.98, 1.72, 66.18, 34.34, 368.38, 0.23, 103.14, 
##     35.54, 10.91, 67.27, 45.54, 3.44, 40.1, 4.21, 30.08, 60.32, 
##     11.1, 0.51, 58.7, 18.71, 14.26, 486.3, NA, 127.44, 26.83), 
##     director = c("David Ayer", "Damien Chazelle", "Todd Phillips", 
##     "M. Night Shyamalan", "Sam Liu", "Zack Snyder", "Tim Miller", 
##     "Gareth Edwards", "Ron Clements", "Anthony Russo", "Mel Gibson", 
##     "Garth Jennings", "Barry Jenkins", "Thea Sharrock", "Denis Villeneuve", 
##     "Bryan Singer", "Scott Derrickson", "David Yates", "Paul Feig", 
##     "Antoine Fuqua", "Damien Leone", "Garth Davis", "Rob Zombie", 
##     "Duncan Jones", "Nacho Vigalondo", "Chan-wook Park", "David Yates", 
##     "Dan Trachtenberg", "Simon Aboud", "Martin Scorsese", "Makoto Shinkai", 
##     "Shane Black", "Peter Berg", "James Wan", "Tom Ford", "Justin Lin", 
##     "Oriol Paulo", "Matt Ross", "Mike Flanagan", "André Øvredal", 
##     "Fede Alvarez", "Sang-ho Yeon", "Cedric Nicolas-Troyan", 
##     "Morten Tyldum", "Nicolas Winding Refn", "Gore Verbinski", 
##     "Theodore Melfi", "Henry Joost", "Clint Eastwood", "William Brent Bell", 
##     "James Gray", "Byron Howard", "Kenneth Lonergan", "Mike Mitchell", 
##     "Jon Favreau", "Michael Bay", "Alex Proyas", "Greg Tiernan", 
##     "Paul Greengrass", "Jaume Collet-Serra", "David Mackenzie", 
##     "Ana Lily Amirpour", "Ariel Vromen", "Tate Taylor", "Tim Burton", 
##     "April Mullen", "Nitesh Tiwari", "Jon Lucas", "Gavin O'Connor", 
##     "Raju Murugan", "Jake Szymanski", "Phil Claydon", "Adam Wingard", 
##     "Babak Najafi", "Jon M. Chu", "David Frankel", "Ang Lee", 
##     "Robert Schwentke", "Ron Howard", "Chris Renaud", "Anna Biller", 
##     "Roland Emmerich", "Dan Mazer", "Burr Steers", "David F. Sandberg", 
##     "Yimou Zhang", "John Madden", "Robert Zemeckis", "Dan Kwan", 
##     "Ethan Coen", "John Hamburg", "Woody Allen", "Julia Ducournau", 
##     "Edward Zwick", "Ross Katz", "Kelly Fremon Craig", "Andrew Stanton", 
##     "Colm McCarthy", "Rawson Marshall Thurber", "Paul W.S. Anderson"
##     ), actor = c("Will Smith", "Ryan Gosling", "Jonah Hill", 
##     "James McAvoy", "Kevin Conroy", "Ben Affleck", "Ryan Reynolds", 
##     "Felicity Jones", "Auli'i Cravalho", "Chris Evans", "Andrew Garfield", 
##     "Matthew McConaughey", "Mahershala Ali", "Emilia Clarke", 
##     "Amy Adams", "James McAvoy", "Benedict Cumberbatch", "Eddie Redmayne", 
##     "Melissa McCarthy", "Denzel Washington", "Jenna Kanell", 
##     "Dev Patel", "Malcolm McDowell", "Travis Fimmel", "Anne Hathaway", 
##     "Min-hee Kim", "Alexander Skarsgård", "John Goodman", "Mia Farkasovska", 
##     "Andrew Garfield", "Ryûnosuke Kamiki", "Russell Crowe", 
##     "Mark Wahlberg", "Vera Farmiga", "Amy Adams", "Chris Pine", 
##     "Mario Casas", "Viggo Mortensen", "John Gallagher Jr.", "Brian Cox", 
##     "Stephen Lang", "Yoo Gong", "Chris Hemsworth", "Jennifer Lawrence", 
##     "Elle Fanning", "Dane DeHaan", "Taraji P. Henson", "Emma Roberts", 
##     "Tom Hanks", "Lauren Cohan", "Charlie Hunnam", "Ginnifer Goodwin", 
##     "Casey Affleck", "Anna Kendrick", "Neel Sethi", "John Krasinski", 
##     "Brenton Thwaites", "Seth Rogen", "Matt Damon", "Blake Lively", 
##     "Chris Pine", "Suki Waterhouse", "Kevin Costner", "Emily Blunt", 
##     "Eva Green", "Erika Linder", "Aamir Khan", "Mila Kunis", 
##     "Ben Affleck", "Guru Somasundaram", "Zac Efron", "Michael Vartan", 
##     "James Allen McCune", "Gerard Butler", "Jesse Eisenberg", 
##     "Will Smith", "Joe Alwyn", "Shailene Woodley", "Tom Hanks", 
##     "Louis C.K.", "Samantha Robinson", "Liam Hemsworth", "Robert De Niro", 
##     "Lily James", "Teresa Palmer", "Matt Damon", "Jessica Chastain", 
##     "Brad Pitt", "Paul Dano", "Josh Brolin", "Zoey Deutch", "Jesse Eisenberg", 
##     "Garance Marillier", "Tom Cruise", "Benjamin Walker", "Hailee Steinfeld", 
##     "Ellen DeGeneres", "Sennia Nanua", "Dwayne Johnson", "Milla Jovovich"
##     )), row.names = c(NA, -100L), class = c("tbl_df", "tbl", 
## "data.frame"))
2

2 Answers

1
votes

The problem is not with scale_fill_brewer. To colour your dots, use scale_color_brewer. I think you have the odd parenthesis because the legend from the rug interferes with the legend from the color.

gp<-ggplot(IMDB)+
  aes(x=runtime,y=rating,col=genre,
      label0="-------",label1=rank, label2="-", label4=runtime, label5=genre, label6=rating, label7=metascore, label8=votes, label9=gross, label10=director, label11=actor)+
  labs(title="Point Plot of the Top 100 Films of 2016\nsized by votes and color filled by genre")+
  theme(plot.title = element_text(hjust=0,size=10))+
  guides(size=FALSE)+ # supress legend for size/votes
geom_point(aes(size=votes))+
           scale_color_brewer(palette = "Set1")+
  geom_rug(show.legend = FALSE)

enter image description here

1
votes

Minor changes make it work. Not using fill as an aesthetic but using color for coloring the points makes things work.

gp<-ggplot(IMDB)+
  aes(x=runtime,y=rating, size=votes,color=genre #,
 #     label0="-------",label1=rank, label2=title, label4=runtime, label5=genre, label6=rating, label7=metascore, label8=votes, label9=gross, label10=director, label11=actor#
 )+
  labs(title="Point Plot of the Top 100 Films of 2016\nsized by votes and color filled by genre")+
  theme(plot.title = element_text(hjust=0,size=10))+
  guides(size=FALSE)+ # supress legend for size/votes
#  scale_fill_brewer(palette = "Set1")+
geom_point()+
           scale_color_brewer(palette = "Set1")+
  geom_rug(inherit.aes = FALSE,
           data=IMDB,
           aes(x=runtime,y=rating,color=genre))
ggplotly(gp)

Additionally, I removed all the labelx aesthetics because they are not used and I do not understand what their purpose is.

The parentheses IMHO appreared because of your double use of the palette for fill and color.