Fran Braga
Fran Braga

Reputation: 197

ymax and ymin values (in error-bar) does not appear in geompoint

I am trying to plot a chart with the mean, maximum and minimum value of 4 variables (amount, exploration, size, source), but the lines referring to ymax and ymin (error-bar) do not appear. These 4 variable are repeated because it appear for marine and freshwater data.

I also would like to invert the graph axes by placing the variables names in the column est on the y axis and the mean values on the x axis.

Does anyone know the error of my script?

Dataset<-read.csv(file= "ICglm.csv", header= TRUE, sep= ";" )
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_errorbar(aes(ymin=min, ymax=max), width=0, color="black") + 
  geom_point(aes(size=1)) 
#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_classic() + xlab("Levels") + ylab("confident interval")



#To put levels on y axis you just need to use coord_flip()
p <- p+ coord_flip
est         	min         	max           	mean    	origen
amount	    -0.108911212	-0.100556517	-0.104733865	freshwater
exploration	0.191367902 	0.20873976  	0.200053831	   freshwater
size	    0.003166273 	0.003276336	    0.003221305	   freshwater
source	    -0.241657983	-0.225174165	-0.233416074   freshwater
amount    	0.07	        0.08	         0.075      	marine
exploration	0.33	        0.34          	 0.335	        marine
size	    0.01	        0.01	         0.01	        marine
source    -1.95         	-1.9        	-1.925	        marine

enter image description here

Upvotes: 0

Views: 892

Answers (1)

ophdlv
ophdlv

Reputation: 254

In your code, there is width=0 in geom_errorbar that is why you can not see error bars. In addition, you should write coord_flip(). With these modifications, your code should work :

ggplot(Dataset,aes(x=est,ymin=min, ymax=max, y=mean, shape=est)) +
  geom_errorbar(aes(ymin=min, ymax=max), color="black") + 
  geom_point(aes(size=1)) +
  guides(size=FALSE,shape=FALSE) + scale_shape_manual(values=c(20, 20, 20, 20)) +
  theme_classic() + xlab("Levels") + ylab("confident interval") +
  coord_flip()

However, instead of geom_errorbar, you can use its rotated version geom_errorbarh. Thus, no need to invert axes and est variable can be directly indicated as y-axe.

ggplot(aes(mean, est, label = origen), data=Dataset) +
  geom_point() +
  ggrepel::geom_text_repel() +
  geom_errorbarh(aes(xmin=min, xmax=max)) + 
  theme_classic() + 
  xlab("confident interval") +
  ylab("Levels")

enter image description here

Upvotes: 1

Related Questions