Skip to content

geom_vline()/geom_hline() doesn't work with coord_flip() #1519

Closed
@kmaheshkulkarni

Description

@kmaheshkulkarni

I am trying to plot a histogram and normal distribution chart overlaying each other. I also need to use coord_flip() function to rotate the chart and display it horizontally. The plot has occurred correctly and it rotated well enough, but, I also need to add a vertical line over this histogram + normal distribution displaying it's mean. For this process, I have first created the histogram + normal distribution chart and used coord_flip over it. After rotating the plot, I have used geom_vline and the line successfully appeared on the plot. But, when I try to convert this plot into ggplotly, the line disappears and only the rotated histogram + normal distribution chart is displayed. Please find the code below.

Plot without ggplotly() function

![Rplot](https://user-images.githubusercontent.com/18054137/56338721-d7991c80-61c8-11e9-9b52-63c451a977ec.png)

Plot with ggplotly()

![newplot (2)](https://user-images.githubusercontent.com/18054137/56338755-fac3cc00-61c8-11e9-8f46-a773c9ee6b3b.png)

code of Plot:

df <- data.frame(x = c(150,160,170,180,190,200,210,220,225,230,231,232,235,240,245,250,255,260,265,270,275,278,280,283,285,288,290,293,295,300,300,300,301,305,311,315,318,320,323,325,330,335,340,345,350,355,365,368,370,371,373,375,380,390,400,410,420,430,440,450))

hist_nd <- ggplot(df, aes(x)) + 
geom_histogram(aes(y = stat(density)), bins = 10, fill = "steelblue", alpha = 0.3) + 
stat_function( fun = dnorm, args = list(mean = mean(df$x), 
sd = sd(df$x)), lwd = 1, col = '#01579b' ) + 
geom_vline(aes(xintercept = mean(x)), color="blue", linetype="dashed", size=1) + theme_minimal() + coord_flip()

hist_nd

p <- ggplotly(hist_nd)

p

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions