[英]Ordering a variable by a specific year in ggplot bar chart R
I have a question related to ordering specific values of a bar chart created with ggplot.我有一个关于对使用 ggplot 创建的条形图的特定值进行排序的问题。
My data "df" is the following:我的数据“df”如下:
city X2020 X2021
1 Stuttgart 2.9 3.1
2 Munich 2.3 2.4
3 Berlin 2.2 2.3
4 Hamburg 3.8 4.0
5 Dresden 3.3 3.0
6 Dortmund 2.5 2.6
7 Paderborn 1.7 1.8
8 Essen 2.6 2.6
9 Heidelberg 3.0 3.2
10 Karlsruhe 2.5 2.4
11 Kiel 2.6 2.7
12 Ravensburg 3.3 2.7
I want exactly this kind of barchart below, but cities should be only ordered by the value of 2021!我想要下面的这种条形图,但城市应该只按 2021 的值排序! I tried "reorder" in the ggplot as recommended, but this does not fit.我按照建议尝试在 ggplot 中“重新排序”,但这不合适。 There are some cities where the ordering is pretty weird and I do not understand what R is doing here.有些城市的排序非常奇怪,我不明白 R 在这里做什么。 My code is the following:我的代码如下:
df_melt <- melt(df, id = "city")
ggplot(df_melt, aes(value, reorder(city, -value), fill = variable)) +
geom_bar(stat="identity", position = "dodge")
str(df_melt)
'data.frame': 24 obs. of 3 variables:
$ city : chr "Stuttgart" "Munich" "Berlin" "Hamburg" ...
$ variable: Factor w/ 2 levels "X2020","X2021": 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 2.9 2.3 2.2 3.8 3.3 2.5 1.7 2.6 3 2.5 ...
https://i.stack.imgur.com/rJQMV.png https://i.stack.imgur.com/rJQMV.png
I think this gets messy because in the variable "value" there are values of both 2020 and 2021 and R possibly takes the mean of both (I dont know!).我认为这会变得混乱,因为在变量“value”中既有 2020 年的值,也有 2021 年的值,而 R 可能取两者的平均值(我不知道!)。 But I have no idea to deal with this further.但我不知道进一步处理这个问题。 I hope somebody can help me with my concern.我希望有人能帮助我解决我的问题。
Thanks!谢谢!
You could try sorting your df with arrange
and then use fct_inorder
to ensure that the city levels is in the order that you want.你可以尝试用你的排序DF arrange
,然后用fct_inorder
,以确保全市水平是在你想要的顺序。
library(tidyverse)
df <- read_table(" city X2020 X2021
1 Stuttgart 2.9 3.1
2 Munich 2.3 2.4
3 Berlin 2.2 2.3
4 Hamburg 3.8 4.0
5 Dresden 3.3 3.0
6 Dortmund 2.5 2.6
7 Paderborn 1.7 1.8
8 Essen 2.6 2.6
9 Heidelberg 3.0 3.2
10 Karlsruhe 2.5 2.4
11 Kiel 2.6 2.7
12 Ravensburg 3.3 2.7 ")
#> Warning: Missing column names filled in: 'X1' [1]
df %>%
select(-X1) %>%
pivot_longer(-city) %>%
arrange(desc(name), -value) %>%
mutate(
city = fct_inorder(city)
) %>%
ggplot(aes(city, value, fill = name)) +
geom_col(position = "dodge")
Created on 2021-07-13 by the reprex package (v1.0.0)由reprex 包(v1.0.0) 于 2021 年 7 月 13 日创建
I just want to add to the previous answer that you can also take this plot and use coord_flip()
to achieve the final result you were looking for.我只想添加到之前的答案中,您也可以使用此图并使用coord_flip()
来实现您正在寻找的最终结果。 😉 😉
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.