r - 小数字的 Y 轴缩放问题

标签 r ggplot2 autoscaling

我对小于 1e-10 的数字的 y 缩放有问题:它们都出现在同一条水平线上。

这是一个可重现的示例:

file <- structure(list(I = c(-7.254574e-11, -5.649333e-11, -5.015416e-11, 
-4.228137e-11, -3.287486e-11, -2.714915e-11, -2.203692e-11, -1.784489e-11, 
-1.150574e-11, -1.058553e-11, -6.189018e-12, -3.735149e-12, -2.303724e-12, 
6.610914e-13, 1.274374e-12, -3.610768e-13, 5.465134e-12, 6.691699e-12, 
8.020478e-12, 1.139353e-11, 1.537988e-11, 1.926399e-11, 2.130825e-11, 
2.45791e-11, 3.204071e-11, 3.582262e-11, 4.287535e-11, 4.624839e-11, 
5.16657e-11, 6.035387e-11), V = c(-2, -1.867, -1.733, -1.6, -1.467, 
-1.333, -1.2, -1.067, -0.933, -0.8, -0.667, -0.533, -0.4, -0.267, 
-0.133, 0, 0.133, 0.267, 0.4, 0.533, 0.667, 0.8, 0.933, 1.067, 
1.2, 1.333, 1.467, 1.6, 1.733, 1.867)), .Names = c("I", "V"), class = "data.frame", row.names = c(NA, 
-30L))

plot(file$V,file$I)

gg <- ggplot(file,aes(x = V,y=I))
print(gg + geom_point())

如您所见,使用 base plot 函数可以正确显示点,而使用 ggplot2 它们显示在水平线上。

我在 2011 年 5 月的邮件列表上看到了一个类似的帖子,Hadley 回答说它可以与 ggplot2 的开发版本一起使用。但是,使用下面描述的 R 版本我仍然收到错误。
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i686-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8       
 [4] LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tikzDevice_0.6.2 filehash_2.2-1   scales_0.2.0     plyr_1.7.1       reshape2_1.2.1  
[6] ggplot2_0.9.0   

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.0        MASS_7.3-18       
 [6] memoise_0.1        munsell_0.3        proto_0.3-9.2      RColorBrewer_1.0-5 stringr_0.6       
[11] tools_2.15.0 

任何人都有线索?

先感谢您 !
蒂博·鲁埃勒

最佳答案

正如布赖恩·迪格斯 (Brian Diggs) 在回复 a related post that @joran pointed to above 时所评论的那样, 问题是由函数 scales::zero_range() 引起的.它使用 all.equal()测试最小和最大 y 值是否在 tolerance = .Machine$double.eps ^ 0.5 = 1.490116e-08 内彼此的;如果是,则使用示例中使用的“零刻度”绘制数据。

作为临时修复,您可以使用 fixInNamespace()删除 zero_range() 的违规位.

library(scales)  ## (The scales package needs to be on your search path)
fixInNamespace("zero_range", pos="package:scales")

fixInNamespace() 推出的编辑器中, 替换 zero_range() 的定义:
function (x) 
{
    length(x) == 1 || isTRUE(all.equal(x[1] - x[2], 0))
}

使用这个(确保保存编辑后的版本):
function (x) 
{
    length(x) == 1 
}

您提供的代码然后运行得很好:

enter image description here

关于r - 小数字的 Y 轴缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900809/

相关文章:

r - 如何以较小的 R 大小将 ggplots 保存为 PDF?

r - 发散条形图 - 添加文本标签根据它是负数还是正数进行调整

docker - Kubernetes HPA为自定义指标获取了错误的当前值

kubernetes - Kubernetes Horizo​​ntal Pod Autoscaler 中的 Pod 副本缩减是如何工作的?

azure - 具有服务结构集群自动缩放功能的 VMSS

r - 如何绘制网络度

r - 计算概率后缀树中上下文状态关系的提升?

r - ggplot geom_bar() 按线型和填充自定义

R:ggplot2 图与 ggupset 和 hrbrthemes::theme_upsum():多余的 "at"出现在 x 轴标签处

r - file.choose() 自定义对话窗口