Bar chart labeled with % relative to the Months in X axis

guy_merchie

I have a data frame like this:

df <- data.frame(
     month = month.name[1:6],
     shirts = runif(6, min=1000, max=2000),
     hats = runif(6, min=1000, max=2000)
    ) |>
    pivot_longer(cols=c("shirts", "hats"), names_to="category", values_to="income")

df

The data frame:

# A tibble: 12 × 3
   month    category income
   <chr>    <chr>     <dbl>
 1 January  shirts    1782.
 2 January  hats      1489.
 3 February shirts    1094.
 4 February hats      1954.
 5 March    shirts    1467.
 6 March    hats      1483.
 7 April    shirts    1512.
 8 April    hats      1890.
 9 May      shirts    1600.
10 May      hats      1914.
11 June     shirts    1333.
12 June     hats      1609.

In order to create a bar plot with the income percentages relative for each category in each month, I did this:

df |> 
    group_by(month, category) |>
    summarize(income = sum(income, na.rm=TRUE)) |>
    ggplot(aes(x=month,y=income, fill=category)) +
    geom_bar(position="dodge", stat="identity") + 
    geom_text(aes(label=paste0(percent(income/sum(income)))), position = position_dodge(width=0.9), angle = 90, hjust=-0.1) +
    labs(x="Month",y="Income") +
    ylim(0, 4000)

But it is calculating the percentage relative to the total income sum from january to june.

I'd like this percentage to be relative to each month total income sum.

What could I change or add something new to the plot code to achieve this?

zephryl

To use monthly totals as the denominator, you need to compute your percentages while grouped by month. The data is still grouped by month after your summarize(), so add a mutate() there:

library(dplyr)
library(ggplot2)
library(scales)

df |> 
    group_by(month, category) |>
    summarize(income = sum(income, na.rm=TRUE)) |>
    mutate(pct_label = percent(income/sum(income), accuracy = 0.1)) |>
    ungroup() |>
    ggplot(aes(x=month,y=income, fill=category)) +
    geom_col(position="dodge") + 
    geom_text(aes(label = pct_label), position = position_dodge(width=0.9), angle = 90, hjust=-0.1) +
    labs(x="Month",y="Income") +
    ylim(0, 4000)

Also note that geom_col() is equivalent to geom_bar(stat = "identity").

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ordering months on x-axis in pandas bar chart

ggvis - Interactive X axis for bar chart

Hide bottom x axis in horizontal bar chart?

Rotate x axis text on Horizontal bar chart

Reorder x axis in bar chart in ggplot

How to make Chart.js with dynamic months on x-axis

How add months as a x-axis to stock chart/ high charts

Stacked bar chart swap x-axis and y-axis

bar chart R geom_bar changing legend and x axis

Horizontal axis for bar chart

Fixing X-axis of Horizontal Plotly Bar Chart

PlotlyJS bar chart X axis autorange not working as expected?

How to resize bar chart based on no of x axis categories in HighCharts

Space between bar chart with day/week/month in x axis in R

Remove some of the X axis labels in ggplot bar chart

to increase space between x axis and first horizontal bar in chart js

How to control the spacings of the x Axis label positions in a stacked bar chart?

Highcharter bar chart cut off x axis label

Holoviews: Remove variables names from x axis on grouped bar chart

Matplotlib bar chart for negative numbers going above x-axis

How to create a bar chart with non-numeric X axis?

How to nicely format the X-axis labels on a pandas bar chart

Stacked bar chart changes x axis plot number values

How to remove decimal values on x-axis of bar chart : Highcharts

ChartJS: create linear x-axis on bar chart

JFreeChart: Bar chart X axis labels overlapping with large datasets

x-axis values not showing all in stacked bar chart

Setting x-axis of bar chart to show month

Sortable bar chart with long x axis labels rotated