I have a basic scatterplot that I've made in plotly (in R). I'm using a continuous input to color the data points which plotly converts into a gradient. However, the removelegend option doesn't remove a continuous legend the way it removes a discrete legend. Consider the example below.
data = mtcars
data$vs = as.character(data$vs)
plotly::plot_ly(
data = data,
x = ~disp,
y = ~mpg,
color = ~vs,
mode = "markers",
type = "scatter"
) %>%
layout(showlegend = FALSE)
plotly::plot_ly(
data = data,
x = ~disp,
y = ~mpg,
color = ~hp,
mode = "markers",
type = "scatter"
) %>%
layout(showlegend = FALSE)
Is there a way to remove the continuous legend?
The issue arises because in the continuous case plotly doesn't call it a legend, it's a color bar. The easiest way to remove it is to pipe in hide_colorbar()
plotly::plot_ly(
data = data,
x = ~disp,
y = ~mpg,
color = ~hp,
mode = "markers",
type = "scatter"
) %>%
hide_colorbar()
Related
I am trying to log transform values that are defining color on a Plotly graph, but I would like to keep original values on the Plotly color bar legend (not log-transformed numbers) in order to improve readability.
Here is the example of what I am trying to do on the mtcars data-set:
mtcars %>% plot_ly(x = ~hp,
y = ~qsec,
size = ~disp,
color = ~mpg)
and you will get this graph:
Let's say I want to log transform color variable (mpg) with this code:
mtcars %>% plot_ly(x = ~hp,
y = ~qsec,
size = ~disp,
color = ~log(mpg))
I will get this graph:
I am satisfied now with the graph, but now the colorbar on the right is having log() numbers.
My question is: how to log() transform color variable on a graph but keep the original numbers on the colorbar that are appropriately adjusted to new log colors?
So, on the one hand, I would like to have the original numbers on the second picture color bar, instead of 2.5, 3 and 3.5, but on the other hand, I would like to keep the color-positions of these numbers as they are on the log scale and without using ggplotly.
Similarly to this answer, this is a matter of using transformed values for the colour bar ticks, but untransformed values as labels.
Here is an option:
library(dplyr)
library(plotly)
# Define pretty breaks on transformed scale
brks_transformed <- pretty(log10(mtcars$mpg), n = 5)
# Breaks on the untransformed scale
brks_untransformed <- sprintf("%.1f", 10^brks_transformed)
mtcars %>%
plot_ly(
x = ~hp, y = ~qsec, size = ~ disp, fill = ~ "",
type = "scatter",
mode = "markers",
marker = list(
color = ~ log10(mpg),
line = list(width = 0),
colorbar = list(
tickmode = "array",
ticktext = brks_untransformed,
tickvals = brks_transformed)))
I want to create a filled area plot with line and scatters like in the screenshot attached but I do not know how could add scatters for every year of x-axis and also annotate its value. My code is:
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)
fig <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'line', stackgroup = 'one', fillcolor = '#F5FF8D')
fig
The mode lines+markers+text allows you to define a line plot with markers and add some text.
I changed the type of year from factor to numeric, because I had to expand the xaxis for readabilty of the annotations.
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year" = as.numeric(rownames(data)), data)
plot_ly(data,
x = ~year,
y = ~Food.and.Tobacco,
text = ~Food.and.Tobacco) %>%
add_trace(
type = 'scatter',
mode = 'lines+markers+text',
fill = 'tozeroy',
fillcolor = '#F5FF8D',
marker = list(color = 'black'),
line = list(color = 'black'),
textposition = "top center",
hovertemplate = paste0("<b>%{x}</b>
Cummulative Food and Tobacco: %{y}
<extra></extra>"),
hoveron = 'points') %>%
layout(xaxis = list(
range= list(min(data$year) - 1, max(data$year) + 1)))
I am trying to get another plot to react to both legend (select/deselect levels of a factor) and drag select. In the following toy example drag select works as intended: the violin and boxplot are re-rendered to depict the points that are selected using the mouse. The legend works for the scatterplot, but not for the boxplot and violin that don't seem to share the legend. I read many posts about shared legend, but nothing worked for subplots of different types.
Even more surprisingly, when deplying to plotly cloud, the plot also loses it's drag select feature (altough I don't need the plotly cloud, I thought I should mention this).
Just to be clear, I'd like the boxplot and violin to be re-rendered based both on selecting on legend and drag-select on scatterplot. As these features theoretically work, the legend should provide the full data or subsets based on levels of the factor used for legend from which the drag selection could be carried out afterwards.
library(plotly)
d <- mtcars
d$cyl <- as.factor(d$cyl)
d <- highlight_key(mtcars)
sp <- plot_ly(d, x = ~mpg, y = ~disp,
color = ~factor(cyl), colors = c("red", "green", "blue"),
legendgroup = ~factor(cyl), showlegend = T) %>%
add_markers()
box <-
plot_ly(d, y = ~disp,
color = I("black"),
legendgroup = ~factor(cyl), showlegend = F) %>%
add_boxplot(name = " ")
violin <-
plot_ly(d, y = ~disp,
color = I("black"),
legendgroup = ~factor(cyl), showlegend = F) %>%
add_trace(type = "violin", name = " ")
p <-
subplot(sp, box, violin, shareY = TRUE, titleX = TRUE, titleY = TRUE) %>%
layout(
dragmode = "select",
barmode = "overlay",
title = "Click and drag scatterplot",
showlegend = TRUE
) %>%
highlight(on = "plotly_selected", off = "plotly_deselect")
p
Any help is greatly appreciated.
I created a scatter plot with plotly in R. Now I want to plot a boxplot with different data next to the scatter plot. I want to use plotly for this.
The result should look like this. Can someone help me please, I have no idea how to do that.
My code so far is
plot_ly(ds, x = ~x, y = ~y , mode = "markers", name = "Clusters", opacity = point.opacity,
text = ds$id,
hoverinfo = "text",
marker = list(symbol = point.symbol, color = ~color, size = point.size,
line = list(color = "#262626", width = point.linewidth, opacity = point.lineopacity)),
showlegend = F)
Here is an example on how to make a scatter with marginal box plots with plotly:
library(plotly)
data(iris)
create, three plots for the data: one for the scatter, two for the appropriate box plots, and one additional empty plot. Use the subplot function to arrange them:
subplot(
plot_ly(data = iris, x = ~Petal.Length, type = 'box'),
plotly_empty(),
plot_ly(data = iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter',
mode = 'markers'),
plot_ly(data = iris, y = ~Petal.Width, type = 'box'),
nrows = 2, heights = c(.2, .8), widths = c(.8,.2), margin = 0,
shareX = TRUE, shareY = TRUE) %>%
layout(showlegend = F)
I would like to reproduce this ggplot graph by using native plotly functionality
ggplot(mtcars, aes(x="mpg", y=mpg))+
geom_boxplot()+geom_jitter(aes(color = mpg),width = 0.3,size=4)+
scale_color_gradient(low="blue", high="yellow", guide = 'none')
ggplotly()
I am trying to do something like this
plot_ly(data=mtcars, y = ~mpg,
type = "box", boxpoints = "all", jitter = 0.3,
marker=list(color=~mpg, size=10),pointpos = 0)
But I can't find the way to control the color of the points (color=~mpg does not do anything)
The reason that I need it to be implemented using native plotly rather then with the "plotlyfied" ggplot as mentioned above is that I need style consistency with other plots that were implemented in native plotly.
This is a little bit of a hack, but it works:
use a separate trace to draw the boxplot (without points)
add a 2nd trace of type scatter to draw the points
unfortunately, scatter traces don't accept jitter as an argument
this means we need to use a continuous scale on the x-axis and add jitter using rnorm
we don't really want the x axis to look like a continuous numeric scale, so we set the tick labels manually to look like categories
Like this:
plot_ly(data=mtcars) %>%
add_trace(y = ~mpg, x=1, type = "box", boxpoints = "none") %>%
add_trace(type="scatter", mode="markers",
y=~mpg, x=rnorm(nrow(mtcars),1,0.05), marker=list(color = ~mpg, size=10))%>%
layout(xaxis = list(tickmode="array", tickvals=c(1), ticktext=c("mtcars") ))
You can extend this approach to have more than one category like this
plot_ly(data=mtcars) %>%
add_trace(y = ~mpg, x=~gear, type = "box", boxpoints = "none") %>%
add_trace(type="scatter", mode="markers", y=~mpg,
x = ~gear+rnorm(nrow(mtcars),0,0.1),
marker=list(color = ~mpg, size=10))%>%
layout(xaxis = list(tickmode="array", tickvals=c(3,4,5), ticktext=c("3","4","5") ))
To control the marker colors, you can use the colorscale argument, together with autocolorscale = FALSE
The colorscale must be an array containing arrays mapping a normalized
value to an rgb, rgba, hex, hsl, hsv, or named color string. At
minimum, a mapping for the lowest (0) and highest (1) values are
required.
For example,
cs = list(list(0, rgb(0,0,1)), list(1, rgb(1,1,0)))
plot_ly(data=mtcars) %>%
add_trace(y = ~mpg, x=1, type = "box", boxpoints = "none") %>%
add_trace(type="scatter", mode="markers",
y=~mpg, x=rnorm(nrow(mtcars),1,0.05),
marker=list(color = ~mpg, autocolorscale=F,
colorscale = cs, size = 10)) %>%
layout(xaxis = list(tickmode="array", tickvals=c(1), ticktext=c("mtcars") ))