ggplot: Beware of Factors!

code
beginner
r
tips
Author

Daniel Kick

Published

May 6, 2020

Don’t get suckered when converting factors! Numeric data can be assigned to a factor type which will throw a wrench in a plot or analysis (1/3) image (2).png

A knee jerk reaction would be to convert it to a numeric with as.numeric(). That doesn’t work either. (2/3) image (3).png

However if you use as.numeric(as.character()) then it works. That’s because factors are ordinal and named so if you convert the type to character first to ensure R is working with the factor names instead of the ranks. (3/3) image (4).png