How to remove / change shortcut in plot geometry in R?

Using this example from the plot, I am trying to build two vertical lines to represent the middle and middle.

Playable code

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() 

Problem

I want to suppress the y-value of -2.2, which is displayed next to the red text β€œaverage”. However, I want the text β€œmedium” to be displayed as shown in the screenshot below. That is, I just want to suppress the label that I put with a black cross. The same problem applies to the median line.

How to hide -2.2?

My inoperative attempt

 #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) 

This attempt still displays 0 (screenshot below):

How to hide 0?

+5
source share
1 answer

Decision

 #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) 

Result (screenshot)

plotlyHoverSolution

explainer

The p object is a list of plotly options, but does not include all parameters. The R API for plotly implicitly use all the default values. Therefore, if you need something else, you need to add a parameter name (for example, hoverinfo ) with user settings (for example, "name+x" ).

Flat reference material

+2
source

Source: https://habr.com/ru/post/1238228/


All Articles