如何 remove/change 在 R 中 plotly geom_vline 中的标签?
How to remove/change the label in a plotly geom_vline in R?
使用 this example from plotly 我试图绘制两条垂直线来表示平均值和中位数。
可重现代码
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()
问题
我想抑制显示在红色文本 'average' 旁边的 y 值 -2.2。但是,我希望文本 'average' 像下面的屏幕截图中那样显示。 IE。我只想压制我打了个黑叉的标签。
同样的问题也适用于中线。
我的无效尝试
#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
(下面的屏幕截图):
解决方案
#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", yaxt="n") +
geom_histogram(binwidth=.5, position="dodge")
#create plotly object
p <- plotly_build(gg)
#append additional options to plot object
p$data[[1]]$hoverinfo <- "name+x" #hover options for 'average'
p$data[[2]]$hoverinfo <- "name+x" #hover options for 'median'
#display plot
plotly_build(p)
结果(截图)
解释器
对象 p
是 plotly
个选项的列表,但不包括 所有 个选项。看起来 R
API 到 plotly
隐含地 使用所有默认值。因此,如果您想要不同的东西,您需要在选项名称(例如 hoverinfo
)后附加自定义设置(例如 "name+x"
)。
使用 this example from plotly 我试图绘制两条垂直线来表示平均值和中位数。
可重现代码
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()
问题
我想抑制显示在红色文本 'average' 旁边的 y 值 -2.2。但是,我希望文本 'average' 像下面的屏幕截图中那样显示。 IE。我只想压制我打了个黑叉的标签。 同样的问题也适用于中线。
我的无效尝试
#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
(下面的屏幕截图):
解决方案
#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", yaxt="n") +
geom_histogram(binwidth=.5, position="dodge")
#create plotly object
p <- plotly_build(gg)
#append additional options to plot object
p$data[[1]]$hoverinfo <- "name+x" #hover options for 'average'
p$data[[2]]$hoverinfo <- "name+x" #hover options for 'median'
#display plot
plotly_build(p)
结果(截图)
解释器
对象 p
是 plotly
个选项的列表,但不包括 所有 个选项。看起来 R
API 到 plotly
隐含地 使用所有默认值。因此,如果您想要不同的东西,您需要在选项名称(例如 hoverinfo
)后附加自定义设置(例如 "name+x"
)。