Home > OS >  How to add text label to R plotly aggregate chart?
How to add text label to R plotly aggregate chart?

Time:05-23

I've charted an aggregated plotly chart to present average values. I'm wondering how I can add a text label to the chart to state the average value shown on the chart.

Below is a reprex based on my code. In this code, I've made a chart presenting average snow and rainfall. I've added text labels to the chart, but they correspond to each data point on the chart (not the average, which is what I'm after). Thank you for your advice.

Current code:

my_tibble <- tibble(precipitation = as.numeric(runif(10,3,10)),
                    week = c(1,2,3,4,5,1,2,3,4,5),
                    fall_type = c("rain", "snow", "snow", "rain", "rain", 
                                  "rain", "rain", "snow", "snow", "snow"),
                    region = c("south", "south", "south", "south", "south", 
                               "north", "north", "north", "north", "north")) 
my_tibble <- my_tibble %>% 
  mutate(chart_cat = paste(region, fall_type, sep = ","))

make_a_chart <- function(location){
  
  my_tibble %>% 
    filter(region==location) %>% 
  
  plot_ly() %>% 
  
  layout(
    yaxis = list(title = none),
    xaxis = list(title = none,
                 tickformat = ","),
    barmode = 'stack',
    showlegend = F) %>% 
  
  add_trace(
    x = ~precipitation,
    y = ~fall_type,
    type = "bar",
    transforms = list(
      list(
        type = "aggregate",
        groups = ~chart_cat,
        aggregations = list(
          list(
            target = "x", func = "avg", enabled = T))))) %>% 
  
  add_text(
    x = ~precipitation*1.25,
    y = ~fall_type,
    text = ~round(precipitation,0),
    showlegend = F,
  ) 
}

make_a_chart("south")  

CodePudding user response:

You can aggregate text just like you aggregate any other element within a Plotly plot.

For example, you could do this:

make_a_chart <- function(location){
  my_tibble %>% 
    filter(region==location) %>% 
    plot_ly() %>% 
    layout(yaxis = list(title = none),
           xaxis = list(title = none, tickformat = ","),
      barmode = 'stack', showlegend = F) %>% 
    add_trace(
      x = ~precipitation,
      y = ~fall_type,
      type = "bar",
      transforms = list(
        list(
          type = "aggregate",
          groups = ~chart_cat,
          aggregations = list(
            list(target = "x", func = "avg", enabled = T))))) %>% 
    add_text(
      x = ~precipitation*1.25,
      y = ~fall_type,
      text = ~round(precipitation,0),
      transforms = list(
        list(
          type = "aggregate",
          groups = ~chart_cat,
          aggregations = list(
            list(target = "text", func = "avg", enabled = T),
            list(target = "x", func = "avg", enabled = T)))),
      showlegend = F,
    )

}

enter image description here

You can style the text, as well.

make_a_chart("south") %>% layout(font = list(color = "#b21e29", size = 15))

enter image description here

  • Related