Search code examples
rggplot2data-visualizationconfidence-intervalerrorbar

confidence interval symbols refinement


The values of my confidence interval of variable SIZ are very close to zero, so when I plot the graph, the circle which represents the average value overlap the intercept line.

Would anyone knows how I can decrease the size of this circle or increase the X-axis scale refinement so that the line of the intercept is over 0.0000, so that there will be no overlap between it and the circle?

I also would like to fill the confidence interval symbols of negative values in red and positive values in blue. Do you know which code should I add in my script?

Thank you very much

   dput(read.table("clipboard",sep=";",header=TRUE))
library(ggplot2)

p <- ggplot(Dataset,aes(x=est,ymin=min, ymax=max, y=mean, shape=est))

#Added horizontal line at y=0, error bars to points and points with size two
p <- p + geom_hline(yintercept=0, size = I(1.1), color = I("red")) +
  geom_errorbar(aes(ymin=min, ymax=max), width=0, color="black") + 
  geom_point(aes(size=2)) 
#Removed legends and with scale_shape_manual point shapes set to 1 and 16
p <- p + guides(size=FALSE,shape=FALSE) + scale_shape_manual(values=c(20, 20, 20, 20))

#Changed appearance of plot (black and white theme) and x and y axis labels
p <- p + theme_light() + xlab("Levels") + ylab("confident interval")
#Final adjustments of plot
p <- p + theme(axis.text.x=element_text(size=rel(1.2)),
               axis.title.x=element_text(size=rel(1.3)),
               axis.text.y=element_text(size=rel(1.2)),
               panel.grid.minor=element_blank(),
               panel.grid.major=element_blank()) 

#To put levels on y axis you just need to use coord_flip()
p <- p+ coord_flip()
print(p)
est	min	max	mean
SOU	-1.988	-1.893	-1.9405
EXP	0.324809225	0.354699871	0.339754548
AMOU	0.078056746	0.08289443	0.080475588
SIZ	0.009487689	0.009808696	0.009648193

enter image description here

enter image description here


Solution

  • to decrease the point size you will have to set a smaller size param e.g. geom_point(size = 0.2). Alternativly you can also control the thickness of the hline. For the different colors of the error bar try geom_linerange(aes(ymin=min, ymax = mean), color="red") + geom_linerange(aes(ymin= mean, ymax=max), color="blue")

    p <- ggplot(Dataset,aes(x=est,ymin=min, ymax=max, y=mean, shape=est))
    
    #Added horizontal line at y=0, error bars to points and points with size two
    p <- p + geom_hline(yintercept=0, size = 0.2, color = 'red') +
     geom_linerange(aes(ymin=min, ymax = mean), color="red") + 
     geom_linerange(aes(ymin= mean, ymax=max), color="blue") + 
     geom_point(size=0.2)
    
    #Removed legends and with scale_shape_manual point shapes set to 1 and 16
    p <- p + guides(size=FALSE,shape=FALSE) + scale_shape_manual(values=c(20, 20, 20, 20))
    
    #Changed appearance of plot (black and white theme) and x and y axis labels
    p <- p + theme_light() + xlab("Levels") + ylab("confident interval")
    #Final adjustments of plot
    p <- p + theme(axis.text.x=element_text(size=rel(1.2)),
               axis.title.x=element_text(size=rel(1.3)),
               axis.text.y=element_text(size=rel(1.2)),
               panel.grid.minor=element_blank(),
               panel.grid.major=element_blank()) 
    
    #To put levels on y axis you just need to use coord_flip()
    p <- p+ coord_flip()
    p  
    

    enter image description here

    structure(list(est = c("SOU", "EXP", "AMOU", "SIZ"), min = c(-1.988, 
    0.324809225, 0.078056746, 0.009487689), max = c(-1.893, 0.354699871, 
    0.08289443, 0.009808696), mean = c(-1.9405, 0.339754548, 0.080475588, 
    0.009648193)), .Names = c("est", "min", "max", "mean"), row.names = c(NA, 
    -4L), class = c("tbl_df", "tbl", "data.frame"))