【问题标题】:Pie chart of single column in R LanguageR语言单列饼图
【发布时间】:2021-12-21 11:52:32
【问题描述】:

如何在 R 语言中创建单列的饼图? 尝试在谷歌上搜索它,但我找不到与单个列相关的任何内容。

【问题讨论】:

    标签: r pie-chart


    【解决方案1】:

    假设您的数据框如下所示:

    df <- data.frame(single_column = c(21, 48, 15, 16))
    
    df
    #>   single_column
    #> 1            21
    #> 2            48
    #> 3            15
    #> 4            16
    

    那么你可以这样做:

    library(ggplot2)
    
    ggplot(df, aes(x = 1, y = single_column, fill = factor(single_column))) + 
      geom_col() +
      geom_text(position = position_stack(vjust = 0.5), 
                aes(label = single_column)) +
      coord_polar(theta = "y") +
      theme_void() +
      theme(legend.position = "none")
    

    【讨论】:

      【解决方案2】:

      来自 Quick-R 的网站:

      # Pie Chart with Percentages
      slices <- c(10, 12, 4, 16, 8)
      lbls <- c("US", "UK", "Australia", "Germany", "France")
      pct <- round(slices/sum(slices)*100)
      lbls <- paste(lbls, pct) # add percents to labels
      lbls <- paste(lbls,"%",sep="") # ad % to labels
      pie(slices,labels = lbls, col=rainbow(length(lbls)),
         main="Pie Chart of Countries")
      

      希望它能满足您的需求。

      【讨论】:

        猜你喜欢
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多