英文:
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]))
在这里,密度图显示出来。
当我给图加注释时:
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]))
然后图就消失了:
有人知道为什么会发生这种情况吗?
谢谢。
英文:
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 <- 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]))
here the density plot shows up.
When I annotate the plot:
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]))
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)
)
英文:
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 <- data.frame(spread = rnorm(1000))
# quantiles
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)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论