2015-12-15 2 views
5

Mit this example from plotly versuche ich zwei vertikale Linien zu plotten, um den Mittelwert und Median darzustellen.Wie entferne/ändere ich die Beschriftung in einer Plotly geom_vline in R?

Reproduzierbare-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

ich den y-Wert -2.2 zu unterdrücken möchten, die neben dem roten Text angezeigt wird 'durchschnittlich'. Ich möchte jedoch, dass der Text "Durchschnitt" wie im Screenshot unten angezeigt wird. I.e. Ich möchte nur das Etikett unterdrücken, durch das ich ein schwarzes Kreuz gelegt habe. Das gleiche Problem gilt für die Medianlinie.

How to hide the -2.2?

Mein Nicht-Arbeits Versuch

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

Dieser Versuch zeigt weiterhin eine 0 (Abbildung unten):

How to hide the 0?

Antwort

2

Lösung

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

Ergebnis (Screenshot)

plotlyHoverSolution

Explainer

Das Objekt p ist eine Liste von plotly Optionen, aber nicht enthalten alle Optionen. Es scheint die R API zu plotlyimplizit verwendet alle Standardwerte. Wenn Sie also etwas anderes möchten, müssen Sie den Optionsnamen (z. B. hoverinfo) an die benutzerdefinierten Einstellungen anhängen (z. B. "name+x").

Plotly reference material

Verwandte Themen