I am trying to make a plot in R in ggplot2 where the X axis is numbers as characters. (As if they were A,B,C etc...) but since they are considered character values the numbers plot in the order 1,10,11....2,20... etc instead of 1,2,3... I was wondering if there was a way to keep the numbers in character class while making them also be in numerical order. Thank you!
我试图在ggplot2中的R中绘制一个图,其中X轴是数字作为字符。 (好像它们是A,B,C等......)但是因为它们被认为是字符值,所以数字按照顺序1,10,11 ...... 2,20 ......而不是1,2, 3 ...我想知道是否有办法将数字保持在字符类中,同时使它们也按数字顺序排列。谢谢!
1 个解决方案
#1
1
Can be done via two class "casts":
可以通过两个类“演员”来完成:
> a = c("1","20","10","11","3")
> b = as.character(sort(as.numeric(a)))
> class(b)
[1] "character"
> b
[1] "1" "3" "10" "11" "20"
#1
1
Can be done via two class "casts":
可以通过两个类“演员”来完成:
> a = c("1","20","10","11","3")
> b = as.character(sort(as.numeric(a)))
> class(b)
[1] "character"
> b
[1] "1" "3" "10" "11" "20"