1

I am trying the following plotting.

I have this data set:

Pathway   Value    Col.Code
AKTSig        1         r
HRAS          2         r
Lbind         3         h
GPCRact       4         r
ACHsig        5         h
ACEest        -2        r
MRNAspl       -3        h
Notch         -4        h
Delta         -5        r
Sonic         -6        r 

I would like to plot a graph that has these columns with pathway along the x axis, value up the y axis and the columns coloured by the Col.Code column. I have tried geom_col() from ggplot2 but this always rearranges the columns into a random order i.e. not highest value to most negative. I have also tried geom_bar() but this creates counts for the pathways and doesn't plot what I have described above.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

3

You can use this:

library(dplyr)

ggplot(data,aes(x=reorder(Pathway,-Value),y=Value,fill=Col.Code))+geom_bar(stat='identity')

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • Beat me to it. But I had used `levels = stringr::str_sort(df1$Pathway, numeric = TRUE)` in the coercion to factor. – Rui Barradas Jul 11 '20 at 16:37
  • Sorry, I think I made my example data too simple. I called the pathways 'P1, P2...' etc but they actually have long character names which are super annoying. I have tried both of your codes and I think this is why they don't work :( Please can you help me out again? Sorry for not being more clear when I wrote the first post! – TobiasFirth Jul 11 '20 at 17:28
  • @TobiasFirth Nice, so could you add real values for pathway? I will add an update. – Duck Jul 11 '20 at 17:30
  • @Duck, Hi, I made the edits, sorry for not being clearer the first time around. Just while I'm here, is there a more efficient way to add example tables to the question without tying them out manually? Best wishes, Tobi – TobiasFirth Jul 11 '20 at 17:39
  • @TobiasFirth Updated now, please check if it helps! – Duck Jul 11 '20 at 17:42
  • @IanCampbell Yes, thanks! I thought it was the order of labels. Sorry! – Duck Jul 11 '20 at 17:51
1

One other approach is with fct_reorder from the forcats package:

library(forcats)
ggplot(data,aes(x=fct_reorder(Pathway,-Value),y=Value,fill=Col.Code)) + 
  geom_bar(stat='identity') + 
  labs(x = "Pathway")

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57