0

I am trying to plot in a stacked bar chart the accumulative cost of seven different units, using ggplot2 and geom_col. The challenge I have is when I plot the data, in the x-axis I do not get the units in an ascending order.

> b1
  Unit variable   value
  1   60k      BOM 2950806
  2  100k      BOM 3236021
  3  120k      BOM 3533470
  4  140k      BOM 3611764
  5  170k      BOM 3855279
  6  200k      BOM 4166095
  7  230k      BOM 4468843
  8   60k     NaOH  255676
  9  100k     NaOH  255676
  10 120k     NaOH  255676
  11 140k     NaOH  255676
  12 170k     NaOH  255676
  13 200k     NaOH  255676
  14 230k     NaOH  255676
library(ggplot2)
p1 <- ggplot(b1, aes(Unit, value, fill = variable))+
geom_col(position = "stack", colour = "black")+
labs(x = expression("Unit size"),
   y = expression("Cost"*" / [NOK]"))+
theme_bw()+
theme(plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())
p1

I expect the x-axis to show 60k, 100k, 120k, 140k, 200k, 230k, but instead it shows

100k, 120k, 140k, 200k, 230k, 60k.

Aloft
  • 1
  • 2
  • The order on the x-axis is due to the factor levels of `b1$Unit` --- type `levels(b1$Unit)` and you will see the factor level order 100k, 120k, ... through 60k. There are a number of options to consider for getting desired plot...see other discussions such as: https://stackoverflow.com/questions/12774210/how-do-you-specifically-order-ggplot2-x-axis-instead-of-alphabetical-order – Ben Sep 27 '19 at 16:09
  • Thanks @Ben, I tried the factor function in different ways, but I end up in a new challenge i.e. my x-axis now reads 60k, 100k, ... and at the end I get one more bar column with N/A :/ `b11 <- factor(b1$Unit, levels = names(sort(b1$Unit), decreasing = FALSE))` and for the plot `p1 <- ggplot(b1, aes(b11, value, fill = variable))` – Aloft Sep 30 '19 at 13:57
  • please see proposed answer and let me know if this is what you had in mind. – Ben Sep 30 '19 at 14:18
  • thanks @Ben, yes the proposed answer is exactly what I had in mind. – Aloft Sep 30 '19 at 14:46

1 Answers1

0

If you place your levels in the desired order:

level_order <- c('60k', '100k', '120k', '140k', '170k', '200k', '230k') 

You can use this in plots without changing the underlying data.

Let me know if this is what you had in mind.

b1 <- read.table(text =
"Unit variable   value
   60k      BOM 2950806
  100k      BOM 3236021
  120k      BOM 3533470
  140k      BOM 3611764
  170k      BOM 3855279
  200k      BOM 4166095
  230k      BOM 4468843
   60k     NaOH  255676
  100k     NaOH  255676
 120k     NaOH  255676
 140k     NaOH  255676
 170k     NaOH  255676
 200k     NaOH  255676
 230k     NaOH  255676", header = T)

library(ggplot2)

level_order <- c('60k', '100k', '120k', '140k', '170k', '200k', '230k') 

p1 <- ggplot(b1, aes(factor(Unit, level = level_order), value, fill = variable))+
  geom_col(position = "stack", colour = "black")+
  labs(x = expression("Unit size"),
       y = expression("Cost"*" / [NOK]"))+
  theme_bw()+
  theme(plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
p1

Alternatively, if you want to change the factor levels in your b1 variable, you can do the following, and no need to change your ggplot statement:

b1$Unit <- factor(b1$Unit, levels = c('60k', '100k', '120k', '140k', '170k', '200k', '230k'))

plot with units in correct order

Ben
  • 28,684
  • 5
  • 23
  • 45