I have a plot that I am rendering in shiny using plotly and ggplot2. However, I do not want the option bar that appears on hover to appear. Is there a way to use ggplotly(p) and remove the option bar?
There is a great answer on community plotly the short version:
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
Using ggplotly:
p <- ggplot(d, aes(carat, price)) + geom_point()
ggplotly(p) %>% config(displayModeBar = F)
If you are not using ggplotly you can do:
plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
mode = "markers", color = carat, size = carat) %>% config(displayModeBar = F)
Related
I would like to zoom in on the y-axis on a plotly plot that uses rangeslider.
A reproducible example:
library(ggplot2)
library(plotly)
p <- ggplot(faithful, aes(x = waiting)) +
geom_histogram(bins = 30)
p <- ggplotly(p)
p <- rangeslider(p)
p
The way I can zoom is the following:
However, I would like to be able to also zoom like this (which is done by not adding a rangeslider):
I assume this can be done by using something along the lines of
p <- layout(p, dragmode = "zoom")
but I haven't been able to make this work.
In reference to this GitHub issue, it can be done by setting fixedrange to FALSE for yaxis.
library(ggplot2)
library(plotly)
ggplot(faithful, aes(x = waiting)) +
geom_histogram(bins = 30) -> p
ggplotly(p) %>%
layout(xaxis = list(rangeslider = list()),
yaxis = list(fixedrange = FALSE)) -> p
I need to convert into widget a simple ggplot, a stacked bar with uncertainty.
The data:
world.tot <- data.frame('country'='world', 'GHG'=c('CH4', 'CO2','N2O'),
'emi'=c(6e+6, 3e+6, 1+6),
'unc.min'=8561406, 'unc.max'=14027350)
and the ggplot:
p2 <- ggplot(world.tot) +
geom_bar(aes(x=country,y=emi,fill=GHG), stat='identity', position='stack' ) +
geom_errorbar(aes(x=country, ymin=unc.min, ymax=unc.max), width=0.2) +
theme(axis.title. x=element_blank(), axis.title. y=element_blank()) +
theme(legend.position='none')
When I try: ggplotly(p2) only the stacked bars are converted, not the error bar. Any advice?
Alternatively, I could use plot_ly to create the plot, but cannot manage to add the error bar:
plot_ly(world.tot, x=~country. y=~emi, color=~GHG,type=bar,
error_y=~list(array(c(unc.min, unc.max))) %>%
layout(barmode='stack')
This produces error bars to all shares of the stacked histogram, while I need only one error at the top of the stacked histogram.
Any help is appreciated
You can prepare a data.frame that has only one error size per group
library(dplyr)
world.err <- world.tot %>%
group_by(country) %>%
summarise(emi = sum(emi), unc.min = 8561406, unc.max = 14027350)
And plot the errors as a separate trace
plot_ly(world.tot) %>%
add_bars(x = ~country, y = ~emi, color = ~GHG, type='bar') %>%
add_trace(x = ~country, y = ~emi, data = world.err,
showlegend = F, mode='none', type='scatter',
error_y = ~list(array = c(unc.min, unc.max), color = '#000000')) %>%
layout(barmode='stack')
I'm trying to make a scatter plot with R plotly, where the points are colored according to a gradient, and on top of that I'd like to text annotate several points, where I'd like the color of the text annotation to follow the same color gradient.
Here's a toy data:
set.seed(1)
df <- data.frame(x=rnorm(100),y=-1*log10(runif(100,0,1)),id=paste0("id",1:100),stringsAsFactors = F)
Here's the colored scatter plot:
library(plotly)
library(dplyr)
scatter.plot <- plot_ly(type='scatter',mode='markers',x=~df$x,y=~df$y,color=~df$y,colors=c("blue","red"),showlegend=FALSE) %>%
layout(xaxis=list(title="Effect Size",zeroline=F),yaxis=list(title="Significance",zeroline=F))
which gives:
Then, I'm trying to add the text annotation.
First, I'm creating the annotation data:
ann.df <- df[sample(100,5,replace = F),]
Then I'm trying this to add the annotation text:
scatter.plot <- scatter.plot %>% layout(annotations = list(x=ann.df$x,y=ann.df$y,text=ann.df$id,xref="x",yref="y",showarrow=T,arrowhead=5,arrowcolor=ann.df$y,ax=20,ay=-40,colors=c("blue","red"),font=list(size=15,color=ann.df$y,colors=c("blue","red"))))
The text is added but not color coded:
Any idea how to get the text colored according df$y with the c("blue","red") gradient ?
I am not sure how to achieve this using plotly. Maybe someone will have a better idea but in the meantime, it might be easier by simply using ggplotly to get what you want.
You could try this:
a <- ggplot(data = df, aes(x = x, y = y, color = y, label1 = x, label2 = y)) +
geom_point() +
theme_bw() +
scale_color_continuous(low = "blue", high = "red")
b <- a +
geom_text(data = ann.df, aes(x=x, y=y, label = id, color = y), hjust = 0, nudge_x = 0.05)
ggplotly(b, tooltip = c("label", "label1", "label2"))
When I try to use ggplotly(), I lose the legend that explains the color scale. If I use color instead of size the legend appears. How do I get the legend to appear when using size?
library(dplyr)
library(plotly)
plot <- mtcars %>%
ggplot(aes(x = mpg, y = cyl, size = disp, frame = hp)) +
geom_point()
ggplotly(plot) %>%
layout(showlegend = T)
I think the issue is that using as size a numeric variable such as disp will create a very large legend e.g.:
p <- ggplot(data=mtcars, aes(x = mpg, y = cyl, colour=factor(disp), size = factor(disp))) +
geom_point()
p <- ggplotly(p)
p
PS: I'm not sure you can use frame and still have the desired legend using ggplotly.
Option 2: Use directly plot_ly e.g.:
plot_ly(mtcars,x = ~ mpg, y= ~ cyl, size = ~disp , type='scatter', mode = 'markers', frame = ~disp) %>%
layout(showlegend = T)
I am trying to remove the legend title using ggplotly without success. I'm sure there is an easy fix, but I cannot find the documentation for it - and removing the legend title (or changing the positioning) using ggplot fails to work. See e.g.:
# Creating the ggplot:
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T),
y = mpg,fill = interaction(cyl, carb, lex.order = T))) +
geom_boxplot() + theme(legend.title=element_blank())
a # No Legend Title
# plotly puts back the legend title
ggplotly(a)
Any ideas how to change / remove the title of the graph? Should it be done using ggplotly or ggplot?
You can use the labs function:
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T),
y = mpg,fill = interaction(cyl, carb, lex.order = T))) +
geom_boxplot()
# update the legend
a <- a + labs(fill= "New Legend")
a
# to remove the label and update the axis texts use:
a <- a+labs(fill= "",x="Cyc/Carb",y="Miles/(US) gallon")
ggplotly(a)