0

Given that I have two data frames, how can I plot this. I understand i can reverse the y scale but unable to get the right plot this way.

#Create dataframe 1
patientA <- c("treatment1", "treatment2", "treatment3", "treatment4", "treatment5", "treatment6", "treatment7")
x1.value   <- as.numeric(c(1, 34, 48, 24, 10, 9, 18))
df.1 <- data.frame(patientA, x1.value)

#Create dataframe 2
patientB <- c("treatment1", "treatment2", "treatment3", "treatment4", "treatment5", "treatment6", "treatment7")
x2.value   <- as.numeric(c(1, 34, 48, 24, 10, 9, 18))
df.2 <- data.frame(patientA, x2.value)

My current scripts, use the following method

bp_1 <- ggplot(new.df.1, aes(x=Treatment ,y=Number,fill=substring(Factors,1)))+ 
  geom_histogram(stat="identity",position="dodge") + scale_y_continuous(labels = scales::comma) 
bp_2 <- ggplot(new.df.2, aes(x=Treatment ,y=Number,fill=substring(Factors,1)))+  
  geom_histogram(stat="identity",position="dodge") + scale_y_continuous(labels = scales::comma)

But i can do one individual plots herein. A desired image is show below. Any help is much appreciated.

enter image description here

user44552
  • 153
  • 1
  • 10
  • What are `new.df.1` and `new.df.2`? – user3640617 May 12 '17 at 14:47
  • new.df.1 and new.df.2 are equivalent to df.1 and df.2 as given in the example. Apologies, I just created a dummy data frame to state the obvious problem. My real data set is much larger. – user44552 May 12 '17 at 14:50
  • I agree the histogram does not have negative values. i just want to show negative y scale since in biological terms df.1 is up-regulated and df.2 is down-regulated; hence want to show a figure that would be a functional representation for dummies like me. I think what i want is geom_bar. – user44552 May 12 '17 at 14:54
  • Check [ggplot2 and a Stacked Bar Chart with Negative Values](http://stackoverflow.com/questions/13734368/ggplot2-and-a-stacked-bar-chart-with-negative-values) – Henrik May 12 '17 at 14:57

1 Answers1

0

You could do something like this...

df <- merge(df.1,df.2,by.x = "patientA",by.y = "patientB")
bp_1 <- ggplot(df) + 
        geom_bar(aes(x=patientA,y=x1.value),fill="blue",stat="identity",position="dodge") + 
        geom_bar(aes(x=patientA,y=-x2.value),fill="red",stat="identity",position="dodge")
Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32