简体   繁体   English

在R语言中使用ggplot2时如何使y轴值按顺序排列

[英]how to make the y-axis value in order when using ggplot2 in R language

I was trying to use ggplot2 to plot multiple lines in a window.我试图使用 ggplot2 在窗口中绘制多行。 this is my code here:这是我的代码:

ggplot(type1_age, aes(x = Year, y = Value, group = Age, color = Age)) + geom_line()+ggtitle('The percentage distribution of type1 diabetes patients in different age groups')+ylab("percentage (%)")

type1_age file looks like this: type1_age 文件如下所示: 在此处输入图像描述 the result figure is this:结果图是这样的: 在此处输入图像描述

the problem is that y-axis in the result figure is not in order.问题是结果图中的 y 轴不按顺序排列。 can you please help me to figure out?你能帮我弄清楚吗? Thanks!谢谢!

you should try this (with tidyverse):你应该试试这个(使用 tidyverse):

type1_age %>%
mutate(Value = Value %>% as.character %>% as.numeric) %>% 
ggplot(aes(x = Year, y = Value, group = Age, color = Age)) +
geom_line()+
ggtitle('The percentage distribution of type1 diabetes patients in different age groups')+
ylab("percentage (%)")

I suspect you Value variable to be a character vector and not a numeric one.我怀疑你的 Value 变量是一个字符向量,而不是一个数字。 The first part of the code should transform it in one.代码的第一部分应将其转换为一个。

Tell me if it works!告诉我它是否有效!

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

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