1

我有以下图表使用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)

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

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

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

2 回答 2

2

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

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

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()

于 2020-10-30T12:47:46.097 回答
0

您可以使用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)

在此处输入图像描述

于 2020-10-30T13:13:40.667 回答