r - 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
解决方案
在您的代码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")
推荐阅读
- python - python numpy - 执行向量和矩阵加法的函数
- c++ - 作为一名学生,我该如何改进糟糕的代码?
- django - 两个模型之间的Django关系
- sql - T-SQL 在更大的 SELECT 语句中创建变量?有更快的方法吗?
- python - 无法在 Pipenv 上切换 python 版本
- c# - C# 根据字符串列表对字典进行排序?
- r - 如何在不插入点的情况下制作 shadow_mark?
- java - 具有相同数据的多个 JComboBoxes,每个项目可选择一次
- python - 拟合 RandomForest 模型但出现 Pandas 错误
- python - utcnow的字符串格式