如何在ggplot2柱状图上正确排序x轴值(月份-年份)?

huangapple go评论78阅读模式
英文:

How can I sort x axis values (month-year) on ggplot2 bar plot correctly?

问题

I have a data frame which contains the following columns: J (year), M (month), Monat (which is composed as M-J), and two other variables which contain name and a month sum. The column Monat (M-J) should go to the x-axis, and the month sum onto the y-axis. The name is used as a category.

As with M-J, the ordering on the x-axis is not correct. How can I get the ordering based on M and J correct?

I tried converting M and J to factors, played with reorder, but I could not get it right.

library(dplyr)
library(ggplot2)

df_cre <- data.frame(J=c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                     2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 
                     2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2022, 
                     2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 
                     2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2023, 2023, 
                     2023, 2023, 2023, 2023), 
                 M=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
                   11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 11, 12, 12, 
                   1, 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 
                   12, 1, 2, 3, 4, 5, 5), 
                 Wer=c("Abt1", "Abt1", "Abt1", "Abt1", 
                  "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", 
                  "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", 
                  "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt2", "Abt1", 
                  "Abt2", "Abt1", "Abt2", "Abt1", "Abt2", "Abt1", 
                  "Abt2", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt2", "Abt1", "Abt2", 
                  "Abt1", "Abt2", "Abt1", "Abt2", "Abt1", "Abt2", "Abt1", 
                  "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt2"), 
                 Einnahmen=c(20265.43, 
                   9323.48, 3350.52, 3338.37, 3317.05, 7223.91, 4632.8, 23264.63, 
                   3678.72, 3374.29, 8290, 8176.53, 7805.57, 3276.87, 3490.59, 
                   3189.03, 3189.72, 7115.97, 3183.22, 3182.24, 3475.89, 3343.2, 
                   203.71, 8095.84, 599.64, 3214.26, 639.52, 3567.89, 509.28, 
                   7327.42, 644.01, 3452.29, 3623.8, 3363.84, 7580.19, 3975.35, 
                   1913, 3796.32, 1110, 3765.24, 1110, 3976.99, 1110, 7542.8, 
                   1549, 3529.44, 3451.7, 3476.97, 3495.02, 4110.98, 485.48, 
                   447.02), 
                 Monat=c("1-2020", "2-2020", "3-2020", "4-2020", "5-2020", 
                  "6-2020", "7-2020", "8-2020", "9-2020", "10-2020", "11-2020", 
                  "12-2020", "1-2021", "2-2021", "3-2021", "4-2021", "5-2021", 
                  "6-2021", "7-2021", "8-2021", "9-2021", "10-2021", "10-2021", 
                  "11-2021", "11-2021", "12-2021", "12-2021", "1-2022", "1-2022", 
                  "2-2022", "2-2022", "3-2022", "4-2022", "5-2022", "6-2022", 
                  "7-2022", "7-2022", "8-2022", "8-2022", "9-2022", "9-2022", 
                  "10-2022", "10-2022", "11-2022", "11-2022", "12-2022", "1-2023", 
                  "2-2023", "3-2023", "4-2023", "5-2023", "5-2023"))

df_cre %>% 
  ggplot(aes(fill=Wer, y=Einnahmen, x=Monat)) + 
  geom_bar(position="stack", stat="identity") +
  coord_flip() +
  #scale_x_discrete(breaks = scales::pretty_breaks(n=10)) 
  scale_x_discrete(guide = guide_axis(n.dodge = 2))
``

<details>
<summary>英文:</summary>

I have a data frame which contains the following columns: J (year), M (month), Monat (which is composed as M-J), and two other variables which contain name and a month sum.
The column Monat (M-J) should go to the x-axis, and the month sum onto the y-axis. The name is used as category.

As with M-J, the ordering on the x-axis is not correct. How can I get the ordering based on M and J correct?

I tried converting M and J to factors, played with reorder, but I could not get it right.

library(dplyr)
library(ggplot2)

df_cre <- data.frame(J=c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2022,
2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022,
2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2023, 2023,
2023, 2023, 2023, 2023),
M=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 11, 12, 12,
1, 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 1, 2, 3, 4, 5, 5),
Wer=c("Abt1", "Abt1", "Abt1", "Abt1",
"Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1",
"Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt1",
"Abt1", "Abt1", "Abt1", "Abt1", "Abt2", "Abt1", "Abt2",
"Abt1", "Abt2", "Abt1", "Abt2", "Abt1", "Abt2", "Abt1",
"Abt1", "Abt1", "Abt1", "Abt1", "Abt2", "Abt1", "Abt2",
"Abt1", "Abt2", "Abt1", "Abt2", "Abt1", "Abt2", "Abt1",
"Abt1", "Abt1", "Abt1", "Abt1", "Abt1", "Abt2"),
Einnahmen=c(20265.43,
9323.48, 3350.52, 3338.37, 3317.05, 7223.91, 4632.8, 23264.63,
3678.72, 3374.29, 8290, 8176.53, 7805.57, 3276.87, 3490.59,
3189.03, 3189.72, 7115.97, 3183.22, 3182.24, 3475.89, 3343.2,
203.71, 8095.84, 599.64, 3214.26, 639.52, 3567.89, 509.28,
7327.42, 644.01, 3452.29, 3623.8, 3363.84, 7580.19, 3975.35,
1913, 3796.32, 1110, 3765.24, 1110, 3976.99, 1110, 7542.8,
1549, 3529.44, 3451.7, 3476.97, 3495.02, 4110.98, 485.48,
447.02),
Monat=c("1-2020", "2-2020", "3-2020", "4-2020", "5-2020",
"6-2020", "7-2020", "8-2020", "9-2020", "10-2020", "11-2020",
"12-2020", "1-2021", "2-2021", "3-2021", "4-2021", "5-2021",
"6-2021", "7-2021", "8-2021", "9-2021", "10-2021", "10-2021",
"11-2021", "11-2021", "12-2021", "12-2021", "1-2022", "1-2022",
"2-2022", "2-2022", "3-2022", "4-2022", "5-2022", "6-2022",
"7-2022", "7-2022", "8-2022", "8-2022", "9-2022", "9-2022",
"10-2022", "10-2022", "11-2022", "11-2022", "12-2022", "1-2023",
"2-2023", "3-2023", "4-2023", "5-2023", "5-2023"))

df_cre %>%
ggplot(aes(fill=Wer, y=Einnahmen, x=Monat)) +
geom_bar(position="stack", stat="identity") +
coord_flip() +
#scale_x_discrete(breaks = scales::pretty_breaks(n=10))
scale_x_discrete(guide = guide_axis(n.dodge = 2))

I flipped the coordinates as the x-axis is very dense. This is a problem for later :)
[![bar plot with wrong ordering on x-axis][1]][1]
[1]: https://i.stack.imgur.com/JPoZg.png
</details>
# 答案1
**得分**: 0
```R
library(lubridate)
library(scales)
df_cre = df_cre |&gt; mutate(date = my(Monat))
df_cre %&gt;%
ggplot(aes(fill = Wer, y = Einnahmen, x = date)) +
geom_bar(position = "stack", stat = "identity") +
coord_flip() +
scale_x_date(breaks = "5 month", labels = date_format("%b-%y"))
英文:
library(lubridate)
library(scales)
df_cre=df_cre |&gt; mutate(date=my(Monat))
df_cre %&gt;% 
ggplot(aes(fill=Wer, y=Einnahmen, x=date)) + 
geom_bar(position=&quot;stack&quot;, stat=&quot;identity&quot;) +
coord_flip()+
scale_x_date(breaks=&quot;5 month&quot;, labels = date_format(&quot;%b-%y&quot;))

如何在ggplot2柱状图上正确排序x轴值(月份-年份)?

答案2

得分: 0

假设您想要躲避的 M-YYYY y 轴:

    df_cre %&gt;% 
      mutate(Monat = forcats::fct_reorder(
        Monat, lubridate::my(Monat)) %&gt;% forcats::fct_rev()) %&gt;% 
      ggplot(aes(x=Einnahmen, y=Monat, fill=Wer)) + 
      geom_col() +
      scale_y_discrete(guide = guide_axis(n.dodge = 2))
英文:

Assuming you want the dodged M-YYYY y axis:

df_cre %&gt;% 
mutate(Monat = forcats::fct_reorder(
Monat, lubridate::my(Monat)) %&gt;% forcats::fct_rev()) %&gt;%
ggplot(aes(x=Einnahmen, y=Monat, fill=Wer)) + 
geom_col() +
scale_y_discrete(guide = guide_axis(n.dodge = 2))

如何在ggplot2柱状图上正确排序x轴值(月份-年份)?

答案3

得分: 0

以下是代码部分的翻译:

通过尝试后,我成功地让它工作,引入了一个新的列,其中保存了每个月的第一天,然后使用 scale_x_date 来在 x 轴上获取标签。

这是生成的图表:

如何在ggplot2柱状图上正确排序x轴值(月份-年份)?

英文:

After playing around I managed to get it working, by introducing a new column which holds the first day of a month, and then using scale_x_date to get a label on the x-axis:

library(dplyr)
library(ggplot2)
df_cre &lt;- 
data.frame(Monat=c(&quot;2020-01-01&quot;, &quot;2020-02-01&quot;, &quot;2020-03-01&quot;, &quot;2020-04-01&quot;, 
&quot;2020-05-01&quot;, &quot;2020-06-01&quot;, &quot;2020-07-01&quot;, &quot;2020-08-01&quot;, &quot;2020-09-01&quot;, 
&quot;2020-10-01&quot;, &quot;2020-11-01&quot;, &quot;2020-12-01&quot;, &quot;2021-01-01&quot;, &quot;2021-02-01&quot;, 
&quot;2021-03-01&quot;, &quot;2021-04-01&quot;, &quot;2021-05-01&quot;, &quot;2021-06-01&quot;, &quot;2021-07-01&quot;, 
&quot;2021-08-01&quot;, &quot;2021-09-01&quot;, &quot;2021-10-01&quot;, &quot;2021-10-01&quot;, &quot;2021-11-01&quot;, 
&quot;2021-11-01&quot;, &quot;2021-12-01&quot;, &quot;2021-12-01&quot;, &quot;2022-01-01&quot;, &quot;2022-01-01&quot;, 
&quot;2022-02-01&quot;, &quot;2022-02-01&quot;, &quot;2022-03-01&quot;, &quot;2022-04-01&quot;, &quot;2022-05-01&quot;, 
&quot;2022-06-01&quot;, &quot;2022-07-01&quot;, &quot;2022-07-01&quot;, &quot;2022-08-01&quot;, &quot;2022-08-01&quot;, 
&quot;2022-09-01&quot;, &quot;2022-09-01&quot;, &quot;2022-10-01&quot;, &quot;2022-10-01&quot;, &quot;2022-11-01&quot;, 
&quot;2022-11-01&quot;, &quot;2022-12-01&quot;, &quot;2023-01-01&quot;, &quot;2023-02-01&quot;, &quot;2023-03-01&quot;, 
&quot;2023-04-01&quot;, &quot;2023-05-01&quot;, &quot;2023-05-01&quot;), 
Wer=c(&quot;Abt1&quot;, &quot;Abt1&quot;, 
&quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, 
&quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, 
&quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt2&quot;, 
&quot;Abt1&quot;, &quot;Abt2&quot;, &quot;Abt1&quot;, &quot;Abt2&quot;, &quot;Abt1&quot;, &quot;Abt2&quot;, &quot;Abt1&quot;, 
&quot;Abt2&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt2&quot;, 
&quot;Abt1&quot;, &quot;Abt2&quot;, &quot;Abt1&quot;, &quot;Abt2&quot;, &quot;Abt1&quot;, &quot;Abt2&quot;, &quot;Abt1&quot;, 
&quot;Abt2&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, &quot;Abt1&quot;, 
&quot;Abt2&quot;), 
Einnahmen=c(20265.43, 9323.48, 3350.52, 3338.37, 3317.05, 
7223.91, 4632.8, 23264.63, 3678.72, 3374.29, 8290, 8176.53, 
7805.57, 3276.87, 3490.59, 3189.03, 3189.72, 7115.97, 3183.22, 
3182.24, 3475.89, 3343.2, 203.71, 8095.84, 599.64, 3214.26, 
639.52, 3567.89, 509.28, 7327.42, 644.01, 3452.29, 3623.8, 
3363.84, 7580.19, 3975.35, 1913, 3796.32, 1110, 3765.24, 
1110, 3976.99, 1110, 7542.8, 1549, 3529.44, 3451.7, 3476.97, 
3495.02, 4110.98, 485.48, 447.02))
df_cre$Monat &lt;- as.Date(df_cre$Monat)
df_cre %&gt;% 
ggplot(aes(fill=Wer, y=Einnahmen, x=Monat)) + 
geom_col(position=&quot;stack&quot;) +
theme(axis.text.x = element_text(angle = 65, hjust = 1)) +
scale_x_date(date_labels = &quot;%b %Y&quot;, date_breaks = &quot;3 month&quot;)

This gives the following plot:

如何在ggplot2柱状图上正确排序x轴值(月份-年份)?

huangapple
  • 本文由 发表于 2023年5月30日 01:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定