0

I am working on generating a tornado plot in R. I am using ggplot2 package with code like the following:

dat <- structure(list(variable = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("# of nodes needed", 
"# of nodes owned", "cost per node"), class = "factor"), Level = structure(c(2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("high", "low"), class = "factor"), 
value = c(-275, -550, -50, 825, 275, 450)), .Names = c("variable", 
"Level", "value"), row.names = c(NA, -6L), class = "data.frame")

ggplot(dat, aes(fill=Level,variable,value )) +
    geom_bar(position = 'identity',stat = 'identity') + coord_flip()

I am curious as to how to change x-axis origin. Right now, the origin is automatically set to zero, and I want to be able to change it to a variable specified numeric value.

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
Dom
  • 187
  • 3
  • 14
  • 1
    Did you try `?xlim`? – Heroka Nov 30 '15 at 14:03
  • When using xlim I get error: discrete value supplied to continuous scale – Dom Nov 30 '15 at 14:05
  • 1
    Try to make your example reproducible. Currently, I have no idea what is in 'result'. – Heroka Nov 30 '15 at 14:08
  • Added in result code, it is a data frame calling cells in an example excel sheet i created – Dom Nov 30 '15 at 14:11
  • 2
    Please read this: [reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The easiest way is to copy-paste the output of `dput(dat)` to your question. – Heroka Nov 30 '15 at 14:13
  • Added output to question – Dom Nov 30 '15 at 14:24
  • 1
    And what exactly is wrong with the current output? What do you mean with 'change x-axis origin to a value'? What is your expected output? (If it's hard to explain, you may also past an image of a hand-drawn sketch/chart). – Heroka Nov 30 '15 at 14:34
  • Right now the x-axis origin is 0, but I want it to be a value in 'result' (in this case, output or $550) – Dom Nov 30 '15 at 14:37
  • 1
    Maybe you can change the labels? – Heroka Nov 30 '15 at 14:42
  • 1
    is `dat$value2 <- dat$value - 550 ; ggplot(dat, aes(fill=Level,variable,value2 )) + geom_bar(position = 'identity',stat = 'identity') + coord_flip()` closer to what you want ? – Cath Nov 30 '15 at 14:48

1 Answers1

2

Not sure if you are still looking for an answer but I was just solving a similar problem. I used limitsand expand in scale_x_continuous.

So I guess for you it would look something like this:

ggplot(dat, aes(fill=Level,variable,value )) +
geom_bar(position = 'identity',stat = 'identity') + 
scale_x_continuous(limits = c(2, 32), expand = c(0, 0))

except making limits = c(2,32) be whatever you want the limits of the x axis to be. Means you have to set this manually, but the best work around I came up with doing the same thing.

Sam
  • 68
  • 7