简体   繁体   中英

Staked histogram with two data frames, one positive and another negative in R

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.

在此处输入图片说明

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM