Ordering bars in barplot()

前端 未结 2 1934
遥遥无期
遥遥无期 2021-01-21 08:32

I have a dataframe, and one of the variables (let\'s call it Q1) has several levels: \"No use\", \"30 min\", \"1 hour\", \"2 hours\", \"3+ hours\".

How can I plot a barp

2条回答
  •  既然无缘
    2021-01-21 09:04

    One possibility is to create a factor version of Q1 where you specify the levels in the desired order:

    df$Q1_fac <- factor(df$Q1, levels = c("30 min", "1 hour",  "2 hours", "3+ hours"))
    
    tt <- table(df$Q1_fac)
    tt
    # Q1_fac
    # 30 min   1 hour  2 hours 3+ hours 
    #      2        4        3        4    
    
    barplot(tt)
    

    enter image description here

提交回复
热议问题