I am trying to hide the plotly toolbar when displaying a reactive ggplot chart within a Shiny app. Currently my code looks like this:
renderPlotly( ggplot(users_activated(), aes(x = month, y = n)) +
geom_bar(stat = "identity"))
I have tried config(displayModeBar = F) to no avail.
You need to convert your ggplot object to a plotly object before you can render it as one, or use plotly functions on it. ggplotly does that. Below, is an example using mpg dataset.
ggplotly(ggplot(mpg, aes(class, displ, color = manufacturer)) +
geom_point()
) %>%
config(displayModeBar = F)
Related
I have a bar chart that I want to make interactive in my R Shiny app using plotly.
When it is rendered as a plot, everything is fine using ggplot:
#Bar plots
ggplot(data = df_continents) +
geom_bar(aes(x=country, y=coal_co2), stat="identity", fill="#2596be") +
scale_y_continuous(labels = number_format())`
But, when I try to use plotly, the format gets messed up in an incredible way:
#Bar chart - Adding Labels
ticklabels <- seq(from=0, to=round(max(df$coal_co2*100000)), by=100000)
ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))
output$bar <- renderPlotly({
df %>%
plot_ly(x =~ country, y = ~ coal_co2, type = "bar", marker = list(color = "#2596be")) %>%
layout(yaxis=list(tickvals = ticklabels,
ticktext = ticktexts
))
})
So, I'm not sure what is the problem here, why do I have those white horizontal lines inside the bars?. How do I get my bar chart plot to look like the first screenshot?.
I'm trying to visualize a dataset with ggplot and make it interactive with shiny. I want to be able to see data for the specific state I select.
This is the error I get and the image is an example of what I'm trying to get.
the code is as follows
output$barplot <- renderPlot({
ggplot(data, aes(x = input$bar_x, y = value))+
geom_bar(aes(fill = variable), stat = "identity", position = "dodge")
})
Where bar_x is the Name of the state.
The table is as shown below. Any help would be appreciated!
Take the following "Horizontal grid" example from the Plotly ggplot2 Library
I want to omit the labels from the X axes in both subplots, like:
The way to do this should be, allegedly, by using the layout() function and including configuration parameter xaxis = list(showticklabels = FALSE). See however the output in the following reprex:
library(tidyverse)
library(reshape2)
library(plotly)
p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1) +
facet_grid(. ~ sex)
ggplotly(p) %>%
layout(xaxis = list(showticklabels = FALSE))
As you can see, only the first subplot is affected.
I have already tried by rendering each subplot as a plotly object, and then using the subplot function:
tips %>% group_split(sex) %>% map(
~{
p <- ggplot(., aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
ggplotly(p)
}
) %>%
subplot(shareY = TRUE) %>%
layout(xaxis = list(showticklabels = FALSE))
The result is the same.
Also, calling layout(., xaxis = list(showticklabels = FALSE)) for each subplot individually fails, as subplot() apparently overrides the layout of the subplots.
Additionally, inspection of the JSON object seems to show that only one layout attribute is generated for the whole subplot, which I understand should control the properties of all subplots.
Any idea on how to solve this? Any help would be much appreciated!
To achieve what you desire, removing all x-axis labels from the ticks, I would remove them using your ggplot theme. Thus before you call ggplotly
p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) +
geom_point(shape=1) +
facet_grid(. ~ sex) +
theme( axis.text.x=element_blank()) # remove x-labels
p
ggplotly(p)
In the following shiny app, I want to add some spaces between mainPanel and a siderbarPanel in the bottom.
Here, I can't read the x axis ticks, I tried to rotate them but it doesnt improve the thing.
I also changed the height and width of the plotlyOutput but doesn't work.
I tried to add an HTML("<br><br><br>") but doesnt work too, the problem is probably from ggplot or plotly ?
My ggplot script is :
ggplot(data = df, aes(x = perimetre_commercial_estime,
y = nbr_ligne,
fill = perimetre_commercial_estime)) +
geom_bar(stat="identity") + theme_light() +
theme(axis.text.x=element_text(angle=30,hjust=1,vjust=0.5,size=6),
axis.title.x=element_blank(),
legend.position="top", legend.direction="horizontal") +
scale_fill_discrete("")
plotly doesnt support horizontal legend, so the graphic below is normal.
Thanks
This Plotly Setting Graph Size in R article might be helpful. Since you don't include all code, I cannot confirm entirely. Please let me know.
library(plotly)
library(dplyr)
gp = ggplot(
data = mtcars %>% add_rownames(),
aes( x = rowname, y = mpg )
) + geom_point() +
theme(axis.text.x=element_text(angle=30,hjust=1,vjust=0.5,size=6),
axis.title.x=element_blank())
ggplotly(gp) %>%
layout(
margin = list(
b=100
)
)
Is there a way to code the hovermode when using plotly with R and ggplot2?
Currently, my code is:
plot <- ggplot(data, aes(var1, var2, text=var3)) +
geom_point()
py$ggplotly(plot)
And I want the plotly graph to automatically have the hover mode set to "show closest data on hover" rather than "compare data on hover".
The answer from 'mkcor' didn't work when trying to do the same in Shiny. I kept getting an 'unused argument' error. For anyone else with the same problem, this worked for me...
Suppose this is my basic plot:
p <- ggplot(myDf, aes(x=x, y=y )) + geom_point(size = 3, shape = 0)
You can convert the ggplot object to a plotly object:
ggObj <- plotly(p)
Then you can change the hovermode like this:
layout(ggObj, hovermode = 'closest')
Add the following argument when calling ggplotly:
py$ggplotly(plot, kwargs=list(layout=list(hovermode="closest")))