【问题标题】:Text and percentages in labels of pie chart饼图标签中的文本和百分比
【发布时间】:2018-08-02 00:17:54
【问题描述】:

您好,我正在做一个基本的饼图,但是当我这样做时,只有“名称”显示为标签。我希望标签是名称+百分比。

所以我有:

reasons=prop.table(table(data$Reason[data$Stops %in% 1]))*100

我得到了:

DP 64
UV 20
TC 16

然后

pie(reasons, color=rainbow(reasons), main="Distribution of Reasons")

这样我只能得到带有标签DP、UV 和TC 的馅饼。

我应该添加什么来获得标签中的DP 64%、UV 20%、TC 16%?

【问题讨论】:

    标签: r label pie-chart


    【解决方案1】:

    我们可以在pie 中使用labels 参数

    library(dplyr)
    df <- read.table(text =
        "DP 64
    UV 20
    TC 16") %>%
        setNames(c("Reason", "Value")) %>%
        mutate(Percentage = sprintf("%s %3.1f%%", Reason, Value / sum(Value) * 100))
    with(df, pie(
        Value,
        labels = Percentage,
        col = rainbow(length(Value)),
        main = "Distribution of Reasons"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多