Stacked barchart over work week and another group

45 views Asked by At

I have several criteria from which I want to create a stacked barchart.

My criteria are: work week, quantity, customer, Tag (Received, Shipped)

This (facet_grid(~Tag)) looks ok but is not really what I need.

ggplot(allRecords, aes(x = reorder(WW,Date), y = Quantity, fill = 
Customer)) + 
geom_bar(stat = "identity", position = "fill", colour = "black") + 
facet_grid(~Tag) +
labs(x = "Work Week") + 
ggtitle("Goods last 3 months by core customers") +
theme_bw()

I need a stacked chart for "received" and "shipped" for each work week by customer.

3

There are 3 answers

2
Maik Lindemann On

Some sample rows:

WW <- c(1,1,1,2,2,2,3,3,3)

Quantity <- c(12,23,12,34,56,23,10,11,23)

Date <- lubridate::ymd_hms(c("2019-01-03 10:07:31", "2019-01-03 10:07:31", "2019-01-03 10:07:31", "2019-01-09 15:49:41", "2019-01-09 15:49:4", "2019-01-09 15:49:4", "2019-01-16 09:53:56", "2019-01-16 09:53:56", "2019-01-16 09:53:56"))

Customer <- c("C1", "C2", "Other","C1", "C2", "Other","C1", "C2", "Other")

Tag <- as.factor(c("Received", "Received", "Shipped", "Shipped", "Received", "Received", "Shipped", "Received", "Received"))

records <- data.frame(WW, Quantity, Date, Customer, Tag)

0
M.Viking On

data

allRecords <- data.frame( 
    Quantity = c(12,23,12,34,56,23,10,11,23), 
    WW = lubridate::week(c("2019-01-03 10:07:31", "2019-01-03 10:07:31", "2019-01-03 10:07:31", "2019-01-09 15:49:41", "2019-01-09 15:49:4", "2019-01-09 15:49:4", "2019-01-16 09:53:56",     "2019-01-16 09:53:56", "2019-01-16 09:53:56")), 
    Customer = c("C1", "C2", "Other","C1", "C2", "Other","C1", "C2", "Other"), 
    Tag = as.factor(c("Received", "Received", "Shipped", "Shipped", "Received", "Received", "Shipped", "Received", "Received")) 
)

code

ggplot(allRecords, aes(x = Tag, y = Quantity, fill = Customer)) + 
  geom_bar(stat = "identity", position = "stack", colour = "black") + 
  facet_grid(~WW) +
  labs(x = "Work Week") + 
  ggtitle("Goods last 3 months by core customers") +
  theme_bw()

output

stacked bar chart

0
Fernando On

Maybe this is what you want?

ggplot(records, aes(x = reorder(WW,Date), y = Quantity, fill = 
                         Tag)) + 
  geom_bar(stat = "identity", position = "fill", colour = "black") + 
  facet_grid(~Customer) +
  labs(x = "Work Week") + 
  ggtitle("Goods last 3 months by core customers") +
  theme_bw()

enter image description here