rcategoriesfactors

R not displaying my ordered factor in order?


Picture of Resulted Chart

I am trying to make a code for taking a table of concentration data for PM2.5, calculate the AQI, put it in a table, and then plot the results.

No matter what I do, even making the AQI_category as an ordered factor, it keeps displaying "Unhealthy" before "Unhealthy for Sensitive Groups" even though "UFSG" should come before "Unhealthy" on the AQI scale.

Here is the code:

#concentration data in ug/m3
pm2.5_ugm3<- c(10.6,12.7,35.1, 2.5, 35.5,14.9, 20.2,1.3,6 ,17.4 ,40.1,60,19,5.5,3.8,10,9.9 ,4.3,36,15 ,3,7.2,16.4,26.5,1,2.5,18.8,19.2,14,3,12)

#calculate AQIs  
result = ifelse((pm2.5_ugm3>=0 & pm2.5_ugm3<=12), 50/12*(pm2.5_ugm3-0)+0, 
                ifelse((pm2.5_ugm3>=12.1 & pm2.5_ugm3<=35.4),(100-51)/(35.4-12.1)*(pm2.5_ugm3-12.1)+51, 
                       ifelse((pm2.5_ugm3>=35.5 & pm2.5_ugm3<=55.4), (150-101)/(55.4-35.5)*(pm2.5_ugm3-35.5)+101, 
                              ifelse((pm2.5_ugm3>=55.5 & pm2.5_ugm3<=150.4), (200-151)/(150.4-55.5)*(pm2.5_ugm3-55.5)+151,"NA" ))))

#create AQI object that rounds result to 0 digits
AQI <- round(as.numeric(result), digits = 0)

#calculate AQI result meaning
AQI_Category <- (ifelse((AQI>=0 & AQI<=50), "Good", 
                                 ifelse((AQI>=51 & AQI<=100),"Moderate", 
                                        ifelse((AQI>=101 & AQI<=150), "Unhealthy for Sensitive Groups", 
                                               ifelse((AQI>=151 & AQI<=200), "Unhealthy","NA" )))))
str(AQI_Category)


#make a table
AQI_Table <- data.frame(pm2.5_ugm3,AQI,AQI_Category)

#orders AQI category by magnitude
AQI_Table$AQI_Category <- as.factor(ordered(AQI_Table$AQI_Category, levels =c('Good','Moderate','Unhealthy for Sensitive Groups','Unhealthy')))
str(AQI_Table)



#stripchart & boxplot

stripchart(AQI ~ AQI_Category, vertical= TRUE, method = "jitter",
           pch = 16,
           col = c('forestgreen','goldenrod','orange1','firebrick'),
           main = "January 2024 AQI")

and I always get the attached chart. any ideas? Thanks in Advance!

additionally this code keeps ordering the levels wrong:

AQI_Category <- as.factor(ordered(ifelse((AQI>=0 & AQI<=50), "Good", 
                       ifelse((AQI>=51 & AQI<=100),"Moderate", 
                              ifelse((AQI>=101 & AQI<=150), "Unhealthy for Sensitive Groups", 
                                     ifelse((AQI>=151 & AQI<=200), "Unhealthy","NA" ))))))

it gives: "Levels: Good Moderate Unhealthy Unhealthy for Sensitive Groups"

So it seems my ordered factor is ordering wrong and I don't know why?

tried making AQI category as an ordered factor, expected it to put them in the right order, it keeps putting UFSG before Unhealthy


Solution

  • I think you accidentally used the vectors AQI and AQI_Category instead of the data frame columns AQI_Table$AQI and AQI_Table$AQI_Category. This can be fixed with one line of code, adding the data argument to the plot.

    stripchart(AQI ~ AQI_Category, vertical= TRUE, method = "jitter",
               pch = 16,
               col = c('forestgreen','goldenrod','orange1','firebrick'),
               main = "January 2024 AQI",
               data = AQI_Table)
    

    output

    This can be prevented by thinking about good variable names. Something like aqi$category would be a better name for AQI_Table$AQI_Category.