首页 > 解决方案 > 如何修复饼图rstudio中的图例

问题描述

pie(table(games_list$Genre),
    main = "Rating Pie Chart",
    col=brewer.pal(length(games_list$Genre),'Spectral'))

legend("topright",
       legend=row.names(games_list$Genre), 
       fill = brewer.pal(length(games_list$Rating), 'Spectral'))

图例中的错误(“topright”,图例 = row.names(games_list$Genre),fill = brewer.pal(length(games_list$Rating),:“图例”的长度为 0

标签: r

解决方案


检查row.names(games_list$Genre)返回的东西是legend参数所期望的。

因为您没有提供示例数据,所以我iris在下面的示例中使用了数据集:

pie(table(iris$Species),
    main = "Rating Pie Chart",
    col=brewer.pal(
      length(unique(iris$Species)), # we need only unique values
      'Spectral'
      )
    )

legend("topright",
       legend=unique(iris$Species),
       fill = brewer.pal(
         length(unique(iris$Species)),
         'Spectral'
         )
       )

上面的代码产生: 在 R 中使用基本图形的示例饼图

顺便说一句,如果您要显示超过 2 或 3 个类型/类,最好使用条形图。这是 Stephen Few 写的一篇关于“为甜点省心”的文章


推荐阅读