0

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

Here's my result

  • 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,
)
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Nasher
  • 7
  • 3

1 Answers1

1

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)

enter image description here

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")
    )
  )

enter image description here

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.

chemdork123
  • 12,369
  • 2
  • 16
  • 32