in my R-Script I am trying to color the diffrent BMI-classes in the background of my ggplot with the following code:
#Gewichtsklassen
untergewicht <- data.frame(
height = c(150, 150, 210, 210),
weight = c(40, 41.6, 81.4, 40)
)
normalgewicht <- data.frame(
height = c(150, 150, 210, 210),
weight = c(41.7, 56.2, 110.1, 81.5)
)
uebergewicht <- data.frame(
height = c(150, 150, 210, 210),
weight = c(41.7, 56.2, 110.1, 81.5)
)
adipositas_I <- data.frame(
height = c(150, 150, 210, 210),
weight = c(56.3, 65.6, 128.3, 110.2)
)
adipositas_II <- data.frame(
height = c(150, 150, 210, 210),
weight = c(65.7, 75.0, 146.6, 128.4)
)
adipositas_III <- data.frame(
height = c(150, 150, 210, 210),
weight = c(75.1, 100, 200, 146.7)
)
grafik <- male_players_gefiltert %>%
ggplot(aes(x = height_cm, y = weight_kg)) +
geom_polygon(data = untergewicht, aes(x = height, y = weight), fill = "blue") +
geom_polygon(data = normalgewicht, aes(x = height, y = weight), fill = "green") +
geom_polygon(data = uebergewicht, aes(x = height, y = weight), fill = "yellow") +
geom_polygon(data = adipositas_I, aes(x = height, y = weight), fill = "orange") +
geom_polygon(data = adipositas_II, aes(x = height, y = weight), fill = "red") +
geom_polygon(data = adipositas_III, aes(x = height, y = weight), fill = "darkred") +
geom_point(alpha = 0.1, shape = 15) +
xlim(150, 210) +
ylim(40, 110) +
geom_vline(xintercept = my_height, linetype = "dashed") +
geom_hline(yintercept = my_weight, linetype = "dashed")
unfortunately no matter what I do (delete the blue ploygon, change the points of the other polygons) the other polygon exept the blue one will not appear on the screen.
The issue is that all polygons except the blue one fall outside of the limits you set with xlim()
and ylim()
. Instead use coord_cartesian
to zoom on a specific range of the data:
Note: Your datasets normalgewicht
and uebergewicht
are identical and hence no green polygon will show up.
library(ggplot2)
ggplot() +
geom_polygon(data = untergewicht, aes(x = height, y = weight), fill = "blue") +
geom_polygon(data = normalgewicht, aes(x = height, y = weight), fill = "green") +
geom_polygon(data = uebergewicht, aes(x = height, y = weight), fill = "yellow") +
geom_polygon(data = adipositas_I, aes(x = height, y = weight), fill = "orange") +
geom_polygon(data = adipositas_II, aes(x = height, y = weight), fill = "red") +
geom_polygon(data = adipositas_III, aes(x = height, y = weight), fill = "darkred") +
coord_cartesian(xlim = c(150, 210), ylim = c(40, 110))
Also note that I might be easier to use only one dataset and geom_polyon
:
data_polygon <- dplyr::lst(
untergewicht, normalgewicht, uebergewicht,
adipositas_I, adipositas_II, adipositas_III
)
pal_color <- c("blue", "green", "yellow", "orange", "red", "darkred")
names(pal_color) <- names(data_polygon)
data_polygon <- data_polygon |>
dplyr::bind_rows(.id = "type")
ggplot() +
geom_polygon(data = data_polygon, aes(x = height, y = weight, fill = type)) +
scale_fill_manual(
values = pal_color
) +
geom_point(alpha = 0.1, shape = 15) +
coord_cartesian(xlim = c(150, 210), ylim = c(40, 110))