问题
I am trying to create a Shiny Dashboard which contains Plots also. Plotly is such a great a package that it provides great visualization and interactivity to the users.
I know that the best way to convert a ggplot to plotly is by using the ggplotly() function. But when i am using the ggplotly, i am facing issues on the size and appearance of the plots in the dashboard. I had requested a question on this before (ggplotly not working properly when number are facets are more ) and also i tried the plotly community to overcome this issue but i believe that this is a Bug.
At last i have made my mind that the I need to manually convert the ggplot to plot_ly. But while doing this I am facing some road blocks and also i am not very sure on whether what i am doing is right or its the easiest way.
The code which i used to generate the ggplot is
Source_Data_dupli <-
data.frame(
key = c(1, 1, 1, 2, 2, 2, 3, 3, 3,4,4,5,5,6,7),
Product_Name = c(
"Table",
"Table",
"Chair",
"Table",
"Bed",
"Bed",
"Sofa",
"Chair",
"Sofa",
"Table",
"Bed",
"Chair",
"Table",
"Bed",
"Bed"
),
Product_desc = c("XX", "XXXX", "YY", "X", "Z", "ZZZ", "A", "Y",
"A","Y","XX", "XXXX", "YY", "X", "Z"),
sd =c(0.1,0.3,0.4,0.5,0.6,0.7,0.7,0.8,0.5,0.9,0.5,0.2,0.3,0.4,0.5),
Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6, 7,6,8,9,12,34)
)
################ ggplot ####################
Source_Data %>%
ggplot(aes(Product_Name, Cost, fill = Product_desc)) +
geom_col(position = position_dodge2(width = .9, preserve = "single")) +
geom_errorbar(aes(ymin=Cost-sd, ymax=Cost+sd), position =
position_dodge2(width = .9, preserve = "single", padding = .5)) +
geom_text(aes(y=Cost+sd, label=Cost), position = position_dodge2(width=.9),
vjust = -1) +
facet_wrap(~key, scales = "free_x", strip.position = "bottom") +
theme(strip.placement = "outside") +
theme_bw()
Thanks for the help in Advance.
David
来源:https://stackoverflow.com/questions/58294613/how-to-convert-a-ggplot-to-plot-ly