数据框结构防止条形图显示在 ggplotly() 图表中

Posted

技术标签:

【中文标题】数据框结构防止条形图显示在 ggplotly() 图表中【英文标题】:Dataframe structure prevents bars to be displayed in ggplotly() chart 【发布时间】:2021-08-13 22:52:54 【问题描述】:

我有下面的数据框:

Cum<-data.frame(structure(list(Age.group = c("00-04", "00-04", "05-14", "05-14", 
                                             "15-24", "15-24", "25-49", "25-49", "50-64", "50-64", "65-79", 
                                             "65-79", "80+", "80+"), Gender = c("Female", "Male", "Female", 
                                                                                "Male", "Female", "Male", "Female", "Male", "Female", "Male", 
                                                                                "Female", "Male", "Female", "Male"), Cases = c(64578, 70518, 
                                                                                                                               187568, 197015, 414405, 388138, 1342394, 1206168, 792180, 742744, 
                                                                                                                               400232, 414613, 282268, 198026), lab = c("64,578", "70,518", 
                                                                                                                                                                        "187,568", "197,015", "414,405", "388,138", "1,342,394", "1,206,168", 
                                                                                                                                                                        "792,180", "742,744", "400,232", "414,613", "282,268", "198,026"
                                                                                                                               ), Age.group.Sum = c(135096, 135096, 384583, 384583, 802543, 
                                                                                                                                                    802543, 2548562, 2548562, 1534924, 1534924, 814845, 814845, 480294, 
                                                                                                                                                    480294), lab2 = c("135,096", "135,096", "384,583", "384,583", 
                                                                                                                                                                      "802,543", "802,543", "2,548,562", "2,548,562", "1,534,924", 
                                                                                                                                                                      "1,534,924", "814,845", "814,845", "480,294", "480,294"), color = c("#4285f4", 
                                                                                                                                                                                                                                          "#4285f4", "#90a9e0", "#90a9e0", "#dd9e5f", "#dd9e5f", "#b45f06", 
                                                                                                                                                                                                                                          "#b45f06", "#b45f06", "#b45f06", "#dd9e5f", "#dd9e5f", "#aebbd6", 
                                                                                                                                                                                                                                          "#90a9e0"), Range = c("<= 74453.8555555556", "<= 74453.8555555556", 
                                                                                                                                                                                                                                                                "148907.711111111 - 223361.566666667", "148907.711111111 - 223361.566666667", 
                                                                                                                                                                                                                                                                "372269.277777778 - 446723.133333333", "372269.277777778 - 446723.133333333", 
                                                                                                                                                                                                                                                                ">= 670084.7", ">= 670084.7", ">= 670084.7", ">= 670084.7", "372269.277777778 - 446723.133333333", 
                                                                                                                                                                                                                                                                "372269.277777778 - 446723.133333333", "223361.566666667 - 297815.422222222", 
                                                                                                                                                                                                                                                                "148907.711111111 - 223361.566666667")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                             -14L)))

我想用color 和图例名称为条形着色以显示Range。我在这里做所有事情,但我没有酒吧。我认为这与我的数据框的结构有关,因为我相信我的代码是正确的。

ggplot_obj <- ggplot(data = Cum, aes(x = `Age group`, y = Cases, group = Gender,fill = Range)) +
        geom_bar(aes(
          # Define a text object here that can be use for reference by ggplot_ly
          # thought ggplot will throw a warning
          text = paste("<b>Gender:</b>", Gender, "<br><b>Age:</b>", `Age group` ,
                       "<br><b>Cases:</b>", lab, "<br><b>Total cases in age group:</b>",
                       lab2)), 
          position = "dodge", stat = "identity") +
        geom_text(aes(y = Cases + 10000, label = Gender), vjust = 1,
                  position = position_dodge(width=0.9),size=2) +
        scale_fill_manual(values = mycols) +
        coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
        theme_bw()+ theme(
          # remove the vertical grid lines
          panel.grid.major.x = element_blank(),
          panel.border = element_blank(), axis.line.x = element_line()
        )+
        scale_y_continuous(labels = paste0(ylab, "M"),
                           breaks = 10^6 * ylab)
      #> Warning: Ignoring unknown aesthetics: text
      # running ggplotly with tooltip option reference to the text defined in ggplot object
      ggplotly(ggplot_obj, tooltip="text") %>%
        config(modeBarButtonsToRemove = c('toImage', "zoom2d", "toggleSpikelines",
                                          "hoverClosestCartesian", "hoverCompareCartesian", "drawline", "autoScale2d",
                                          "resetScale2d", "zoomIn2d", "zoomOut2d", "pan2d", 'select2d', 'lasso2d')) %>%
        config(displaylogo = FALSE)

【问题讨论】:

一般来说,您应该在将条形图注入ggplotly() 调用之前检查它:)。我不得不注释掉(i)#scale_fill_manual(values = mycols)+,(ii)#scale_y_continuous(labels = paste0(ylab,“M”),breaks = 10^6 * ylab),以及(iii)正确的@987654326 @到Age.group。有了这个ggplot() 对象,您现在可以喂ggplotly()。你应该得到一个彩色条形图。现在开始修复错误。 ...您还可以考虑查看一个绘图教程,直接在plotly 中对条形图进行编码。 (注意:geom_bar(... stat="identity") 现在可以用geom_col() 完成。) 用你注释掉的那些行我猜结果会有所不同 我现在得到的颜色也与数据框中设置的颜色不同。 从概念上讲,“结果”在处理“硬数据”的意义上是相同的。缩放“美化”你的情节。作为一种调试策略,我建议先绘制它,即管道工作,然后再处理“t 的交叉”。显然,您调用的是 fill_manual mycols (而不是颜色)......这里的重点是您在线条上工作并修复错误。如果您使用正确的变量分配,您的颜色将如您所愿。或者按照您想要的方式标记刻度标签。 我试过了,但我无法得到预期的结果。如果您能提供解决方案,我将不胜感激并接受感谢 【参考方案1】:

@firmo23 根据要求,我刚刚修复了您的 ggplot 代码:

library(ggplot2)
library(scales)

# 1. correct variable name: Age.group
# 2. assign scale_fill_manual to "data frame given color" column 
# 3. use a slightly better scale_y_continuous() call for 10^6 units

ggplot_obj <- ggplot(data = Cum, aes(x = `Age.group`, y = Cases, group = Gender,fill = Range)) +
    geom_bar(aes(
        # Define a text object here that can be use for reference by ggplot_ly
        # thought ggplot will throw a warning
        text = paste("<b>Gender:</b>", Gender, "<br><b>Age:</b>", `Age.group` ,
                     "<br><b>Cases:</b>", lab, "<br><b>Total cases in age group:</b>",
                     lab2)), 
        position = "dodge", stat = "identity") +
    geom_text(aes(y = Cases + 10000, label = Gender), vjust = 1,
              position = position_dodge(width=0.9),size=2) +
    scale_fill_manual(values = Cum$color) +
    coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
    theme_bw()+ theme(
        # remove the vertical grid lines
        panel.grid.major.x = element_blank(),
        panel.border = element_blank(), axis.line.x = element_line()
    ) +
    scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))

我希望这能让你到达那里。 然后我得到了这个情节(即我在 cmets 中提到的 - 这部分有效!)。

【讨论】:

很高兴它对您有所帮助。如前所述,如果您遇到麻烦,调试策略是确定“哪里”出现问题。只有在 ggplot 呈现无错误的情况下,在 plotly() 调用中注入 ggplot 对象才能起作用。 “预期”颜色是对基本情节的补充。因此,如果您设法获得“管道”(基本)ggplot --> plotly,您可以处理您想要添加的口哨和铃声。例如。标签、比例尺等。这样您可以逐层添加并限制潜在的错误/问题区域。 我对@9​​87654322@有疑问

以上是关于数据框结构防止条形图显示在 ggplotly() 图表中的主要内容,如果未能解决你的问题,请参考以下文章

ggplotly堆积条形图在过滤后不调整大小

更改 ggplotly() 重叠条形图中的条形颜色

如何有条件地在条形图上的条形图上显示数据框的不同列值?

如何绘制条形图,显示由数据框列组成的各种桶的频率计数?

如何从 Pandas 数据框对象显示 X 轴到 Matplotlib 条形图

Plotly:如何在将多个组作为条形图的同时显示和过滤具有多个下拉列表的数据框?