Home > Back-end >  how to make the y-axis value in order when using ggplot2 in R language
how to make the y-axis value in order when using ggplot2 in R language

Time:07-12

I was trying to use ggplot2 to plot multiple lines in a window. 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: enter image description here the result figure is this: enter image description here

the problem is that y-axis in the result figure is not in order. can you please help me to figure out? Thanks!

CodePudding user response:

you should try this (with 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. The first part of the code should transform it in one.

Tell me if it works!

  • Related