简体   繁体   中英

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.

Im very new to R, I appreciate any help I can get.

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 . The giveaway is the final line of output: "804 Levels: ..... " . Anytime you see that "XXX Levels: ..." statement, you know your data is being stored as factors.

To convert to numbers, first convert to strings, then use 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. So what you are seeing in your Y-axis is the underlying numeric representation. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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