英文:
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 |> mutate(date = my(Monat))
df_cre %>%
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 |> mutate(date=my(Monat))
df_cre %>%
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"))
答案2
得分: 0
假设您想要躲避的 M-YYYY y 轴:
df_cre %>%
mutate(Monat = forcats::fct_reorder(
Monat, lubridate::my(Monat)) %>% forcats::fct_rev()) %>%
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 %>%
mutate(Monat = forcats::fct_reorder(
Monat, lubridate::my(Monat)) %>% forcats::fct_rev()) %>%
ggplot(aes(x=Einnahmen, y=Monat, fill=Wer)) +
geom_col() +
scale_y_discrete(guide = guide_axis(n.dodge = 2))
答案3
得分: 0
以下是代码部分的翻译:
通过尝试后,我成功地让它工作,引入了一个新的列,其中保存了每个月的第一天,然后使用 scale_x_date
来在 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 <-
data.frame(Monat=c("2020-01-01", "2020-02-01", "2020-03-01", "2020-04-01",
"2020-05-01", "2020-06-01", "2020-07-01", "2020-08-01", "2020-09-01",
"2020-10-01", "2020-11-01", "2020-12-01", "2021-01-01", "2021-02-01",
"2021-03-01", "2021-04-01", "2021-05-01", "2021-06-01", "2021-07-01",
"2021-08-01", "2021-09-01", "2021-10-01", "2021-10-01", "2021-11-01",
"2021-11-01", "2021-12-01", "2021-12-01", "2022-01-01", "2022-01-01",
"2022-02-01", "2022-02-01", "2022-03-01", "2022-04-01", "2022-05-01",
"2022-06-01", "2022-07-01", "2022-07-01", "2022-08-01", "2022-08-01",
"2022-09-01", "2022-09-01", "2022-10-01", "2022-10-01", "2022-11-01",
"2022-11-01", "2022-12-01", "2023-01-01", "2023-02-01", "2023-03-01",
"2023-04-01", "2023-05-01", "2023-05-01"),
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))
df_cre$Monat <- as.Date(df_cre$Monat)
df_cre %>%
ggplot(aes(fill=Wer, y=Einnahmen, x=Monat)) +
geom_col(position="stack") +
theme(axis.text.x = element_text(angle = 65, hjust = 1)) +
scale_x_date(date_labels = "%b %Y", date_breaks = "3 month")
This gives the following plot:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论