Sorting stacked columns using values from another column

Hi, I can use ggplot to get the stacked columns, but only sorted alphabetically vertically. I have seen several examples of ordering manually but I have too many points to order them manually.

I need each column of the plot to stack in ascending order, using the $_CLEARED column.

Any help appreciated!

Stacked column:
ggplot(data, aes(fill=DUID, y=MW_CLEARED, x=INTERVAL_DATETIME)) +
geom_bar(position="stack", stat="identity")

DUID STATE INTERVAL_DATETIME MW_CLEARED $_CLEARED
MEWF1 QLD1 31/01/2022 4:05 2.516 -983.5
BARRON-1 QLD1 31/01/2022 4:05 33 -983.2
BARRON-2 QLD1 31/01/2022 4:05 33 -983.2
TNPS1 QLD1 31/01/2022 4:05 360 -972.3
COOPGWF1 QLD1 31/01/2022 4:05 178.139 -968.3
DDPS1 QLD1 31/01/2022 4:05 255 -962.9
YARWUN_1 QLD1 31/01/2022 4:05 111.93 -922.51
MPP_1 QLD1 31/01/2022 4:05 425 14.48
MPP_2 QLD1 31/01/2022 4:05 434.99689 14.48
GSTONE1 QLD1 31/01/2022 4:05 120 64.47
CALL_B_1 QLD1 31/01/2022 4:05 350 83.82
STAN-2 QLD1 31/01/2022 4:05 215 86.98
STAN-1 QLD1 31/01/2022 4:05 250 86.98
STAN-3 QLD1 31/01/2022 4:05 250 86.98
TARONG#1 QLD1 31/01/2022 4:05 190 90.58
TARONG#2 QLD1 31/01/2022 4:05 190 90.58
TARONG#3 QLD1 31/01/2022 4:05 190 90.58
TARONG#4 QLD1 31/01/2022 4:05 190 90.58
CPP_3 QLD1 31/01/2022 4:05 360 93.56
BRAEMAR5 QLD1 31/01/2022 4:05 100 95.52
KAREEYA1 QLD1 31/01/2022 4:05 22 97.53

Hi @BennyBeatts , remember put a reproducible example for better help you all the community.

If I understand well, you need make before an order of this column and next make the plot.
First, check the correct name of $_CLEARED

attach(data)

data2 <- data[order($_CLEARED),]

# Next make the plot.

Ok thanks. I have created a simplified set here.

df <- data.frame(DUID=c('MEWF1', 'BARRON-1', 'BARRON-2', 'TNSPS1', 'COOPGWF1', 'MEWF1', 'BARRON-1', 'BARRON-2', 'TNSPS1', 'COOPGWF1', 'MEWF1', 'BARRON-1', 'BARRON-2', 'TNSPS1', 'COOPGWF1'),
MW=c(2.516, 33, 33, 360, 178, 2.9, 33, 33, 360, 169, 2.9, 33, 33, 360, 171),
PERIOD=c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
PRICE=c(-983.5, -983.2, -983.2, -972.3, -968.3, -983.5, -983.2, -983.2, -972.3, -968.3, -983.5, -983.2, -983.2, -972.3, -968.3))

ggplot(df, aes(fill=DUID, y=MW, x=PERIOD)) +
geom_bar(position="stack", stat="identity")

I am attempting to sort each column using the PRICE column in ascending order.

:thinking: :thinking: but in the plot you dont use the Price column.

Make the order in this column not affect the plot.

## Using PRICE in y axis:
df2 <- df[order(PRICE),]

ggplot(df, aes(fill=DUID, y=PRICE, x=PERIOD)) +
  geom_bar(position="stack", stat="identity")

The y-axis needs to be MW, but sorted in ascending order of PRICE.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.