首页 > 解决方案 > ymax 和 ymin 值(在错误栏中)不会出现在 geompoint 中

问题描述

我正在尝试绘制一个包含 4 个变量(数量、探索、大小、来源)的平均值、最大值和最小值的图表,但没有出现引用 ymax 和 ymin(误差条)的线。这 4 个变量是重复的,因为它出现在海洋和淡水数据中。

我还想通过将变量名称放在 y 轴上的列 est 和 x 轴上的平均值来反转图形轴。

有谁知道我的脚本的错误?

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

在此处输入图像描述

标签: rggplot2errorbar

解决方案


在您的代码width=0中,geom_errorbar这就是您看不到错误栏的原因。另外,你应该写coord_flip(). 通过这些修改,您的代码应该可以工作:

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

但是,geom_errorbar您可以使用其旋转版本来代替geom_errorbarh. 因此,不需要反转轴,est变量可以直接表示为y轴。

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

在此处输入图像描述


推荐阅读