0

I have the dataframe below:

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

and I want to color the bars by color and the legend names to show the Range. I do everything here but I get no bars. I think it has to do with the structure of my dataframe cause my code is correct I believe.

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)
firmo23
  • 7,490
  • 2
  • 38
  • 114
  • In general you should check your barplot before injecting it into a `ggplotly()` call :). I had to comment out (i) # scale_fill_manual(values = mycols) +, (ii) # scale_y_continuous(labels = paste0(ylab, "M"), breaks = 10^6 * ylab), and (iii) correct `Age group` to `Age.group`. With this `ggplot()` object you can now feed `ggplotly()`. You should get a coloured bar chart. Now start fixing the errors. ... You may also think about checking out a plotly tutorial to code the barchart directly in `plotly`. (Note: `geom_bar(... stat="identity")` can be done now with `geom_col()`.) – Ray May 25 '21 at 17:17
  • with those lines you commented out the result will be different I guess though – firmo23 May 25 '21 at 17:21
  • also the colors I get now are different that those that are set in the dataframe. – firmo23 May 25 '21 at 17:30
  • Conceptually, the "result(s)" is the same in the sense that the "hard data" is processed. Scales "beautify" your plot. As a debugging strategy, I recommend have it plotted first, i.e. pipeline works, before dealing with the "cross of the t". Obviously, you call fill_manual mycols (and not color) ... The point here is that you work on the lines and fix the errors. And if you use correct variable assigments, your colours will be as wanted. or the scale labels are marked in the way you want. – Ray May 25 '21 at 17:32
  • I tried but I could not have the expected result. if you can provide a solution I will appreciate and accept thanks – firmo23 May 25 '21 at 17:36

1 Answers1

1

@firmo23 as requested, I just fixed your ggplot code:

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

I hope this gets you there. I get this plot then (i.e. what I mentioned in the comments - this part works!).

enter image description here

Ray
  • 2,008
  • 14
  • 21
  • Glad that it helped you. As mentioned before, if you run into trouble the debugging strategy is to identify "where" things break. Injecting a ggplot-object in a `plotly()` call can only work if the ggplot renders error-free. The "expected" colours is an add on to a basic plot. Thus, if you managed to get the "pipeline" (basic) ggplot --> plotly in place, you can work on the whistle and bells you want to add. E.g. labels, scales, etc. This way you add layer by layer and confine the potential error / issue areas. – Ray May 26 '21 at 05:12
  • I have an issue here with that https://stackoverflow.com/questions/67780876/ggplot-object-does-not-color-bars-according-to-color-specified-in-the-dataframe – firmo23 May 31 '21 at 22:42