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
Related
I'm converting a ggplot2 plot to plotly.
The plot consists of a tile layer (in the background) and a point layer (in the foreground).
I would like to have tooltips when hovering on the tiles.
The code below mostly gets me what I am looking for. When I hover on tiles in "points free" zones, the desired tooltip appears. However, when I hover in areas with a high density of dots, the tooltips do not appear.
I thought that playing with the layerData parameter in the ggplotly call might help, but that was not the case.
library(ggplot2)
library(dplyr)
library(plotly)
set.seed(1)
dat_points <- data.frame(x = rnorm(100), y = rnorm(100))
dat_tiles <- expand.grid(tx = -3:3, ty = -3:3)
dat_tiles$val <- rnorm(nrow(dat_tiles))
dat_tiles$label <- sample(LETTERS[1:5], nrow(dat_tiles), replace = T)
p <- ggplot() +
geom_tile(data = dat_tiles, aes(x = tx, y = ty, fill = val, text = label)) +
geom_point(data = dat_points, aes(x = x, y = y), alpha = .5)
gg <- ggplotly(p, tooltip = "text")
gg
I would like that hovering on high density areas (e.g. 0, 0) would bring up tooltips with the same promptness as in low density areas.
EDIT: added static image of the plot.
You can switch the order of you layers in p and because of how ggplotly() constructs from a ggplot object, you get an identical looking plot, but with the desired tooltip behavior!
p <- ggplot() +
geom_point(data = dat_points, aes(x = x, y = y), alpha = 1) +
geom_tile(data = dat_tiles, aes(x = tx, y = ty, fill = val, text = label))
p # this looks bad
gg <- ggplotly(p, tooltip = "text")
gg # but this looks good!
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"))
I've been trying to recreate some heatmaps from this, but my y ticks are quite far apart from the remainder of geom_tile.
Here is my code
library(tidyverse)
library(ggthemes)
library(viridis)
d <-c('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
days <- factor(d, levels = d,ordered=TRUE)
L = rnorm(7*24,0,20)
df <- data.frame(days,L) %>%arrange(days)
df['Time'] = c(0:23)
gg <- ggplot(data = df, aes(x = Time, y = days, fill = L))+
geom_tile(color = 'white',size = 0.1)+
scale_fill_viridis('')+
coord_equal()+
labs(x = NULL, y = NULL, title = 'Practice HeatMap')+
theme_tufte(base_family="Helvetica")+
theme(axis.ticks=element_blank())
print(gg)
and the resulting plot
Even using hjust wont move the labels closer. How can I achieve a cleaner look?
Is there a way to bring the labels forward with respect to plot panel in ggplot?
Actually I was trying to answer my question here. I have not got any satisfactory response to that one although I thought it would be possible in ggplot. This is an attempt to get a solution although a hacky one. But the labels are rendered below the plot panel here.
Following are my (example) data, attempted solution and the resulting plot.
library(ggplot2)
library(magrittr)
mydata = data.frame(expand.grid(Tag = c('A','B','C'),Year = 2010:2011,PNo = paste0("X-",1:4)),Value = round(runif(24,1,20)))
mydata$dist = ifelse(mydata$Tag == 'A',0,ifelse(mydata$Tag=='B',2,7))
mydata %>% ggplot(aes(x = dist,y = Value,fill = factor(Year))) +geom_bar(stat='summary',position = 'dodge',fun.y='mean',width = 1) +
facet_wrap(~PNo,ncol=2) +
theme(axis.text.x = element_blank(),axis.ticks.x = element_blank()) +
geom_label(data = mydata %>% filter(PNo %in% c('X-3','X-4')),aes(x = dist,y=0,label = Tag),size=6,inherit.aes=F,color = 'red')
You have to turn off clipping of the bottom panel elements:
p <- mydata %>% ggplot(aes(x = dist,y = Value,fill = factor(Year))) +geom_bar(stat='summary',position = 'dodge',fun.y='mean',width = 1) +
facet_wrap(~PNo,ncol=2) +
theme(axis.text.x = element_blank(),axis.ticks.x = element_blank()) +
geom_label(data = mydata %>% dplyr::filter(PNo %in% c('X-3','X-4')),aes(x = dist,y=0,label = Tag),size=6,inherit.aes=F,color = 'red')
library(grid)
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[grep("panel-2-\\d+", gt$layout$name)] <- "off"
grid.draw(gt)
See Point clipped on x-axis in ggplot
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)