GEOM_LOVENGON未显示在R

问题描述 投票:0回答:1

在我的r-script中,我试图在我的ggplot的背景下使用以下代码为不同的BMI类着色:

  #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")
不幸的是,无论我做什么(删除蓝色的ploygon,更改其他多边形的点),另一个多边形exept blue一个不会出现在屏幕上。

r ggplot2 rstudio geom
1个回答
0
投票
xlim()

ylim()
的极限之外。而是使用
coord_cartesian
缩放数据的特定范围:
注:您的数据集

normalgewicht

uebergewicht
是相同的,因此不会显示绿色多边形。
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))

enter image description here还请注意,我可能更容易使用一个数据集和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))

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.