0

Trying to create a pie chart that shows the count of films in the IMDB top 250 by their rating / certificate. Despite creating a vector that specifies the manual order I want them, cannot get plotly to change the sorted order of the labels.

How can I manually change the labels for this chart?

x <- c('G', 'PG', 'PG-13', 'R', 'Not Rated', 'Other')

Ratings_df <- df %>%
  dplyr::group_by(certificate) %>%
  dplyr::summarize(count = n()) %>%
  dplyr::mutate(certificate = ifelse(count < 17, "Other", certificate)) %>%
  dplyr::group_by(certificate) %>%
  dplyr::summarise(count = sum(count)) %>%
  dplyr::mutate(certificate = factor(certificate, levels = x)) %>%
  dplyr::arrange(certificate) %>%
  data.frame()
Ratings_df

plot_ly(Ratings_df, labels = ~certificate, values = ~count) %>%
  add_pie(hole=0.6) %>%
  layout(xaxis = list(categoryorder = "array", 
                      categoryarray = c('G', 'PG', 'PG-13', 'R', 'Not Rated', 'Other')),
    title="Count of Films by Rating")
r2evans
  • 141,215
  • 6
  • 77
  • 149
TWilliams
  • 1
  • 1
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 19 '23 at 07:11
  • Could you please share some reproducible data using `dput`? – Quinten Feb 19 '23 at 10:11
  • The dupe-link talks about the legend, but it also affects the aesthetic layers as well. If adding `sort`, ala `plot_ly(Ratings_df, labels = ~certificate, values = ~count, sort = FALSE) %>% ...` does _not_ work, please @ping me and we'll work through the differences. (For that to happen, please [edit] your question and add some sample data, preferably with `dput(head(Ratings_df,20))` or similar in order to get sufficient and representative data.) – r2evans Feb 19 '23 at 16:58

0 Answers0