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

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

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

我得到了什么:

这是我解决问题的代码

my_theme <-   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), plot.title = element_text(size = 10),
                    panel.background = element_rect(fill = "white", color = "gray50", size=3.5),
                    strip.clip="off",
                    strip.background = element_rect(color = "gray50",fill = "lightgray", size = 1.5),
                    legend.background=element_rect(fill = "lightgray", color="gray50", size=1.5),
                    legend.key = element_rect(fill = "lightgray", size=4),
                    axis.ticks.x = element_blank(),
                    axis.ticks.y = element_blank(),
                    legend.key.height = unit(1, "cm"),
                    legend.key.width = unit(1, "cm"))

ggplot(data = melted_df, aes(sample = value, color = Distribution)) +
  stat_qq(position = "identity") + geom_qq_line() +
  facet_wrap(~Distribution, scales = "free") + xlab('Theoretical Quantiles') + 
  ylab('Sample Quantiles') + 
  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的最小可重复示例:

library(ggplot2)

my_theme <- theme(
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  plot.title = element_text(size = 10),
  panel.background = element_rect(
    fill = "transparent",
    color = "gray50", size = 3.5
  ),
  strip.clip = "off",
  strip.background = element_rect(
    color = "gray50",
    fill = "lightgray", size = 1.5
  ),
  legend.background = element_rect(
    fill = "lightgray",
    color = "gray50", size = 1.5
  ),
  legend.key = element_rect(fill = "lightgray", size = 4),
  axis.ticks.x = element_blank(),
  axis.ticks.y = element_blank(),
  legend.key.height = unit(1, "cm"),
  legend.key.width = unit(1, "cm"),
  panel.ontop = TRUE
)
#> Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

ggplot(data = mpg, aes(sample = hwy, color = class)) +
  stat_qq(position = "identity") +
  geom_qq_line() +
  facet_wrap(~class, scales = "free") +
  xlab("Theoretical Quantiles") +
  ylab("Sample Quantiles") +
  ggtitle("QQ Plot for Different Distributions against Qnorm!") +
  my_theme

yzxexxkh

yzxexxkh2#

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

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

kq0g1dla

kq0g1dla3#

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

library(ggplot2)
ggplot(
  data = data.frame(x = c(0, 0, 1, 1),
                    y = c(0, 1, 0, 1),
                    facet = c('A', "A", "A", "A")),
  aes(x, y)
) + 
  geom_point() +
  facet_wrap(~facet)

ggplot(
  data = data.frame(x = c(0, 0, 1, 1),
                    y = c(0, 1, 0, 1),
                    facet = c('A', "A", "A", "A")),
  aes(x, y)
) + 
  geom_point() +
  facet_wrap(~facet) +
  theme(
    panel.background = element_rect(fill = "white", color = "gray50", size=3.5)
  )

ggplot(
  data = data.frame(x = c(0, 0, 1, 1),
                    y = c(0, 1, 0, 1),
                    facet = c('A', "A", "A", "A")),
  aes(x, y)
) + 
  geom_point() +
  facet_wrap(~facet) +
  theme(
    panel.background = element_rect(fill = "white", color = "gray50", size=3.5),
    strip.clip = "off",
    strip.background = element_rect(color = "gray50",fill = "lightgray", size = 1.5)
  ) 

ggplot(
  data = data.frame(x = c(0, 0, 1, 1),
                    y = c(0, 1, 0, 1),
                    facet = c('A', "A", "A", "A")),
  aes(x, y)
) + 
  geom_point(size = 5) +
  facet_wrap(~facet) +
  scale_x_continuous(expand = expansion(0, 0)) +
  scale_y_continuous(expand = expansion(0, 0))

相关问题