5
使用this example from plotly我试图绘制两条垂直线来表示平均值和中值。如何在R中绘制geom_vline中的标签?
重现代码
library(plotly)
# data
df1 <- data.frame(cond = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200),rnorm(200, mean=.8)))
df2 <- data.frame(x=c(.5,1),cond=factor(c("A","B")))
# graph
ggplot(data=df1, aes(x=rating, fill=cond)) +
geom_vline(aes(xintercept=mean(rating, na.rm=T))
, color="red", linetype="dashed", size=1, name="average") +
geom_vline(aes(xintercept=median(rating, na.rm=T))
, color="blue", linetype="dashed", size=1, name="median") +
geom_histogram(binwidth=.5, position="dodge")
ggplotly()
问题
我想抑制被旁边的红色文字显示的 '平均' 的y值-2.2。不过,我希望文字“平均”显示为与下面屏幕截图中的相同。即我只想压制我穿过黑色十字的标签。 同样的问题适用于中线。
我不工作的尝试
#plot
gg <- ggplot(data=df1, aes(x=rating, fill=cond)) +
geom_vline(aes(xintercept=mean(rating, na.rm=T))
, color="red", linetype="dashed", size=1, name="average")+
geom_vline(aes(xintercept=median(rating, na.rm=T))
, color="blue", linetype="dashed", size=1, name="median") +
geom_histogram(binwidth=.5, position="dodge")
p <- plotly_build(gg)
# p$data[[1]]$y[1:2] <- " " # another attempt, but the line doesn't display at all
p$data[[1]]$y <- NULL # delete the y-values assigned to the average line
plotly_build(p)
这种尝试仍然(下图)显示0
: