R 没有按顺序显示我的订购因素?

问题描述 投票:0回答:1

Picture of Resulted Chart我正在尝试编写一个代码,用于获取 PM2.5 浓度数据表,计算 AQI,将其放入表格中,然后绘制结果。

无论我做什么,即使将 AQI_category 作为有序因素,它仍然会在“敏感群体不健康”之前显示“不健康”,尽管在 AQI 量表上“UFSG”应该出现在“不健康”之前。

这是代码:

#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")

我总是收到随附的图表。有任何想法吗?预先感谢!

此外,此代码始终对级别排序错误:

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" ))))))

它给出: “级别:良好 中等 不健康 不健康,适合敏感群体”

所以看来我的订购因素订购错误,我不知道为什么?

尝试将 AQI 类别作为有序因素,希望能够将它们按正确的顺序排列,但它始终将 UFSG 置于不健康之前

categories orders factors
1个回答
0
投票

我认为您不小心使用了向量

AQI
AQI_Category
而不是数据框列
AQI_Table$AQI
AQI_Table$AQI_Category
。这可以通过一行代码来修复,将
data
参数添加到绘图中。

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

可以通过考虑好的变量名称来避免这种情况。像

aqi$category
这样的名字会是
AQI_Table$AQI_Category
更好的名字。

© www.soinside.com 2019 - 2024. All rights reserved.