2

I have a data set that looks like this:

samp.data <- structure(list(Track = c(1,1,1,1,1,1,1,1,2,2,2), 
    Base = c("A","C","B","A","D","D","C","A","A","B","B"), 
    Length = c(1,1,1,1,2,3,1,1,1,1,1)), 
    .Names = c("Track", "Base", "Length"), class = "data.frame",row.names = c(NA, 11L))

#    Track Base Length
# 1      1    A      1
# 2      1    C      1
# 3      1    B      1
# 4      1    A      1
# 5      1    D      2
# 6      1    D      3
# 7      1    C      1
# 8      1    A      1
# 9      2    A      1
# 10     2    B      1
# 11     2    B      1

I am trying to plot an unordered stacked bar, with Tracks on the x axis and Length on the y axis. In other words, the bar graph wouldn't group the A bases together and plot it as one length of 1+1+1+1=4. It would plot each base in order. First it would plot the A base of length 1 in Track 1, C base of length 1 above that, B base of length 1 above that, A base of length 1 above that, D base of length 2 above that, and so on.

Below is a crude ASCII diagram of what I am trying to describe:

  |       C
L |       Y
e |       Y          Key
n |       R          A = Red
g | B     B          B = Blue
t | B     G          C = Green
h | R     R          D = Yellow
  ----------
    2     1
     Track

Sorry if the explanation is a little confusing. Thank you for your help!

Edit: This question is different from the possible duplicate, because I would like to ungroup the stacked sections.

mathematical.coffee
  • 51,909
  • 10
  • 130
  • 180
alki
  • 2,835
  • 5
  • 17
  • 39
  • Edit: This question is different from the possible duplicate, because I would like to ungroup the stacked sections. – alki Jul 19 '15 at 20:58
  • Maybe you should perform the transformation before hand. If you think about it, a graphing package shouldn't have to perform any computations, but `ggplot2` does a small bit of work in some cases. – CinchBlue Jul 19 '15 at 21:01
  • To confirm: is the label for track 1 and 2 transposed in your chart? I expected track 1 to have a taller bar. – Ricky Jul 20 '15 at 02:56
  • Also there's an extra comma at the end of `Base` for your reproducible data, causing syntax error. – Ricky Jul 20 '15 at 02:58
  • @Ricky, Chani - I edited it after Ricky spottied it but before Chani could fix it. No worries, all fixed now – mathematical.coffee Jul 20 '15 at 03:03

1 Answers1

2

Just use geom_bar(stat='identity'), set your x to Track, your y to length - it all works out.

Note - I converted your Base to factor (makes sense), as well as your Track (also makes sense to me, but if you wish to keep it numeric that's fine. You may wish to add a + scale_x_discrete() then in order to have your tracks show up as whole numbers on the x axis).

samp.data$Base <- factor(samp.data$Base)
samp.data$Track <- factor(samp.data$Track)

ggplot(samp.data, aes(x=Track, y=Length, fill=Base)) +
  geom_bar(stat='identity') +
  scale_fill_manual(values=c('red', 'blue', 'green', 'yellow'))

The last line sets the colours as you please. If you wish to reverse the x axis order (so that your track 2 appears first), do + scale_x_reverse().

enter image description here

I do not know what you mean by "ungroup the base" in your question, but say you wanted to draw an outline around each "chunk" of DNA you could add (e.g.) colour="black" in the geom_bar (e.g. in track 1, there is a D of length 2 immediately followed by a D of length 3 so it's drawn as a big D of length 5 - adding colour="black" outlines the 2-chunk separately to the 3-chunk though they still have the same colour).

mathematical.coffee
  • 51,909
  • 10
  • 130
  • 180