0

I have some data like this:

myd <- structure(list(var1 = structure(1:4, .Label = c("II", "III", 
       "IV", "V"), class = "factor"), zero_co = c(15.15152, 3.030303, 
        0, 0), non_zero_CO = c(84.84848, 96.969697, 100, 100), size = c(230, 
        813, 317, 1532)), .Names = c("var1", "zero_co", "non_zero_CO", 
        "size"), row.names = c(NA, -4L), class = "data.frame")

# myd
# I                   II         III   IV     V  
# zero_co       15.15152    3.030303    0     0
# non-zero CO   84.84848   96.969697  100   100
# size         230.00000  813.000000  317  1532

I want to plot size on the y-axis and other two variables zero_co and non-zero CO as a stacked bars on the x-axis. I am trying to plot this using gplots and ggplots but finding difficulties. How do I plot this?

Carl
  • 4,232
  • 2
  • 12
  • 24
PoisonAlien
  • 431
  • 4
  • 13
  • 2
    your data is absolutely unreadable. Learn how to make reproducible example in R: http://stackoverflow.com/q/5963269/684229 – Tomas Jan 08 '13 at 12:49
  • sorry for that.. I have edited it now. – PoisonAlien Jan 08 '13 at 12:50
  • Can you show what you did ? – SHRram Jan 08 '13 at 12:50
  • basically I tried following this post: http://stackoverflow.com/questions/6999144/how-do-you-create-a-bar-plot-for-two-variables-mirrored-across-the-x-axis-in-r – PoisonAlien Jan 08 '13 at 12:52
  • 2
    I mean what is your own code (adopted from the post) and error message if you got any ! – SHRram Jan 08 '13 at 12:55
  • I am not any expert in R, and from that post I tried changing variables and column names but its giving error as invalid subscript type. Please be nice am still a beginner. – PoisonAlien Jan 08 '13 at 12:59
  • Don't worry, I think your question is perfectly valid and brushed up the markup a bit to make it easier to read. It would indeed help though if you added your own code so that we have something to start working from. – Backlin Jan 08 '13 at 13:06
  • thank you Backlin. But I really tried to plot it on my own and finally posted it here. – PoisonAlien Jan 08 '13 at 13:11
  • I have a hard time figuring out how you want the final plot to look though. A barplot can only show one variable or groupings per axis. I take it that you want `size` te be plotted on `y` but fail to see how `zero_co` and `non-zero_CO` fit in. They appear to be continuous variables rather than groupings. So to clarify please provide either your non-working code or a mock-up picture of what you want to create. – Backlin Jan 08 '13 at 13:30
  • Hi backlin - here is what actually what I want to do. as you said zero_co and non-zero_co doesn't fit in. what I wanted to show is, how zero_co and non-zero_co are related to length (of chromosome) variable. So I thought if put two variables as a stacked bar and show length on y-axis, it would clear it up. But it was a dumb idea . Is there any better way to achieve this ?? And I counldn't find no mock-up picture, sorry. – PoisonAlien Jan 08 '13 at 13:48
  • @poisonAlien I do not intend not to be nice, getting help is much faster if you can provide whatever you are trying, I am not expert either ... otherwise people have hardtime to understand what is the problem and what you intend to do ... – shNIL Jan 08 '13 at 14:58

3 Answers3

4

Here is a solution if I understand it correctly. As you have quantative variable at both x and y axis you can not do with bar plot. You need to use rectangle (look like bar anyway).

myd <- data.frame (var1 = c("II", "III", "IV", "V"), zero_co = c(15.15152 , 3.030303,    0,     0),
             non_zero_CO = c(84.84848,   96.969697,  100,   100),
              size = c(230.00000,  813.000000,  317,  1532))

    require(ggplot2)

ggplot(myd) + geom_rect(aes(xmin = 0, xmax = zero_co, ymin =size , ymax =size + 80 ), fill = "lightgreen") +
geom_rect(aes(xmin = zero_co, xmax = zero_co + non_zero_CO, ymin =size , ymax =size + 80 ), fill = "darkblue") + theme_bw()

Give you the plot:

enter image description here

jon
  • 11,186
  • 19
  • 80
  • 132
3

Here is where I could get to based on my limited understanding:

myd <- data.frame (var1 = c("II", "III", "IV", "V"), zero_co = c(15.15152 , 3.030303,    0,     0),
             non_zero_CO = c(84.84848,   96.969697,  100,   100),
              size = c(230.00000,  813.000000,  317,  1532))
myd1 <- as.matrix (t(myd[,2:3]))

barplot(myd1)

enter image description here

shNIL
  • 963
  • 10
  • 24
1

I'm not sure how you wish the final plot to look like but here's a ggplot2 proposal.

First, reshape the data into the long format:

library(reshape2)
myd_long <- melt(myd, measure.vars = c("zero_co", "non_zero_CO"))

Calculate absolute value (I suppose value represents the percentage of size.):

myd_long <- within(myd_long, valueAbs <- size * value / 100)

Plot:

library(ggplot2)    

ggplot(myd_long, aes(y = valueAbs, x = var1, fill = variable)) +
  geom_bar(stat = "identity")

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168