0

I have a dataframe that looks like this:

df <- read.table(text = "range  count
<=30    2
30-40   6
50-60   8
>=60    5", header = T, sep="\t")

df

range count
<=30 2
30-40   6
50-60   8
>=60 5

I am trying to plot a line plot with ggplot2 with x axis having the order shown from top to bottom, but when I plot the graph , I get >=60 as the second entry on x axis. I want it as the last. What am I missing. Here is the code

ggplot(df, aes(x= range ,y=count, group = 1))+
  geom_line(color="red") +
  geom_point(color="black",fill = "red",shape=21)+
  xlab("range")+ylab("count (%)")+
  scale_y_continuous(limits =c(0,30),breaks = seq(0,30,5))
user3138373
  • 519
  • 1
  • 6
  • 18
  • 2
    You should either convert the `range` variable to a factor where the levels are in the order you want, or you should set the `limits` argument in `scale_x_discrete()`. – teunbrand Apr 27 '23 at 21:13
  • The package `forcats` has a lot of useful functions for converting your variable to a factor. – LMc Apr 27 '23 at 21:14
  • df$range = factor(c('<=30','30-40','50-60','>=60'), levels = c('<=30','30-40','50-60','>=60')) – dandrews Apr 27 '23 at 21:16

0 Answers0