5

I am trying to make a ggplot2 Barplot with broken Y axis using the script provided HERE

My script

library(ggplot2)
        dat <- read.csv("expression",header=T, check.names = FALSE)
        dat
pd <- position_dodge(0.7) # move them .05 to the left and right


#Function to transform data to y positions
trans <- function(x){pmin(x,2) + 0.05*pmax(x-2,0)}

yticks <- c(0, 1, 2, 10,15,20,25,30)

#Transform the data onto the display scale
dat$mean_t <- trans(dat$value)
dat$sd_up_t <- trans(dat$value + dat$sd)
dat$sd_low_t <- pmax(trans(dat$value - dat$sd),1) #

png("test.png", units="in", family="Times",  width=6, height=4, res=300) #pointsize is font size| increase image size to see the key


ggplot(data=dat, aes(x=gene, y=mean_t, group=treatment, fill=treatment)) +
  geom_errorbar(aes(ymin=sd_low_t, ymax=sd_up_t),size=.5,  
                width=.2,
                position=position_dodge(0.7)) + 
   geom_col(position="dodge") +
  geom_rect(aes(xmin=0, xmax=4, ymin=2.2, ymax=2.3), fill="white") +
  scale_y_continuous(limits=c(0,NA),expand = c(0,0), breaks=trans(yticks), labels=yticks) +
  labs(y="Relative titer of CLas")+
      theme_classic()    

My data looks like this

gene,treatment,value,sd
tar1,EV,1,0.1
tar1,OX6,25.4,3
tar1,OX102,18.3,3
tar2,EV,1,0.1
tar2,OX6,1.6,0.2
tar2,OX102,1.5,0.2
tar3,EV,1,0.1
tar3,OX6,3.98,0.3
tar3,OX102,1.7,0.1

My plot enter image description here

I have been running through 3 problems

  1. Set maximum on the y-axis is not working.
  2. My error bars are not properly placed.
  3. My y-axis is not covered with white geom_rect

Thanks for your help.

pali
  • 195
  • 2
  • 14

0 Answers0