0

hello everyone I hope everything is fine.

I'm trying to order my ggplot bars by group but it won't work. I've tried several things as described in the post bellow but I'm still not able to see the bars ordered. Ggplot is ordering by Sample order instead of Cell.Type order.

Can you please help?

My df:

            Cell.Type      Sample   Counts
SRR10162433     hiPSC SRR10162433 27794703
SRR10162434     hiPSC SRR10162434 27077486
SRR10162435     hiPSC SRR10162435 28423798
SRR10162436     hiPSC SRR10162436 26941632
SRR10162445     hiPSC SRR10162445 27393854
SRR10162446     hiPSC SRR10162446 26964857
SRR10162430       TFB SRR10162430 23063031
SRR10162432       TFB SRR10162432 27106366
SRR10162438       TFB SRR10162438 25882727
SRR10162440       TFB SRR10162440 25535387
SRR10162442       TFB SRR10162442 30059135
SRR10162444       TFB SRR10162444 28329032
ggplot(data = libSizes, aes(x = Sample, y = Counts/1e6, fill = Cell.Type)) +
  labs(fill = "CellType", x = "Sample", y = "Counts (CPM)") +
  geom_bar(stat = "identity", position = 'dodge') +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal()

GGplot bargraph I got with the above code

I tried suggestions in this post:

Order Bars in ggplot2 bar graph

I was expecting to order my ggplot bars by group (hiPSC, TFB), I wish to see the green bars first, then the orange ones.

Thanks in advance.

Thalles
  • 1
  • 1
  • You say that you tried suggestions from the linked question: can you show the code that you tried? The code in your question does not contain anything that would reorder the bars, as given in the answers to the linked question. – neilfws Jan 24 '23 at 02:19
  • 1
    Here's another SO question about reordering categories in R and ggplot2 by using factors: https://stackoverflow.com/questions/45148897/how-to-reorder-factor-levels-in-a-tidy-way – Jon Spring Jan 24 '23 at 04:05
  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems (rather than a table or screenshot for example). One way of doing is by using the `dput` function on the data or a subset of the data you are using, then pasting the output into your question. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Jan 24 '23 at 06:30

1 Answers1

0

You need to convert "Sample" column in factors according to the order you want. In your example, it seems that the data frame is already ordered.

With the data frame you provided you just need to do this:

libSizes$Sample=factor(libSizes$Sample, levels = unique(libSizes$Sample))

And then use the exact same plotting:

ggplot(data = libSizes, aes(x = Sample, y = Counts/1e6, fill = Cell.Type)) +
  labs(fill = "CellType", x = "Sample", y = "Counts (CPM)") +
  geom_bar(stat = "identity", position = 'dodge') +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal()

If you want other orders you need to sort the data frame as you want. For instance, let's say that you want TFB shown before "hiPSC": By doing the following you will have on the first lines TFB, then hiPSC. After than you convert the "Sample" column according to the new order.

libSizes=libSizes[order(libSizes$Cell.Type, decreasing = TRUE),]
libSizes$Sample=factor(libSizes$Sample, levels = unique(libSizes$Sample))

Here the result of the first part of the code: enter image description here

And here the result of the second part of the code with the new order: enter image description here

Fabrizio
  • 927
  • 9
  • 20