在ggplot2上添加注释会导致密度图消失。

huangapple go评论60阅读模式
英文:

adding annotations on ggplot2 causes density plot to disappear

问题

以下是您提供的内容的中文翻译:

我正在为一个项目制作多个密度图。当我在添加到图中的分位数竖线上添加注释时,会导致密度图消失。

d2 <- pt_diff %>%
  summarize(lower = quantile(spread, probs = 0.40),
            upper = quantile(spread, probs = 0.60))
p <- ggplot(pt_diff, aes(x = spread)) +
      geom_density(aes(fill = 'red')) +
      geom_vline(data = d2, aes(xintercept = lower)) +
      geom_vline(data = d2, aes(xintercept = upper)) +
      ggtitle(paste0(current_week$home.team.name[k],' vs. ', current_week$away.team.name[k]))

在这里,密度图显示出来。

在ggplot2上添加注释会导致密度图消失。

当我给图加注释时:

p <- ggplot(pt_diff, aes(x = spread)) +
     geom_density(aes(fill = 'red')) +
     geom_vline(data = d2, aes(xintercept = lower)) +
     annotate('text', label = round(d2$lower, 2), x = -30, y = 100) + 
     geom_vline(data = d2, aes(xintercept = upper)) +
     annotate('text', label = round(d2$upper,2), x = 4.5, y = 200) +
     ggtitle(paste0(current_week$home.team.name[k],' vs. ', current_week$away.team.name[k]))

然后图就消失了:

在ggplot2上添加注释会导致密度图消失。

有人知道为什么会发生这种情况吗?

谢谢。

英文:

I'm making a number of density plots for a project I'm working on. When I add annotations to the quantile vline that I add to the graph, it causes the density plot to disappear.

  d2 &lt;- pt_diff %&gt;%
    summarize(lower = quantile(spread, probs = 0.40),
              upper = quantile(spread, probs = 0.60))
  p &lt;- ggplot(pt_diff, aes(x = spread)) +
        geom_density(aes(fill = &#39;red&#39;)) +
        geom_vline(data = d2, aes(xintercept = lower)) +
        geom_vline(data = d2, aes(xintercept = upper)) +
        ggtitle(paste0(current_week$home.team.name[k],&#39; vs. &#39;, current_week$away.team.name[k]))

here the density plot shows up.

在ggplot2上添加注释会导致密度图消失。

When I annotate the plot:

   p &lt;- ggplot(pt_diff, aes(x = spread)) +
         geom_density(aes(fill = &#39;red&#39;)) +
         geom_vline(data = d2, aes(xintercept = lower)) +
         annotate(&#39;text&#39;, label = round(d2$lower, 2), x = -30, y = 100) + 
         geom_vline(data = d2, aes(xintercept = upper)) +
         annotate(&#39;text&#39;, label = round(d2$upper,2), x = 4.5, y = 200) +
         ggtitle(paste0(current_week$home.team.name[k],&#39; vs. &#39;, current_week$away.team.name[k]))

then the plot disappears:
在ggplot2上添加注释会导致密度图消失。

Does anyone have any idea why this would happen?

thanks.

答案1

得分: 2

以下是您要翻译的内容:

正如 @r2evans 在评论中已经提到的,文本的 y 值太高了。请记住,在 0.0 到 y 值之间。

这里提供了将注释的 x 和 y 值设置为主要值和最大值的解决方案:

library(ggplot2)

# 随机数据
set.seed(123)
pt_diff <- data.frame(spread = rnorm(1000))

# 分位数
d2 <- pt_diff %>%
  summarize(lower = quantile(spread, probs = 0.40),
            upper = quantile(spread, probs = 0.60))

ggplot(pt_diff, aes(x = spread)) +
  geom_density(aes(fill = 'red')) +
  geom_vline(data = d2, aes(xintercept = lower)) +
  annotate('text', label = round(d2$lower, 2), x = min(pt_diff$spread), y = max(density(pt_diff$spread)$y)/2) + 
  geom_vline(data = d2, aes(xintercept = upper)) +
  annotate('text', label = round(d2$upper,2), x = max(pt_diff$spread), y = max(density(pt_diff$spread)$y)/2) +
  ylim(0, max(density(pt_diff$spread)$y)*1.5)
)

在ggplot2上添加注释会导致密度图消失。

英文:

As @r2evans already stated in the comments thy y values are to high for the text. Remember you have in 0.0.. y-values.

Here is a solution setting the x and y values of the annotation to the main and max of the values:

library(ggplot2)

# random data
set.seed(123)
pt_diff &lt;- data.frame(spread = rnorm(1000))

# quantiles
d2 &lt;- pt_diff %&gt;%
  summarize(lower = quantile(spread, probs = 0.40),
            upper = quantile(spread, probs = 0.60))


ggplot(pt_diff, aes(x = spread)) +
  geom_density(aes(fill = &#39;red&#39;)) +
  geom_vline(data = d2, aes(xintercept = lower)) +
  annotate(&#39;text&#39;, label = round(d2$lower, 2), x = min(pt_diff$spread), y = max(density(pt_diff$spread)$y)/2) + 
  geom_vline(data = d2, aes(xintercept = upper)) +
  annotate(&#39;text&#39;, label = round(d2$upper,2), x = max(pt_diff$spread), y = max(density(pt_diff$spread)$y)/2) +
  ylim(0, max(density(pt_diff$spread)$y)*1.5)
)

在ggplot2上添加注释会导致密度图消失。

huangapple
  • 本文由 发表于 2023年5月14日 19:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定