我有一个包含航空公司名称的图表,我想按字母顺序对它们进行排序(颠倒当前顺序)。
这是我的代码。 我尝试创建一个排序的列,然后绘制它,但是没有用。
#joining the two files
joinflights<-merge(delayed_flights,total_flights,by='airline')%>%
mutate(sorted_airline = fct_relevel(airline, "AirTran Airways Corporation","Alaska Airlines Inc.","American Airlines Inc.","Delta Air Lines Inc.",
"Endeavor Air Inc.","Envoy Air","ExpressJet Airlines Inc.","Frontier Airlines Inc.","Hawaiian Airlines Inc.",
"JetBlue Airways","Mesa Airlines Inc.","SkyWest Airlines Inc.","Southwest Airlines Co.","United Air Lines Inc.",
"US Airways Inc.","Virgin America"))
#creating a new variable to calculate the delay rate per airline
#creating a bar chart
airlines<-ggplot(joinflights%>%mutate(delay_rate=(count.x/count.y))
,aes(x= sorted_airline,y=delay_rate
))+
#adding a title and renaming the axises
labs(title="Flights Delay Rates by Airline",
x="Airline",y="Flights Delay Rate")+
#displayin y axis in percentage
scale_y_continuous(labels = percent)+
#adding a bar chart
geom_bar(stat = "identity")+
#flipping the graph to a horizntal view
coord_flip()
谢谢
答案 0 :(得分:0)
joinflights <- joinflights %>%
dplyr::mutate(sorted_airline = forcats::fct_relevel(sorted_airline, sort))
ggplot(...)
答案 1 :(得分:0)
尝试反转级别:
joinflights <- merge(delayed_flights, total_flights, by = 'airline') %>%
mutate(sorted_airline = fct_relevel(airline, rev(unique(airline))
答案 2 :(得分:0)
airlines <- ggplot(joinflights %>% mutate(delay_rate=(count.x/count.y)),
aes(x= forcats::fct_rev(sorted_airline),y=delay_rate))+...