Barplot Conditional on Two Variables

With the toy dataset below, I'd like to create a bar chart.

Each bar should be the description. The length of the bar should be the total length of days. But I'd also like the bar to have a total of the cost.

Is it possible to do this, please?

Desc <- rep(LETTERS[1:10], times = 10)
Desc <- as.factor(Desc)
Cost <- sample(1:100, replace = FALSE)
Days <- sample(1:100, replace = FALSE)
DAT <- data.frame(Desc, Days, Cost)
1 Like

This is very crude but it may help. I have placed Cost in tthe bar to' hope, reduce the imprcession that it implies 'Days`

What exactly are you trying to display? There may be a number of better approaches. A barchart can be a bit clumsy in many cases

# Load packages --------------------------------------------------------
pacman::p_load(ggplot2, numform)

# Load data ------------------------------------------------------------
Desc <- sample(LETTERS[1:10], replace = FALSE)
Desc <- as.factor(Desc)
Cost <- sample(1:10, replace = FALSE)
Days <- sample(1:10, replace = FALSE)
dat1 <- data.frame(Desc, Days, Cost)

# Plot --------------------------------------------------------------------

ggplot(dat1, aes(x = Desc, y = Days)) +
  geom_col(fill = "skyblue") +
  geom_text(aes(label = f_dollar(Cost)), vjust = +4.0) 

1 Like

Hi @jrkrideau - appreciate your response and time. Before you replied, I had just updated my code so that the Cost variable had more observations and the label on the bars would be the sums. Would you be able to indicate how to include the the sums of costs for each bar, please?

1 Like

I cannot see how your new data set can work. You only have ten distinct values of Desc but 100 distinct values of Cost. You need a one–to–one mapping of the two.

Again, can you give us a rough description of what you are actually plotting? Finagling around with a mock data set when we have no idea of the expected end result is rather a waste of time. There ins nothing wrong with a mock data set but we need to know what the end result hopes to show.

1 Like