2017-08-31 88 views
0

此代碼將X變量繪製爲< 8,10-12,12-20,20+然後8-10。 爲什麼「8-10」出現故障?謝謝。ggplot2 X變量的順序不正確

tst1 <- data.frame(
len = c(1.00, 1.038219, 1.040130, 1.078980, 1.186618), 
pm25=c("<8", "8-10","10-12", "12-20","20+")) 

plottst1 <- ggplot(tst1, aes(x=factor(pm25), y=len)) 
plottst1 

回答

0

你應該使用一個有序factor

mylevels <-c("<8", "8-10","10-12", "12-20","20+") 
tst1 <- data.frame(
    len = c(1.00, 1.038219, 1.040130, 1.078980, 1.186618), 
    pm25=factor(mylevels, levels=mylevels, ordered =T)) 


plottst1 <- ggplot(tst1, aes(x=pm25, y=len)) 
plottst1