我有一段时间内国家/地区级别的统计数据。我过去常常facet_wrap()按国家/地区绘图,但我想仅根据最新值(2015 年)按降序排列绘图。我尝试使用transform()但仅对第一个值(2005)进行排序。我认为forcats::fct_reorder()可以让我到达那里,但我没有成功地插入 的任何论点facet_wrap()。这可能吗?grid_arrange() 如果可能的话,我不想按照这个问题的建议去做。
Country <- c('a', 'b', 'c', 'x', 'y', 'z')
`2005` <- c(500, 700, 600, 900, 800, 1000)
`2010` <- c(900, 600, 800, 1000, 500, 700)
`2015` <- c(1000, 900, 500, 800, 700, 600)
df1 <- data.frame(Country, `2005`, `2010`, `2015`)
gather1 <- gather(df1, "Year", myValue, 2:4)
gg1 <- ggplot() +
geom_line(data = gather1, aes(x = Year, y = myValue, group = Country), size = 1.5, color = "orange") +
facet_wrap(~ Country, ncol = 3) +
theme(aspect.ratio = (35/50))
gg1
Run Code Online (Sandbox Code Playgroud)
干得好!
gather2 <- df1 %>%
mutate(Country=fct_reorder(Country, `2015`)) %>%
gather("Year", myValue, 2:4)
gg2 <- ggplot() +
geom_line(data = gather2, aes(x = Year, y = myValue, group = Country), size = 1.5, color = "orange") +
facet_wrap(~ Country, ncol = 3) +
theme(aspect.ratio = (35/50))
gg2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2643 次 |
| 最近记录: |