R语言 为什么堆叠条形图中有一个放错位置的geom_text标签?

6qqygrtg  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(103)

框架看起来像这样

focus_graph <- structure(list(question = c("Coherence: The lesson intentionally\nconnects mathematical concepts\nwithin and/or across grades\nas appropriate, reflecting the\ncoherence in the standards.", 
"A. Overall", "Rigor: The lesson intentionally\ntargets the aspect(s) of rigor\n(conceptual understanding,\nprocedural skill and fluency,\napplication) called for by the\nstandard(s) being addressed.", 
"Focus: The lesson focuses on the\ndepth of the grade/course-level\ncluster(s), grade/course-level\ncontent standard(s), or part(s)\nthereof", 
"Focus: The lesson focuses on the\ndepth of the grade/course-level\ncluster(s), grade/course-level\ncontent standard(s), or part(s)\nthereof", 
"A. Overall", "Rigor: The lesson intentionally\ntargets the aspect(s) of rigor\n(conceptual understanding,\nprocedural skill and fluency,\napplication) called for by the\nstandard(s) being addressed.", 
"Coherence: The lesson intentionally\nconnects mathematical concepts\nwithin and/or across grades\nas appropriate, reflecting the\ncoherence in the standards.", 
"Coherence: The lesson intentionally\nconnects mathematical concepts\nwithin and/or across grades\nas appropriate, reflecting the\ncoherence in the standards.", 
"Focus: The lesson focuses on the\ndepth of the grade/course-level\ncluster(s), grade/course-level\ncontent standard(s), or part(s)\nthereof", 
"A. Overall", "Rigor: The lesson intentionally\ntargets the aspect(s) of rigor\n(conceptual understanding,\nprocedural skill and fluency,\napplication) called for by the\nstandard(s) being addressed."
), response = structure(c(1L, 1L, 1L, 1L, 3L, 2L, 2L, 3L, 2L, 
2L, 3L, 3L), levels = c("Not Yet", "In Process", "Established"
), class = "factor"), n = c(6L, 5L, 5L, 4L, 3L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L), percent = c(75, 62.5, 62.5, 50, 37.5, 25, 25, 12.5, 
12.5, 12.5, 12.5, 12.5), color = c("white", "white", "white", 
"white", "black", "black", "black", "black", "black", "black", 
"black", "black"), size = c(8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 
7)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))

代码看起来像这样:

library(tidyverse)
focus_graph |>
  ggplot(aes(x = question, y = percent)) +
  geom_col(aes(fill = response),
           position = ggplot2::position_stack(reverse = TRUE)
           ) +
  geom_text(aes(label = paste0(round(percent), "%\n(n = ", n, ")"), color = color, size = size), 
            position = position_stack(reverse = FALSE, vjust = 0.5),
            fontface = "bold",
            show.legend = FALSE) +
  scale_fill_manual(values = c("#040404", "#02587A", "#00ACF0")) +
  scale_color_identity() +
  scale_y_continuous(labels = scales::percent_format(scale = 1)) +
  scale_size_continuous(range = c(5, 10)) +
  coord_flip() +
  labs(x = "", y = "", title = stringr::str_wrap("High-Quality Mathematical Content: Does the content of the lesson reflect the key instructional shifts required by college and career ready mathematics standards?", width = 50)) +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        legend.text = element_text(size = 17),
        plot.title = element_text(face = "bold", size = 20),
        axis.text.y = element_text(size = 16, face = "bold"))

结果图像有一个错误的geom_text没有正确居中,它奇怪地包括堆叠条的一部分中的38%和12%,而所有其他的都正确放置。
我尝试了上面的代码,得到了下面的图像。

l0oc07j2

l0oc07j21#

我已经解决了这个问题,只是通过添加group = response参数到geom_text的aes参数,这是最终的代码。

ggplot() +
    geom_col(aes(x = question, y = percent, fill = response),
      position = position_stack(vjust = 0.5, reverse = TRUE),
      alpha = 0.95
    ) +
    geom_text(aes(x = question, y = percent, label = paste0(round(percent), "%\n(n = ", n, ")"), color = color, size = size, group = response),
      position = position_stack(vjust = 0.5, reverse = TRUE),
      fontface = "bold",
      show.legend = FALSE
    ) +
    scale_fill_manual(values = tl_palette(color = "blue", n = unique_colors_n + 1, theme = "dark")[-1]) +
    scale_color_identity() +
    scale_y_continuous(labels = scales::percent_format(scale = 1)) +
    scale_size_continuous(range = c(5, 10)) +
    coord_flip() +
    labs(x = "", y = "", title = title) +
    theme_tl(legend = TRUE) +
    theme(
      legend.position = "bottom",
      legend.title = element_blank(),
      legend.text = element_text(size = 17),
      legend.key.width = unit(2, "cm"),
      legend.key.height = unit(1.5, "cm"),
      plot.title = element_text(face = "bold", size = 20),
      axis.text.y = element_text(size = 16, face = "bold")
    )

相关问题