在 plot_ly 中手动排序标签

问题描述 投票:0回答:0

尝试创建一个饼图,根据评分/证书显示 IMDB 前 250 名的电影数量。尽管创建了一个向量来指定我想要的手动顺序,但无法改变标签的排序顺序。

如何手动更改此图表的标签?

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")
r vector dplyr plotly visualization
© www.soinside.com 2019 - 2024. All rights reserved.