如何修改facet wrap中的多个图表标题?

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

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

如何修改facet wrap中的多个图表标题?

  • 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 &lt;- ggplot(data = kenmerken_ot, aes(x = Persoonskenmerken, y = slachtoffers, fill = 
kenmerk))
p + geom_col(position = &quot;dodge&quot;) +
facet_wrap(~ soort + kenmerk) +
labs(x = NULL, y = &quot;Slachtoffers in %&quot;, fill = &quot;Test&quot;) +
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

如何修改facet wrap中的多个图表标题?

  • 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 &lt;- tibble::tribble(
  ~Marges,  ~Persoonskenmerken,                        ~Perioden, ~slachtoffers, ~soort,         ~kenmerk,
  &quot;Waarde&quot;, &quot;Leeftijd: 15 tot 25 jaar&quot;,                2021,      19.5,          &quot;Online&quot;,       1,
  &quot;Waarde&quot;, &quot;Leeftijd: 25 tot 45 jaar&quot;,                2021,      18.4,          &quot;Online&quot;,       1,
  &quot;Waarde&quot;, &quot;Leeftijd: 45 tot 65 jaar&quot;,                2021,      18,            &quot;Online&quot;,       1,
  &quot;Waarde&quot;, &quot;Leeftijd: 65 jaar of ouder&quot;,              2021,      12,            &quot;Online&quot;,       1,
  &quot;Waarde&quot;, &quot;Onderwijsniveau: 1 Laag&quot;,                 2021,      15.4,          &quot;Online&quot;,       2,
  &quot;Waarde&quot;, &quot;Onderwijsniveau: 2 Middelbaar&quot;,           2021,      17.7,          &quot;Online&quot;,       2,
  &quot;Waarde&quot;, &quot;Onderwijsniveau: 3 Hoog&quot;,                 2021,      17.7,          &quot;Online&quot;,       2,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 1e 20%-groep&quot;, 2021,      17.7,          &quot;Online&quot;,       3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 2e 20%-groep&quot;, 2021,      15.6,          &quot;Online&quot;,       3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 3e 20%-groep&quot;, 2021,      16.9,          &quot;Online&quot;,       3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 4e 20%-groep&quot;, 2021,      17.1,          &quot;Online&quot;,       3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 5e 20%-groep&quot;, 2021,      17.3,          &quot;Online&quot;,       3,
  &quot;Waarde&quot;, &quot;Leeftijd: 15 tot 25 jaar&quot;,                2021,      23.7,          &quot;Traditioneel&quot;, 1,
  &quot;Waarde&quot;, &quot;Leeftijd: 25 tot 45 jaar&quot;,                2021,      20.8,          &quot;Traditioneel&quot;, 1,
  &quot;Waarde&quot;, &quot;Leeftijd: 45 tot 65 jaar&quot;,                2021,      16.1,          &quot;Traditioneel&quot;, 1,
  &quot;Waarde&quot;, &quot;Leeftijd: 65 jaar of ouder&quot;,              2021,      9.8,           &quot;Traditioneel&quot;, 1,
  &quot;Waarde&quot;, &quot;Onderwijsniveau: 1 Laag&quot;,                 2021,      15.2,          &quot;Traditioneel&quot;, 2,
  &quot;Waarde&quot;, &quot;Onderwijsniveau: 2 Middelbaar&quot;,           2021,      16.7,          &quot;Traditioneel&quot;, 2,
  &quot;Waarde&quot;, &quot;Onderwijsniveau: 3 Hoog&quot;,                 2021,      19,            &quot;Traditioneel&quot;, 2,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 1e 20%-groep&quot;, 2021,      22.6,          &quot;Traditioneel&quot;, 3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 2e 20%-groep&quot;, 2021,      16.4,          &quot;Traditioneel&quot;, 3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 3e 20%-groep&quot;, 2021,      15.6,          &quot;Traditioneel&quot;, 3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 4e 20%-groep&quot;, 2021,      15.7,          &quot;Traditioneel&quot;, 3,
  &quot;Waarde&quot;, &quot;Gestandaardiseerd inkomen: 5e 20%-groep&quot;, 2021,      17.1,          &quot;Traditioneel&quot;, 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)

如何修改facet wrap中的多个图表标题?

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`=&quot;age&quot;,
      `2`=&quot;education&quot;,
      `3`=&quot;income&quot;,
      &quot;Traditioneel&quot;=&quot;Traditioneel&quot;,
      &quot;Online&quot;=&quot;Online&quot;)
    )
  )

如何修改facet wrap中的多个图表标题?

Note that we have to specifically name all values here (even if they don't change, like &quot;Online&quot; = &quot;Online&quot;), 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.

huangapple
  • 本文由 发表于 2023年5月7日 03:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190828.html
匿名

发表评论

匿名网友

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

确定