How to change the x-axis ticks in a facet-wrapt stacked bar chart--ggplot R

Jota3go

I'm trying to replace the x-axis tick in a faceted stacked bar chart by another variable I have in my dataframe. I am comparing the corresponding week for two different years, so I am using the "year" variable on x-axis, the "total" variable on y-axis and wrapping by the week's number in the years (i.e the first week of January gets number 1, the second is 2 etc). I've tried a lot of things already including scale_x_discrete; scale_x_date; axi.ticks.x, geom_text etc....

Here is a reproducible example:

dat <- data.frame(date = as.Date(c("2021-01-03", "2020-01-05", "2021-01-03", "2020-01-05", "2021-01-03", "2021-01-03", "2021-01-10","2020-01-12", "2021-01-10", "2020-01-12", "2021-01-10", "2021-01-10", "2021-01-17", "2020-01-19", "2021-01-17", "2020-01-19", "2021-01-17", "2021-01-17")),
                 total = c(109334, 150052, 36546, 51476, 2961, 19988, 106719, 126748, 31893, 
                           29576, 5401, 19336, 91021, 108892, 23030, 26115, 3861, 15663),
                 product = c("A", "A","B", "B", "C", "D", "A", "A", "B", "B", "C", "D", "A", 
                              "A","B", "B", "C", "D"),
                 week = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3),
                 year = c(2021,2020,2021,2020,2021,2021,2021,2020,2021,2020,2021,2021,2021,2020,2021,2020,2021,2021))  

dat %>% ggplot(aes(year, total, color = year == 2021)) +
  geom_bar(position = "stack", stat = "identity", size =0.6)+
  #scale_x_date(expand = c(0,0), labels = date_format("%m/%d/%y"))+
  scale_y_continuous(expand = c(0,0), breaks = seq(0, 350000, by = 50000), limits=c(0, 350000))+
  #scale_x_discrete(breaks = dat$year, labels = dat$date)+
  scale_color_manual(values = c(NA, 'red'), guide=F)+
  facet_wrap(~week, nrow = 1)+
  #geom_text(aes(y= total, label = date), position = position_dodge(width = 1), angle = 90, vjust = -1, size = 2)+
  theme(axis.text.x=element_text(angle = 90, vjust =0, hjust =0),
        #axis.ticks.x = ,
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.spacing.x = unit(0.1, "lines"))

The final result would be having the x-axis showning the week's date (Sunday's date for that week). desired x-axis ticks (please ignore the * on it)

Thanks!

Jon Spring

It looks like you could use a discrete x-axis here, since you don't care about the position of dates on a timeline as much as just being categories that are in chronological order. Using as.character(date) accomplishes that, or you could swap in format(date, "%d-%b-%Y") to replicate the date format in your example. Then you'll also want the scales = "free_x" parameter of facet_wrap, so that each facet can show just the dates that appear in that facet.

Edit: If you want to show a formatted date, but have it in chronological order, you'll need to make the formatted date into a factor. forcats::reorder() gives a way to control the order of the factor based on another variable.

dat %>% 
    mutate(date_label = format(date, "%d-%b-%Y") %>% forcats::fct_reorder(date)) %>%
    ggplot(aes(date_label, total, color = year == 2021)) +
    geom_bar(position = "stack", stat = "identity", size =0.6)+
    scale_y_continuous(expand = c(0,0), breaks = seq(0, 350000, by = 50000), limits=c(0, 350000))+
    scale_color_manual(values = c(NA, 'red'), guide=F)+
    facet_wrap(~week, nrow = 1, scales = "free_x")+
    ...

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the position of stacked stacked bar chart in ggplot in R?

How to create a stacked bar chart in r with ggplot

R: ggplot stacked bar chart with counts on y axis but percentage as label

How to make axis label of stacked bar chart in the middle of ticks chart in D3?

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

how to change the stacked bar chart using ggplot2 (percentage, sort) in R

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

Manually change order of y axis items on complicated stacked bar chart in ggplot2

Why does stacked bar plot change when add facet in r ggplot2

Creating a Stacked Percentage Bar Chart in R with ggplot

R ggplot labels on stacked bar chart

Special Stacked Bar Chart R ggplot

Create Stacked "Progress" Bar Chart in R with ggplot

R ggplot Sort Percent Stacked Bar Chart

R Stacked Bar Chart: How to change the order of geom text labels

How to change stacking order in stacked bar chart in R?

Stacked bar chart with group by and facet

Stacked bar chart swap x-axis and y-axis

How to manually change grouping of just stacked portion of a stacked + grouped ggplot bar-chart

How to add direct labels to a bar chart in ggplot for numeric x axis

How to plot a Stacked and grouped bar chart in ggplot?

How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

How to fix no. of ticks on X Axis in Bar Chart using d3?

ggplot change color of one bar from stacked bar chart

Reorder x axis in bar chart in ggplot

How to rename the y axis ticks in ggplot r

R: Multiple Columns for Each X-axis Element in Stacked geom_bar (ggplot)

r ggplot2 modify x axis labels of stacked bar plot with interaction aes

How to change position of x-axis text in bar graph in ggplot?