How to sort an R Plotly grouped bar chart within groups?

Yang Wu

Reproducible example:

library(data.table)
library(plotly)

df <- data.table(
  x = c(rep("positive", 4), rep("neutral", 4), rep("none", 4), rep("negative", 4)),
  y = rep(c("one", "two", "three", "four"), 4),
  percent = c(
    38.9, 33.3, 8.7, 19.1, 29.4, 29.6, 21.6, 19.4,
    37.4, 24.9, 16.6, 21.1, 27.8, 34.5, 14.4, 23.2
  )
)

# Sort rows based on 'percent' within each subgroup in 'y', preserving grouping in 'x'
df <- df[order(x, -percent)]

plot_ly(
  df, 
  x = ~percent,
  y = ~x,
  color = ~y,
  type = "bar",
  orientation = "h",
  hovertemplate = "Group: %{fullData.name} <br> Percent: %{x}% <extra></extra>",
  opacity = 0.9,
  hoverlabel = list(
    align = "left"
  )
) %>%
  layout(
    font = list(
      family = "Open Sans",
      size = 13
    ),
    xaxis = list(
      title = "", 
      showticklabels = TRUE
    ),
    yaxis = list(
      title = ""
    )
  )

This produces the following chart:

enter image description here

How do I sort the bars from highest to lowest (percentages) within each group? Is this possible at the R level? I tried specifying categoryorder and categoryarray parameters in layout but it doesn't seem to take effect? Thanks.

Andrew Ingalls

So this is what I would do for a faceted solution which uses ggcharts library and bar_chart:

p <- ggcharts::bar_chart(
  df,
  y,
  percent,
  fill = x,
  facet = x,
)
p

Not exactly what you wanted

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Grouped bar chart with Plotly

R - Grouped Bar Plot Ordering Within Groups

How to color bars in grouped plotly bar chart

How to reorder the groups in a grouped bar-chart

How to create a grouped bar chart where bars within groups are organized in descending order?

How to add a Pie chart and grouped Bar chart on plotly express subplots

Plotly: How to animate a bar chart with multiple groups using plotly express?

Adding error bars to a grouped R plotly bar chart

How to label a grouped bar chart using plotly express?

How to specify the x coordinate on a grouped bar chart on plotly?

Dimple.js: How to color the background of groups in a grouped bar chart?

Plotly: Sort multicategory bar chart

Create a grouped bar chart of sums in plotly

python plotly - stacked + grouped bar chart

plotly: descending order in grouped bar chart

Plotly grouped bar chart from pandas df

Grouped Bar Chart of Means in R

Plot grouped bar chart in R

R Plotly overlay bar chart

Change color of each bar in a grouped bar chart plotly to custom colors

Plotly bar chart legend within subplot

How to make a grouped bar chart

How do I split grouped bar chart in R by variable

How to created a grouped and stacked bar chart in R with ggplot2

R: How can I custum reorder a grouped bar chart?

In R, what is the best way to add the p-values from comparisons between groups to a grouped bar chart

How to remove x-axis tick labels in a stacked-and-grouped bar chart using Plotly

R - Plotly several color palettes grouped bar

How to Arrange the x axis on a Plotly Bar Chart with R

TOP Ranking

HotTag

Archive