Is there a way to insert a source annotation below the x-axis label in Plotly? I can get an annotation below the x-axis, but if I change the margins, the annotation gets cut off. For now, I am able to put an annotation between the x-axis and the x-axis label. Below is a MWE for the included chart.
df <- data.frame(x = 1:10, y = 1:10)
plot_ly(data = df, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
layout(title = 'Sample Chart',
margin = list(l = 50, r = 50, t = 60, b = 60),
annotations = list(text = 'Source: U.S. Census Bureau.',
font = list(size = 12),
showarrow = FALSE,
xref = 'paper', x = -0.03,
yref = 'paper', y = -0.2))
I would like to move the "Source: U.S. Census Bureau" below the x-axis title.
You need to add line breaks using html tags
plot_ly(data = df, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
+ layout(title = 'Sample Chart',xaxis=list(
+ title = 'x <br> Source: U.S. Census Bureau.'),
+ margin = list(l = 50, r = 50, t = 60, b = 60))
Related
I wrote a code to make a subplots with scatterplots using my data. Here is a graph:
This is hours on x axis. As you see, not all of them appear on x axis. How could i make all 24 hours be on axis? Even if for example in dataframe there is no value for 23 o'clock, i want it to be on x axis. How to do that?
Here is my code:
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = ~ paste(metrics, " - ", time_pos),
colors = ~ time_pos,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "lines+markers",
marker = list(
size = 7,
color = "white",
line = list(width = 1.5)
),
width = 700,
height = 620
) %>% layout(autosize = T,legend = list(font = list(size = 8)))
}
subplot(subplotList, nrows = length(subplotList), margin = 0.05)
}
This could be achieved in layout via the attribute xaxis like so. The ticks or breaks can be set via tickvals, the tick labels via ticktext.
This is illustrasted using some random data in the reproducible example below:
library(plotly)
set.seed(42)
d <- data.frame(
x = sort(sample(24, 15)),
y = 1:15 + runif(15),
z = 1:15 + runif(15)
)
plot_ly(d) %>%
add_trace(x = ~x, y = ~y, type = "scatter", mode = "lines+markers") %>%
add_trace(x = ~x, y = ~z, type = "scatter", mode = "lines+markers") %>%
layout(xaxis = list(tickvals = 1:24, ticktext = paste0(1:24, "h")))
How can I reproduce the following graph in plotly
library(dplyr)
library(ggplot2)
tibble(x =1:10, y = 1:10) %>%
ggplot(aes(x,y)) +
geom_line() +
scale_y_continuous(sec.axis = ~.*2)
I tried the following code based on this answer here
library(dplyr)
library(plotly)
tibble(x =1:10, y = 1:10) %>%
mutate(y2 = y*2) %>%
plot_ly() %>%
add_lines(x =~x, y =~y) %>%
add_lines(x= ~x, y=~y2,
yaxis = "y2", color = I("transparent"),
hoverinfo='skip', showlegend=FALSE) %>%
layout(yaxis2 = list(side = "right", overlaying = "y", showgrid = FALSE),
margin = list(r = 50))
While at first glance it appears to work, it only provides a partial solution, since if I interactively try to change the scale of the main (left) y axis on the produced graph (by dragging it up or down), the right axis does not move with the graph (because it is linked only to the second invisible graph). This of course is not acceptable as it does not allow using any of interactive features of plotly reliably which is the reason I wanted to use it to begin with instead of ggplot.
Edit: Just realized that this plotly solution does not seem to work at all in the case of non linear transformation between the axes (while ggplot handles it beautifully).
You just need to set up dtick and tick0 for plotly to have the same graph as ggplot2 one. See below;
library(plotly)
library(dplyr)
tibble(x =1:10, y = 1:10) %>%
mutate(y2 = y*2) -> df1
n0 <- 4
y0 <- max(df1$y)/n0
x0 <- max(df1$x)/n0
df1 %>%
plot_ly(data = . , x = ~x, y = ~y,
type = "scatter", mode = "lines", width = 800,
color = I("red"), name = "") %>%
add_trace(x = ~x, y = ~y2, yaxis = "y2",
color = I("red"), name = "") %>%
layout(yaxis = list(showline = FALSE, side = "left",
title = "", color = "red",
dtick = y0, tick0 = y0, fixedrange=TRUE),
yaxis2 = list(showline = FALSE, side = "right", overlaying = "y", position = 1,
title = "", anchor = "free", color = "blue",
dtick = 2*y0, tick0 = 2*y0, fixedrange=TRUE),
xaxis = list(showline = FALSE, zeroline = FALSE, title = "",
dtick = x0, tick0 = x0),
showlegend = FALSE,
margin = list(l = 50, r = 50, b = 50, t = 50, pad = 4)
)
Created on 2020-06-19 by the reprex package (v0.3.0)
I am trying to remove the error bar values in the tooltip in an R Plotly graph.
I have tried playing around with the hovertext arguments from here: https://plot.ly/r/hover-text-and-formatting/ but can't get it to work.
I have these in a function where sometimes there are error bars, but most of the time there aren't (because I don't have the data) so the extra detail in the tooltip is not needed (as it just shows +0/-0).
With the example below, I would want it to just show 2010, 5 without the confidence intervals.
Any ideas?
library(tidyverse)
library(plotly)
data <- tibble(x = c(2010, 2011, 2012),
y = c(5, 6, 7),
err_high = c(1, 1, 1),
err_low = c(0.9, 1, 1.1))
#plotly graph
plot_ly() %>%
add_trace(data = data, x = ~x, y = ~y,
name = 'Actual', type = 'scatter', mode = 'lines+markers',
line = list(shape = 'linear', width= 4, dash = 'solid'),
error_y = list(type = "data", symmetric = FALSE, array = ~err_high, arrayminus = ~err_low)) %>%
layout(xaxis = list(title = 'Year'),
yaxis = list (title = 'Value', rangemode = "tozero"))
You can include
text=paste(data$x, data$y, sep=', '),
hoverinfo='text',
in add_trace() to get this:
Plot:
Complete code:
library(tidyverse)
library(plotly)
data <- tibble(x = c(2010, 2011, 2012),
y = c(5, 6, 7),
err_high = c(1, 1, 1),
err_low = c(0.9, 1, 1.1))
#plotly graph
plot_ly() %>%
add_trace(data = data, x = ~x, y = ~y,
name = 'Actual', type = 'scatter', mode = 'lines+markers',
line = list(shape = 'linear', width= 4, dash = 'solid'),
text=paste(data$x, data$y, sep=', '),
hoverinfo='text',
error_y = list(type = "data", symmetric = FALSE, array = ~err_high, arrayminus = ~err_low)) %>%
layout(xaxis = list(title = 'Year'),
yaxis = list (title = 'Value', rangemode = "tozero"))
I would like to know if it is possible to move the row labels on the right side of the heatmap, rather than on the left side or this cannot be moved in plotly.
data<-as.matrix(mtcars)
data[upper.tri(data)] <- NA
library(plotly)
plot_ly(x=colnames(data), y=rownames(data), z = data,colors = colorRamp(c("red","green")), type = "heatmap") %>%
layout(
xaxis=list(tickfont = list(size = 30), tickangle = 45),
margin = list(l = 150, r = 50, b = 150, t = 0, pad = 4))
For that we may use yaxis = list(side = "right") and adjust the colorbar position with colorbar = list(x = -0.4) (you may need to play a little with this value for your specific graph):
plot_ly(x = colnames(data), y = rownames(data), z = data,
colors = colorRamp(c("red","green")),
type = "heatmap", colorbar = list(x = -0.4)) %>%
layout(xaxis = list(tickfont = list(size = 30), tickangle = 45),
yaxis = list(side = "right"),
margin = list(l = 150, r = 50, b = 150, t = 0, pad = 4))
I made a boxplot:
dat %>%
plot_ly(y = ~xval, color = ~get(col), type = "box",
boxpoints = "all", jitter = 0.7,
pointpos = 0, marker = list(size = 3),
source = shiny_source, key = shiny_key,
hoverinfo = 'text', text = txt)
but problem is that jittered points are not interactive and cannot be marked separately, so I came with an idea to add those points using add_markers:
dat %>%
plot_ly(y = ~xval, color = ~get(col), type = "box",
boxpoints = FALSE, jitter = 0.7,
pointpos = 0, marker = list(size = 3),
source = shiny_source, key = shiny_key,
hoverinfo = 'col', text = txt
) %>%
add_markers(x = ~get(col), y = ~varval, size = I(6))
but now points are in straight line and I'd like to add some jitter (for example by using beeswarm package). But I don't know how to get coordinates of qualitative variable IC0 on X axis. Any ideas?
I find myself in the same potential case often with plotly and ggplot2-- 3 lines of code to get 90% of what I want, and 30 lines of code to get the aesthetics just right.
One potential solution/workaround: Take advantage of R's "factors are coded with integers" paradigm, plot everything on a numeric scale, and then cover your tracks by hiding x labels and x hover values.
dat <- data.frame(xval = sample(100,1000,replace = TRUE),
group = as.factor(sample(c("a","b","c"),1000,replace = TRUE)))
dat %>%
plot_ly() %>%
add_trace(x = ~as.numeric(group),y = ~xval, color = ~group, type = "box",
hoverinfo = 'name+y') %>%
add_markers(x = ~jitter(as.numeric(group)), y = ~xval, color = ~group,
marker = list(size = 6),
hoverinfo = "text",
text = ~paste0("Group: ",group,
"<br>xval: ",xval),
showlegend = FALSE) %>%
layout(legend = list(orientation = "h",
x =0.5, xanchor = "center",
y = 1, yanchor = "bottom"
),
xaxis = list(title = "Group",
showticklabels = FALSE))
Yields the following