-1

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.

  • 4
    https://stackoverflow.com/help/how-to-ask – s_baldur Jan 18 '19 at 13:10
  • Are you able to make some sample/dummy data that works with the plot you have? It doesn't have to be many rows, just a small set of data that can be used to test solutions – Jonny Phelps Jan 18 '19 at 13:12
  • Hi, Stacked Bar charts are often a pain to build, usually requiring pivoting the data (dplyr::gather). How do you want the output chart to look (x, y, group, color, etc)? – M.Viking Jan 18 '19 at 14:08
  • My x-axis is the work week, the stacked bar is the count of goods for each customer. I already have the chart via facet_grid(~Tag) but I need for each work week a stacked bar for received and a stacked bar for shipped goods. If you run the code above with the sample rows, you will see what I mean. Sorry, don't know how to describe it in a better way. – Maik Lindemann Jan 18 '19 at 14:20
  • I understand this - In your sample chart, received and shipped are facet'ed, and customers are stacked by week - but instead you want received and shipped stacked by week. In the end three stacks (1 for each week) for each customer. – M.Viking Jan 18 '19 at 14:36
  • ggplot(allRecords, aes(x = reorder(WW,Date), y = Quantity, fill = Tag)) + geom_bar(stat = "identity", position = "stack", colour = "black") + facet_grid(~Customer) – M.Viking Jan 18 '19 at 14:43
  • Thanks for your reply. This is a good view at the data too. I don't want the facetting, I need for each work week two stacked charts, one for shipped, one for received. All in one graph. – Maik Lindemann Jan 18 '19 at 15:08

3 Answers3

0

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)

  • You can make data.frame without intermediate var: allRecords <- data.frame( 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")) ) – M.Viking Jan 18 '19 at 13:58
  • you can make week easier like this -- WW = lubridate::week(c("2019-01-03 10:07:31", ... – M.Viking Jan 18 '19 at 15:01
0

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

Fernando
  • 126
  • 9
0

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

M.Viking
  • 5,067
  • 4
  • 17
  • 33