【问题标题】:Waffle plot does not show one group华夫饼图不显示一组
【发布时间】:2020-10-30 10:55:48
【问题描述】:

我有以下使用library(waffle)的图表

我的问题是没有出现姓氏组。我正在使用的代码如下

counts<-c(135.92, 15.98, 14.97, 14.15, 5.82, 11.82, 0.07 )
counts_names<-sprintf("%s (%s)", c("Coal", "Gas", "Wind", "Hydro", "Grid-scalar solar", "Rooftop solar", "Storage systems"), 
                  scales::percent(round(counts/sum(counts), 4)))
names(counts)<-counts_names
Generation_graph<-waffle(counts)+ scale_fill_tableau(name=NULL)

我怎样才能得到我的原始图表,右边有七个组

更新:阅读其中一个 cmets,我注意到包含选项 labels 使我能够保留包含所有名称的原始图表。

Generation_graph<-waffle(counts)+ scale_fill_tableau(name=NULL, labels=counts_names)

【问题讨论】:

    标签: r graph waffle-chart


    【解决方案1】:

    问题是您的第七类的价值太小而无法显示在情节中。最后一个未标记的组只是反映了默认情况下华夫饼添加的“正方形”以“填充”最后一列。

    根据您要达到的目标,有几种选择:

    library(waffle)
    #> Loading required package: ggplot2
    library(ggthemes)
    
    counts<-c(135.92, 15.98, 14.97, 14.15, 5.82, 11.82, 0.07)
    counts_names<-sprintf("%s (%s)", c("Coal", "Gas", "Wind", "Hydro", "Grid-scalar solar", "Rooftop solar", "Storage systems"), 
                          scales::percent(round(counts/sum(counts), 4)))
    names(counts)<-counts_names
    
    1. 您可以通过colors 参数设置颜色。在这种情况下,最后一个类别将显示在图例中,但不会显示在图中。还。在这种情况下,最后一列没有填满。
    waffle(counts, colors = tableau_color_pal()(length(counts)))
    

    1. 您可以使用ceiling(),而不是使用原始数据。因为您的最后一个类别反映在情节和图例中。
    waffle(ceiling(counts), colors = tableau_color_pal()(length(counts)))
    

    1. 最后,如果您最后一列要填满,您可以使用ceiling(),让waffle 选择颜色并使用scale_fill_manual 覆盖:
    waffle(ceiling(counts)) + scale_fill_tableau()
    

    【讨论】:

      【解决方案2】:

      您可以使用scale_fill_manual() 来获取您要查找的内容

      library(ggthemes)
      library(waffle)
      counts<-c(135.92, 15.98, 14.97, 14.15, 5.82, 11.82, 0.07 )
      counts_names<-sprintf("%s (%s)", c("Coal", "Gas", "Wind", "Hydro", "Grid-scalar solar", "Rooftop solar", "Storage systems"), 
                            scales::percent(round(counts/sum(counts), 4)))
      names(counts)<-counts_names
      Generation_graph<-waffle(counts) + 
        scale_fill_manual(values = c("red", "blue", "green", "purple", "pink", "yellow", "orange", "blue"),
                          labels = counts_names, 
                          drop = TRUE)
      

      【讨论】:

        猜你喜欢
        • 2022-07-11
        • 1970-01-01
        • 1970-01-01
        • 2018-10-04
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        相关资源
        最近更新 更多