0

Just started working with R and followed a tutorial to create a waterfall plot. It worked with very well when using dichotomous values:

col <- ifelse(tumor_tidy_wp$zr == "YES", "#1B9E77", "#D95F02")

barplot(tumor_tidy_wp$maxchange, col=col, border="Black", space=0.5, ylim=c(-75,75),
      main = "Waterfall plot", ylab="Change from baseline (%)",
      cex.axis=1.2, cex.lab=1.4, legend.text=c("Yes","No"),
      args.legend=list(title="Uptake 89Zr on any time-point", fill=c("#1B9E77","#D95F02"), border=NA, cex=0.9)) +
      theme(axis.line.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
      axis.title.y = element_text(face="bold",angle=90))

Waterfallplot_dichotomous

I also have a continous scale of this value and in stead of YES/NO I would like to have a gradient instead, but I cannot get this to work. I tried to use versions of scale_fill() but failed. Is there another way?

r2evans
  • 141,215
  • 6
  • 77
  • 149
Iris
  • 3
  • 1
  • https://stackoverflow.com/editing-help. Note that `'''` (three single quotes) is not a code-fence, it's three backticks followed by a language refence, as in `\`\`\`lang-r`, all on its own line (followed by a newline). Closing the code-fence is just three backticks on their own line. Formatting code helps visualize the question for flow (text, code, text) and for making the code syntactically reasonable to read (newlines, indentation, etc). – r2evans May 02 '20 at 18:15
  • Are you able to provide a sufficient sample of your data? The best way is with `dput`, perhaps `dput(head(tumor_tidy_wp,10))`, recognize that that will produce a smaller graph (but still likely get the point across for you to apply to your whole frame). The key to this (and the onus is completely on you) is to provide a *representative* and *unambiguous* sample of your data. Copying what `tumor_tidy_wp` looks like on the console is occasionally or often insufficient (due to whitespace, `factor`s, etc), so using `dput` is often better. – r2evans May 02 '20 at 18:16
  • I've never seen an example that combines base graphics (`barplot`) with `ggplot2::theme` in this way. Are you certain that's doing what you intend for it to do? – r2evans May 02 '20 at 18:20
  • Sorry not really sure how/if i can share my data. It is a relatively simple data set with 40 obs with %change frome baseline and values for uptake on PET-scan between 0 and 10 (for the gradient). I used this tutorial: https://www.r-bloggers.com/waterfall-plots-what-and-how/ . Pretty sure the ggplot2 scale_file options are probably not right but dont know if I can fix what I have or if I should start over with a ggplot graph (but could not get the bars to work with ggplot yet) – Iris May 02 '20 at 18:29
  • See https://stackoverflow.com/q/5963269 for some context. But for "how", just run the command I gave you (`dput(head(tumor_tidy_wp,10))`) on your console, and then [edit] your question and paste it in there in a new code block. – r2evans May 02 '20 at 18:44
  • Other options are to (1) mimick your *need* using a base dataset such as `mtcars` or `iris` (potentially with simple manipulation); or (2) create one manually, either deterministically or stochastically (use `set.seed` to give us a fixed point of randomness), and using `data.frame(...)`. Just thoughts. – r2evans May 02 '20 at 18:50
  • 1
    I fixed it! I changed from using barplot to the ggplot + geom_bar + scale_fill_gradient2 like so ```ggplot(data=tumor_tidy_wp, aes(x=x, y=maxchange, fill=IA)) + geom_bar(stat="identity") + scale_fill_gradient2()``` – Iris May 02 '20 at 18:59
  • I don't think there is an easy fix for using gradient with the basic barplot function – Iris May 02 '20 at 19:00
  • I think you're right (about base `barplot`), but I hadn't tried hard yet. `ggplot2` does make some types of plotting and colors easier. You still likely have a delay on self-answering, but please come back to "answer" your own question then accept it. – r2evans May 02 '20 at 19:05

1 Answers1

0

Fixed it myself by changing from using barplot to the ggplot + geom_bar + scale_fill_gradient2 like so

ggplot(data=tumor_tidy_wp, aes(x=x, y=maxchange, fill=IA)) + geom_bar(stat="identity") + scale_fill_gradient() + theme_classic2() + expand_limits(y=c(-60, 60)) + ylab("Change from baseline(%)")

Waterfall plot with gradient

Iris
  • 3
  • 1