R语言 如何在ggplot2中使数据点覆盖条带?

xurqigkl  于 2023-10-13  发布在  其他
关注(0)|答案(3)|浏览(176)

问题是,我想获得样本图,但这里我的qqplot数据点躺在带上。我不知道该怎么改变。
下面是我们想要的结果:

我得到了什么:

这是我解决问题的代码

  1. my_theme <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), plot.title = element_text(size = 10),
  2. panel.background = element_rect(fill = "white", color = "gray50", size=3.5),
  3. strip.clip="off",
  4. strip.background = element_rect(color = "gray50",fill = "lightgray", size = 1.5),
  5. legend.background=element_rect(fill = "lightgray", color="gray50", size=1.5),
  6. legend.key = element_rect(fill = "lightgray", size=4),
  7. axis.ticks.x = element_blank(),
  8. axis.ticks.y = element_blank(),
  9. legend.key.height = unit(1, "cm"),
  10. legend.key.width = unit(1, "cm"))
  11. ggplot(data = melted_df, aes(sample = value, color = Distribution)) +
  12. stat_qq(position = "identity") + geom_qq_line() +
  13. facet_wrap(~Distribution, scales = "free") + xlab('Theoretical Quantiles') +
  14. ylab('Sample Quantiles') +
  15. ggtitle('QQ Plot for Different Distributions against Qnorm!') + my_theme
krcsximq

krcsximq1#

只是想澄清一下:这些点绘制在panel.background的顶部,而不是strip.background
解决这个问题的一个选择是设置panel.ontop = TRUE,这也需要将panel.background的填充颜色设置为"transparent"。这样的panel.background和所有(!!)主题定义的其他panel.xxx元素绘制在图的顶部。
使用基于ggplot2::mpg的最小可重复示例:

  1. library(ggplot2)
  2. my_theme <- theme(
  3. panel.grid.major = element_blank(),
  4. panel.grid.minor = element_blank(),
  5. plot.title = element_text(size = 10),
  6. panel.background = element_rect(
  7. fill = "transparent",
  8. color = "gray50", size = 3.5
  9. ),
  10. strip.clip = "off",
  11. strip.background = element_rect(
  12. color = "gray50",
  13. fill = "lightgray", size = 1.5
  14. ),
  15. legend.background = element_rect(
  16. fill = "lightgray",
  17. color = "gray50", size = 1.5
  18. ),
  19. legend.key = element_rect(fill = "lightgray", size = 4),
  20. axis.ticks.x = element_blank(),
  21. axis.ticks.y = element_blank(),
  22. legend.key.height = unit(1, "cm"),
  23. legend.key.width = unit(1, "cm"),
  24. panel.ontop = TRUE
  25. )
  26. #> Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
  27. #> ℹ Please use the `linewidth` argument instead.
  28. #> This warning is displayed once every 8 hours.
  29. #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
  30. #> generated.
  31. ggplot(data = mpg, aes(sample = hwy, color = class)) +
  32. stat_qq(position = "identity") +
  33. geom_qq_line() +
  34. facet_wrap(~class, scales = "free") +
  35. xlab("Theoretical Quantiles") +
  36. ylab("Sample Quantiles") +
  37. ggtitle("QQ Plot for Different Distributions against Qnorm!") +
  38. my_theme

展开查看全部
yzxexxkh

yzxexxkh2#

您可以简单地将panel.border添加到theme。这是一个矩形元素,始终绘制在面板上:

  1. ggplot(data = melted_df, aes(sample = value, color = Distribution)) +
  2. stat_qq(position = "identity") +
  3. geom_qq_line() +
  4. facet_wrap(~Distribution, scales = "free") +
  5. xlab('Theoretical Quantiles') +
  6. ylab('Sample Quantiles') +
  7. ggtitle('QQ Plot for Different Distributions against Qnorm!') +
  8. my_theme +
  9. theme(panel.border = element_rect(fill = NA, linewidth = 3.5,
  10. color = 'gray50'))

kq0g1dla

kq0g1dla3#

您没有提供一个可重复的示例,但问题很可能是您正在使用库来制作qqplot并添加qq行。这些库不能很好地与较厚的边框配合使用。很可能他们正在修改scale_x_continuousexpand参数。
请参阅此处的示例(仅显示最后一组图,因为这些图是指向轴的图,将绘制在面板边框的顶部):

  1. library(ggplot2)
  2. ggplot(
  3. data = data.frame(x = c(0, 0, 1, 1),
  4. y = c(0, 1, 0, 1),
  5. facet = c('A', "A", "A", "A")),
  6. aes(x, y)
  7. ) +
  8. geom_point() +
  9. facet_wrap(~facet)
  10. ggplot(
  11. data = data.frame(x = c(0, 0, 1, 1),
  12. y = c(0, 1, 0, 1),
  13. facet = c('A', "A", "A", "A")),
  14. aes(x, y)
  15. ) +
  16. geom_point() +
  17. facet_wrap(~facet) +
  18. theme(
  19. panel.background = element_rect(fill = "white", color = "gray50", size=3.5)
  20. )
  21. ggplot(
  22. data = data.frame(x = c(0, 0, 1, 1),
  23. y = c(0, 1, 0, 1),
  24. facet = c('A', "A", "A", "A")),
  25. aes(x, y)
  26. ) +
  27. geom_point() +
  28. facet_wrap(~facet) +
  29. theme(
  30. panel.background = element_rect(fill = "white", color = "gray50", size=3.5),
  31. strip.clip = "off",
  32. strip.background = element_rect(color = "gray50",fill = "lightgray", size = 1.5)
  33. )
  34. ggplot(
  35. data = data.frame(x = c(0, 0, 1, 1),
  36. y = c(0, 1, 0, 1),
  37. facet = c('A', "A", "A", "A")),
  38. aes(x, y)
  39. ) +
  40. geom_point(size = 5) +
  41. facet_wrap(~facet) +
  42. scale_x_continuous(expand = expansion(0, 0)) +
  43. scale_y_continuous(expand = expansion(0, 0))

展开查看全部

相关问题