首页 > 解决方案 > RI 有一个已按频率降序排列的数据框我如何让 ggplot 以降序显示频率,而是按列名称显示

问题描述

我有一个带有 station_name 的数据框。我已按 station_name 频率对数据帧进行了排序。这里有一些额外的调试代码。

```{r}
   df <- filter(df_clean_distances, start_station_name != "NA" )
   d <-df %>% select( start_station_name) %>%
   group_by(start_station_name) %>%
   summarize( freq = n())
   head( d$freq )
   dput(head(d))
   d2 <- d[ order(-d$freq),]
   head( d2 )
   #plot freq
   ggplot(d2, aes( x=start_station_name, y= freq)) + 
       geom_bar( stat = "identity") +
       theme(axis.text.x = element_blank()) +
       ylim( c(0,35000))
```

我可以看到数据框是降序排列的,我什至添加了一个额外的 order() 以确保我是降序排列的。

```{r}
   structure(list(start_station_name = c("Streeter Dr & Grand Ave", 
   "Clark St & Elm St", "Lake Shore Dr & Monroe St", "Theater on the Lake", 
   "Lake Shore Dr & North Blvd", "Wells St & Concord Ln"), freq = c(35200L, 
   32266L, 29748L, 29561L, 26947L, 25053L)), row.names = c(NA, -6L
   ), class = c("tbl_df", "tbl", "data.frame"))
```

但是当我通过 ggplot 运行它时,我得到了这个

在此处输入图像描述

如何告诉 ggplot 保持降序,以便图表是从左到右平滑的降序图?

标签: rggplot2

解决方案


推荐阅读