简体   繁体   English

在R中绘制时Y轴比例误差

[英]Error in Y axis scale when plotting in R

I have attached a screenshot of the problem im facing. 我已附上我面临的问题的屏幕截图。 该死的看那个石

Basically my Y axis values are all between -1 and 1 as can be seen the command line for the most part but in the plot, y appears to be between 0-800. 基本上我的Y轴值都在-1和1之间,这在大多数情况下可以从命令行看到,但是在图中,y似乎在0-800之间。

Im very new to R, I appreciate any help I can get. 我对R非常陌生,我很感谢我能提供的任何帮助。

Thank you! 谢谢!

I'm not sure how you loaded the data in, but I am guessing that your values where assumed to be strings and converted to factors. 我不确定如何加载数据,但是我猜测您的值假定为字符串并转换为因子。

Regardless of how it happened, what is causing your issue is that your column is a factor . 无论如何发生,导致您的问题的原因都在于您的专栏是一个factor The giveaway is the final line of output: "804 Levels: ..... " . 赠品是输出的最后一行: "804 Levels: ..... " Anytime you see that "XXX Levels: ..." statement, you know your data is being stored as factors. 每当您看到“ XXX Levels:...”语句时,就知道您的数据被存储为因素。

To convert to numbers, first convert to strings, then use as.numeric: 要转换为数字,请首先转换为字符串,然后使用as.numeric:

`mseries[, 2] <- as.numeric(as.character(mseries[, 2]))` 
 # then plot again

Factors in R are stored as integers with a mapping from each integer to a string. R中的因子存储为整数,并具有从每个整数到字符串的映射。 So what you are seeing in your Y-axis is the underlying numeric representation. 因此,您在Y轴上看到的是基本的数字表示形式。 Incidentally, this is also the reason that you first need to convert to string; 顺便说一句,这也是您首先需要转换为字符串的原因。 if you convert a factor to numeric, the number you will receive is the underlying integer value of the factor mapping. 如果将因子转换为数字,则将收到的数字是因子映射的基础整数值。

While this may appear confusing for the context in question, for appropriate usage this is extremely practical. 尽管这对于所涉及的上下文可能会造成混淆,但是对于适当的用法,这是极其实用的。 For example, for a variable indicating Male / Female. 例如,对于指示“男性/女性”的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM