ggexample: Beispiele SGB-Grafiken

Description See Also Examples

Description

Hier werden verschiedene Beispiele fuer typische SGB-Grafiken unter Verwendung des SGB-Themes und der SGB Farben beschrieben. Zudem zeigt es auch, wie Grafiken als png, emf (vektorisierte Grafiken fuer Word) oder als pdf gespeichert werden koennen.

See Also

scale_color_discrete und scale_fill_discrete fuer diskrete Variablen. scale_color_gradient und scale_fill_gradient fuer kontinuerliche Variablen. usecol um komplexere Paletten zu mischen. seecol um alle SGB-Paletten und -Farben zu anzusehen.

Other plot functions: theme_sgb()

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
## Not run: 
library(ggplot2)
library(gsgb)
theme_set(theme_sgb(base_size = 9))

## Linien ------------------------------------------------
# z.B. Zeitreihen
ggplot(kof, aes(x = time, y = value, colour = branche)) +
  geom_line(size = 1) +
  geom_hline(yintercept = 0, size = 0.5) + # dickere Linie bei 0
  labs(x = "", y = "")

# Farben & Zeitvariable anpassen
ggplot(mangelindikator, aes(x = time, y = value, colour = label)) +
  geom_line(size = 1) +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  scale_color_manual(values = rev(usecol(pal_sgb_dunkelblau))) +
  labs(x = "", y = "Mangelindikator")


## Saeulen ------------------------------------------------
# geom_bar: y-Achse bildet Anzahl der Beobachtungen ab
ggplot(mpg, aes(as.factor(year), fill = drv)) +
  geom_bar(position = "dodge")

# Farben anpassen
ggplot(mpg, aes(as.factor(year), fill = drv)) +
  geom_bar(position = "dodge") +
  scale_fill_manual(values = usecol(c(SGBrot, SGBgelb, pal_sgb_dunkelblau[4])))

# geom_col: y-Achse ist eine Variable aus den Daten
ggplot(dplyr::filter(mangelindikator, time == "2020-01-01" | time == "2004-01-01")) +
  geom_col(aes(x = as.factor(time), y = value, fill = label), position = "dodge") +
  labs(x = "", y = "Mangelindikator")


## Punkte ------------------------------------------------
# Diskrete Farbvariable (hier Species)
ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
  geom_point()

# Kontinuierliche Farbvariable (hier Sepal.Length)
ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Sepal.Length)) +
  geom_point()


## Raster ------------------------------------------------
# Diskrete Farbvariable
df <- data.frame(
  x = rep(c(2, 5, 7, 9, 12), 2),
  y = rep(c(1, 2), each = 5),
  z = factor(rep(1:5, each = 2)))
ggplot(df, aes(x, y)) +
  geom_tile(aes(fill = z)) +
  scale_fill_manual(values = usecol(pal_sgb))

# Kontinuierliche Farbvariable
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
    geom_raster()


## Kuchen-Diagramm ------------------------------------------------
library(magrittr)
df <- data.frame(group = c("Oberstes 1%", "Uebrige 9%", "Unterste 90%"),
                 value = c(42, 33, 25))
df <- df %>%
  dplyr::arrange(dplyr::desc(group)) %>%
  dplyr::mutate(prop = value / sum(df$value) * 100) %>%
  dplyr::mutate(ypos = cumsum(prop) - 0.5 * prop)

ggplot(df, aes(x = "", y = prop, fill = group)) +
  geom_bar(stat = "identity",
           width = 1,
           color = "white") +
  coord_polar("y", start = 0) +
  geom_text(aes(y = ypos, label = scales::percent(prop / 100)),
            color = "white",
            size = 5) +
  theme_sgb_blank() + # minimales Theme ohne Achsen und andere Beschriftungen
  theme(
    legend.position = "right",
    legend.direction =   "vertical",
    legend.justification = c("right", "center"))


## Als png speichern ------------------------------------------------
ggplot(kof) +
  geom_line(aes(x = time, y = value, colour = branche), size = 1) +
  geom_hline(yintercept = 0, size = 0.5) +
  labs(x = "", y = "")
ggsave("docs/lineplot.jpeg", wide = TRUE)


## Als EMF speichern ------------------------------------------------
# (emf ist ein Vektorformat, das von Word akzeptiert wird)
devEMF::emf(file = "docs/lineplot2.emf", width = 6.2, height = 2.75) # width/height in inches
ggplot(mangelindikator, aes(x = time, y = value, colour = label)) +
  geom_line(size = 1) +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  labs(x = "", y = "Mangelindikator")
dev.off()


## Als PDF speichern --------------------------------------------
# Eine Seite
pdf("docs/barplot.pdf", height = 20/2.54, width = 27/2.54)
ggplot(mpg, aes(as.factor(year), fill = drv)) +
  geom_bar(position = "dodge") +
  labs(x = "", y = "")
dev.off()

# Mehrere Seiten
pdf("docs/barplot_mult.pdf", onefile = TRUE,
    height = 20/2.54, width = 27/2.54,
    paper = "a4r")
ggplot(mpg, aes(as.factor(year), fill = drv)) +
  geom_bar(position = "dodge") +
  labs(x = "", y = "")
ggplot(kof) +
  geom_line(aes(x = time, y = value, colour = branche), size = 1) +
  geom_hline(yintercept = 0, size = 0.5) +
  labs(x = "", y = "")
dev.off()


## End(Not run)

SchweizerischerGewerkschaftsbund/gsgb documentation built on Dec. 18, 2021, 1 p.m.