1

So i have a dataframe that looks like this:

enter image description here

I am trying to plot this so that the 'cancers' are on the x axis and then the 'oddsRatio' and 'foldIncrease' are both y-axis parameters. Ideally I have a bar graph with oddsRatio and foldIncrease for each cancer next to each other, and being distinguished by color.

This is the closest I can get:

enter image description here

Using the following code:

cancers = c("All Cancers", "SCLC", "Lu SCC", "Lung AC", "Larynx", "Pharynx Oral", "Cavity", "Esophageal SCC", "Esophageal AC", "Bladder", "Liver", "Cervix", "Kidney", "Pancreas")
oddsRatio = c(1, 111.3, 103.5, 21.9, 13.2, 6.6, 4.2, 3.9, 3.9, 3.8, 2.9, 1.8, 1.7, 1.6)
foldIncrease = c(1.15464695360441, 0.858680717668245, 1.29986125563649, 4.56755645705811, 2.52922152922153, 0.818596228001855, 0.892133516869108, 1.04842410881178, 1.01791768793961, 1.1423932173087, 1.1871100629828, 0.857694671793762, 1.39573948596081, 1.33458517681876)

cancerData = data.frame(cancers, oddsRatio, foldIncrease)

par(mar = c(5,5,2,5))
with(cancerData, plot(cancers, oddsRatio, type="scatter", col="red3", 
             ylab='Odds Ratio',
             ylim=c(0,150)))

par(new = T)
with(cancerData, plot(cancers, foldIncrease, pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2))
axis(side = 4)
mtext(side = 4, line = 3, 'Fold Increase')

To clarify I am looking for this:

enter image description here

The Nightman
  • 5,609
  • 13
  • 41
  • 74

1 Answers1

0

Is this what you are wanting? (note, I used ggplot2 rather than base)

library(tidyverse)
tidy <- cancerData %>% 
    gather(stats, val, -cancers)

ggplot(tidy, aes(cancers, val)) + 
    geom_bar(aes(fill = stats), stat = "identity", position = "dodge")

enter image description here

Edit

Okay, so generally I don't think this is a great idea. See here for why. But if you really want to, the below should do it for you.

vars <- length(unique(cancerData$cancer))

par(mar = c(4, 4, 4, 6))
plot(1:vars, seq(0, 120, length = vars), 
    type = "n",
    main = "Odds Ratios and Fold Increase",
    bty = "n",
    xaxt = "n",
    xlab = "Cancers",
    ylab = "Odds Ratios")
rect(1:vars - 0.25, 0, 1:vars, cancerData$oddsRatio, col = "blue")
axis(1, at = 1:vars, labels = unique(cancerData$cancer))

par(new = TRUE)
plot(1:vars, seq(0, 5, length = vars), 
    type = "n", 
    xaxt = "n", 
    yaxt = "n",
    bty = "n",
    xlab = "",
    ylab = "")
rect(1:vars, 0, 1:vars + 0.25, cancerData$foldIncrease, col = "red")
axis(4)
mtext("Fold Increase", side = 4, line = 3)
legend("topright", 
    fill = c("blue", "red"), 
    legend = c("Odds Ratio", "Fold Increase"),
    box.lwd = 0)

enter image description here

Community
  • 1
  • 1
Daniel Anderson
  • 2,394
  • 13
  • 26
  • It is close, but the big change is that I want two y-axes, so I would like to be able to plot the fold increase on its own axis so that the bars aren't so small. – The Nightman Nov 14 '16 at 19:49