I am trying to make interactive density plots for my data in R. I am able to make a basic density plot for some simulated data:
library(plotly)
library(ggplot2)
a = rnorm(100,10,10)
b = plot(density(a))
b
Now, I am trying to make this an interactive plot. I tried the following line of code, but I get an empty plot:
ggplotly(b)
Therefore, I tried to replicate the original density plot "b" using the "ggplot2" library:
p <- ggplot(a, aes(x=a)) +
geom_density()
ggplotly(p)
This seems to work:
But at the same time I get an error:
Error: `data` must be a data frame, or other object coercible by `fortify()`, not a numeric vector
Can someone please explain why this error comes?
Thanks
Try this
a <- data.frame(a)
p <- ggplot(a, aes(x=a)) +
geom_density()
ggplotly(p)
Reasons for your error messages
1. The reason ggplotly(b)
gives an error is that you need to give a ggplot object to ggplotly
.
2.
a = rnorm(100,10,10)
p <- ggplot(a, aes(x=a)) + # does not work
geom_density()
The reason you have an error for this because a
is not a data.frame, it is a matrix.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.