英文:
How to modify multiple chart titles in facet wrap?
问题
I created 6 charts using facet_wrap()
using this code::
我使用以下代码创建了6个使用facet_wrap()
的图表:
library(ggplot2)
p <- ggplot(data = kenmerken_ot, aes(x = Persoonskenmerken, y = slachtoffers, fill = kenmerk))
p + geom_col(position = "dodge") +
facet_wrap(~ soort + kenmerk) +
labs(x = NULL, y = "Slachtoffers in %", fill = "Test") +
guides(fill = FALSE) +
theme(axis.text.x = element_blank())
The data looks like this:
数据如下:
Marges | Persoonskenmerken | Perioden | slachtoffers | soort | kenmerk |
---|---|---|---|---|---|
Waarde | Leeftijd: 15 tot 25 jaar | 2021 | 19.5 | Online | 1 |
Waarde | Leeftijd: 25 tot 45 jaar | 2021 | 18.4 | Online | 1 |
Waarde | Leeftijd: 45 tot 65 jaar | 2021 | 18 | Online | 1 |
Waarde | Onderwijsniveau: 1 Laag | 2021 | 12 | Online | 2 |
Waarde | Onderwijsniveau: 2 Middelbaar | 2021 | 17.7 | Online | 2 |
Waarde | Onderwijsniveau: 3 Hoog | 2021 | 17.7 | Online | 2 |
Waarde | Gestandaardiseerd inkomen: 1e 20%-groep | 2021 | 17.7 | Online | 2 |
-
I want to remove the "Online and Tradioneel" labels and place them on the right of the plot (Horizontally).
我想要移除"Online and Tradioneel"标签并将它们水平地放在图的右侧。
-
I also want to replace the 1, 2, and 3 labels with age, education, and income.
我还想将1、2和3标签替换为age(年龄)、education(教育)和income(收入)。
-
Since they are on top of each other I want them to appear only once not twice for every chart the age, education, and income labels).
由于它们彼此叠加在一起,我希望年龄、教育和收入标签只出现一次,而不是每个图表都出现两次。
英文:
I created 6 charts using facet_wrap()
using this code::
library(ggplot2)
p <- ggplot(data = kenmerken_ot, aes(x = Persoonskenmerken, y = slachtoffers, fill =
kenmerk))
p + geom_col(position = "dodge") +
facet_wrap(~ soort + kenmerk) +
labs(x = NULL, y = "Slachtoffers in %", fill = "Test") +
guides(fill = FALSE) +
theme(axis.text.x = element_blank())
The data looks like this:
Marges | Persoonskenmerken | Perioden | slachtoffers | soort | kenmerk |
---|---|---|---|---|---|
Waarde | Leeftijd: 15 tot 25 jaar | 2021 | 19.5 | Online | 1 |
Waarde | Leeftijd: 25 tot 45 jaar | 2021 | 18.4 | Online | 1 |
Waarde | Leeftijd: 45 tot 65 jaar | 2021 | 18 | Online | 1 |
Waarde | Onderwijsniveau: 1 Laag | 2021 | 12 | Online | 2 |
Waarde | Onderwijsniveau: 2 Middelbaar | 2021 | 17.7 | Online | 2 |
Waarde | Onderwijsniveau: 3 Hoog | 2021 | 17.7 | Online | 2 |
Waarde | Gestandaardiseerd inkomen: 1e 20%-groep | 2021 | 17.7 | Online | 2 |
- I want to remove the "Online and Tradioneel" labels and place them on the right of the plot (Horizontally).
- I also want to replace the 1, 2, and 3 labels with age, education, and income.
- Since they are on top of each other I want them to appear only once not twice for every chart the age, education, and income labels).
How can I accomplish that?
kenmerken_ot <- tibble::tribble(
~Marges, ~Persoonskenmerken, ~Perioden, ~slachtoffers, ~soort, ~kenmerk,
"Waarde", "Leeftijd: 15 tot 25 jaar", 2021, 19.5, "Online", 1,
"Waarde", "Leeftijd: 25 tot 45 jaar", 2021, 18.4, "Online", 1,
"Waarde", "Leeftijd: 45 tot 65 jaar", 2021, 18, "Online", 1,
"Waarde", "Leeftijd: 65 jaar of ouder", 2021, 12, "Online", 1,
"Waarde", "Onderwijsniveau: 1 Laag", 2021, 15.4, "Online", 2,
"Waarde", "Onderwijsniveau: 2 Middelbaar", 2021, 17.7, "Online", 2,
"Waarde", "Onderwijsniveau: 3 Hoog", 2021, 17.7, "Online", 2,
"Waarde", "Gestandaardiseerd inkomen: 1e 20%-groep", 2021, 17.7, "Online", 3,
"Waarde", "Gestandaardiseerd inkomen: 2e 20%-groep", 2021, 15.6, "Online", 3,
"Waarde", "Gestandaardiseerd inkomen: 3e 20%-groep", 2021, 16.9, "Online", 3,
"Waarde", "Gestandaardiseerd inkomen: 4e 20%-groep", 2021, 17.1, "Online", 3,
"Waarde", "Gestandaardiseerd inkomen: 5e 20%-groep", 2021, 17.3, "Online", 3,
"Waarde", "Leeftijd: 15 tot 25 jaar", 2021, 23.7, "Traditioneel", 1,
"Waarde", "Leeftijd: 25 tot 45 jaar", 2021, 20.8, "Traditioneel", 1,
"Waarde", "Leeftijd: 45 tot 65 jaar", 2021, 16.1, "Traditioneel", 1,
"Waarde", "Leeftijd: 65 jaar of ouder", 2021, 9.8, "Traditioneel", 1,
"Waarde", "Onderwijsniveau: 1 Laag", 2021, 15.2, "Traditioneel", 2,
"Waarde", "Onderwijsniveau: 2 Middelbaar", 2021, 16.7, "Traditioneel", 2,
"Waarde", "Onderwijsniveau: 3 Hoog", 2021, 19, "Traditioneel", 2,
"Waarde", "Gestandaardiseerd inkomen: 1e 20%-groep", 2021, 22.6, "Traditioneel", 3,
"Waarde", "Gestandaardiseerd inkomen: 2e 20%-groep", 2021, 16.4, "Traditioneel", 3,
"Waarde", "Gestandaardiseerd inkomen: 3e 20%-groep", 2021, 15.6, "Traditioneel", 3,
"Waarde", "Gestandaardiseerd inkomen: 4e 20%-groep", 2021, 15.7, "Traditioneel", 3,
"Waarde", "Gestandaardiseerd inkomen: 5e 20%-groep", 2021, 17.1, "Traditioneel", 3,
)
答案1
得分: 1
You are looking to use facet_grid()
instead of facet_wrap()
. Here's an explanation:
facet_wrap()
separates facets with each getting its label, and there's no grouping.facet_grid()
separates and groups facets along two variables, with labels per column and row.
To customize labels, use the labeller=
argument:
p + facet_grid(soort ~ kenmerk, labeller = as_labeller(
c(`1` = "age",
`2` = "education",
`3` = "income",
"Traditioneel" = "Traditioneel",
"Online" = "Online")
))
Note the use of backticks (`) for numeric values like 1, 2, and 3.
英文:
OP, you are looking to use facet_grid()
here, and not facet_wrap()
. Here's something that explains the difference, but here's my own explanation:
-
facet_wrap()
separates the facets, with each getting it's own label, and with facets not being grouped at all. You can specify number of columns and rows, but otherwise no grouping is performed. -
facet_grid()
separates the facets and groups along two variables. The resulting facets are grouped along columns and rows, respectively, and are labeled once per column and per row by default.
There are packages that affect how grouping and labeling is done on facets, and you can check them out. Notably, lemon
and ggh4x
both have ways to customize facets.
The default option separates the labels for your two variables as you wanted:
p + facet_grid(soort~kenmerk)
To change the labels, you need to access the labeller=
argument within the facet_*()
function. I find it's easiest to format a named vector as_labeller()
:
p + facet_grid(
soort~kenmerk, labeller = as_labeller(
c(`1`="age",
`2`="education",
`3`="income",
"Traditioneel"="Traditioneel",
"Online"="Online")
)
)
Note that we have to specifically name all values here (even if they don't change, like "Online" = "Online"
), and also note the method of using single backticks (`) to enclose a value that is a number, such as 1, 2, and 3.
Final note: I have no idea why the x axis labels aren't showing here, but I had to mess with your dput()
to get the dataframe to read properly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论