The hover name that appears outside the hover box on my plotly plot is getting cutoff because of the long names. I have found solutions for plotly.js and plotly.py but not R. I have been able to remove that name from the hover or move it into the box with the value, but need a solution that keeps it in place while allowing the entire name to be visible.
# sample data
Value <- rnorm(100, mean = 5)
Time <- c(1:100)
Site <- "Really long name for Site 1"
datTemp <- data.frame(Time, Value, Site)
Value <- rnorm(100, mean = 5)
Time <- c(1:100)
Site <- "Really long name for Site 2"
datTemp2 <- data.frame(Time, Value, Site)
Value <- rnorm(100, mean = 5)
Time <- c(1:100)
Site <- "Really long name for Site 3"
datTemp3 <- data.frame(Time, Value, Site)
figTemp<-datTemp%>%
plot_ly(x = ~Time, y = ~Value, type = "scatter", mode = "lines", line = list(width = 0.75), name = ~Site[1])%>%
layout(xaxis = list(title = "Date"), yaxis = list(title = "Temperature (C)"), legend = list(x = 0, y = 1.05, orientation = "h"))
figTemp<-figTemp%>%
add_trace(data = datTemp2, y = ~Value, type = "scatter", mode = "lines", name = ~Site[1])%>%
add_trace(data = datTemp3, y = ~Value, type = "scatter", mode = "lines", name = ~Site[1])
Add hovertemplate = paste('(%{x}, %{y})') to your call to plot_ly. Worked. Try this:
library(tidyverse)
library(plotly)
# sample data
Value <- rnorm(100, mean = 5)
Time <- c(1:100)
Site <- "Really long name for Site 1"
datTemp <- data.frame(Time, Value, Site)
Value <- rnorm(100, mean = 5)
Time <- c(1:100)
Site <- "Really long name for Site 2"
datTemp2 <- data.frame(Time, Value, Site)
Value <- rnorm(100, mean = 5)
Time <- c(1:100)
Site <- "Really long name for Site 3"
datTemp3 <- data.frame(Time, Value, Site)
figTemp<-datTemp%>%
plot_ly(x = ~Time, y = ~Value, type = "scatter", mode = "lines", line = list(width = 0.75), name = ~Site[1],
hovertemplate = paste('(%{x}, %{y})')) %>%
layout(xaxis = list(title = "Date"), yaxis = list(title = "Temperature (C)"), legend = list(x = 0, y = 1.05, orientation = "h"))
figTemp<-figTemp%>%
add_trace(data = datTemp2, y = ~Value, type = "scatter", mode = "lines", name = ~Site[1]) %>%
add_trace(data = datTemp3, y = ~Value, type = "scatter", mode = "lines", name = ~Site[1])
figTemp
Created on 2020-03-18 by the reprex package (v0.3.0)
Related
I am trying to add a second category to x axis with Plotly under R like this:
Here is my code:
library(plotly)
data <- data.frame(day= c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 1))
fig <- plot_ly(data= data, x = ~day) %>%
add_trace(y = ~val,
type = 'scatter',
mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(data= data %>% filter(flag == 1), y = 0,
type = 'scatter',
hoverinfo = "text",
mode = 'markers',
name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red',
symbol = "x",
size = 12
),
showlegend = T
)
fig
I have tried this, which seems good but the markers disappear from the graph, maybe due to the filter on data.
library(plotly)
data <- data.frame(day= c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 1))
fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
add_trace(y = ~val,
type = 'scatter',
mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(data= data %>% filter(flag == 1), y = 0,
type = 'scatter',
hoverinfo = "text",
mode = 'markers',
name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red',
symbol = "x",
size = 12
),
showlegend = T
)
fig
You didn't provide a reproducible question, so I've made data. (Well, data I shamelessly stole from here). This creates a bar graph with two sets of x-axis labels. One for each set of bars. One for each group of bars. The content of the x-axis is the same in both traces.
library(plotly)
fig <- plot_ly() %>%
add_bars(x = list(rep(c("first", "second"), each = 2),
rep(LETTERS[1:2], 2)),
y = c(2, 5, 2, 6),
name = "Adults") %>%
add_bars(x = list(rep(c("first", "second"), each = 2),
rep(LETTERS[1:2], 2)),
y = c(1, 4, 3, 6),
name = "Children")
fig
Update
You added data and code trying to apply this to your data. I added an update and apparently missed what the problem was. Sorry about that.
Now that I'm paying attention (let's hope, right?), here is an actual fix to the actual problem.
For this change, I modified your data. Instead of the flag annotated with a one, I changed it to zero. Then I used flag as a variable.
data <- data.frame(day = c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 0))
fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
add_trace(y = ~val,
type = 'scatter', mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(y = ~flag,
type = 'scatter', hoverinfo = "text",
mode = 'markers', name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red', symbol = "x", size = 12),
showlegend = T)
fig
You're going to get a warning about discrete & non-discrete data, which isn't really accurate, but it shows up, nonetheless.
parameterslist <- cbind(v =c(40, 50, 60))
for (i in 1:nrow(parameterslist))
outlist[[i]] <- ode(func = model, y = yini, times = times, parms = c(parameterslist[i,], parameters))
outlist <- as.data.frame(outlist)
plot_vdisp_3 <- plot_ly(outlist, x = ~times, y = ~power1, name = 'v=30m/s', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~outlist$y1, name = 'v=40m/s', mode = 'lines+markers') %>%
add_trace(y = ~outlist$y1.1, name = 'v=50m/s', mode = 'lines+markers') %>%
add_trace(y = ~outlist$y1.2, name = 'v=60m/s', mode = 'lines+markers') %>%
layout( title = 'Drag force according to different wind speeds',
xaxis = list(title = 'Time (hours)'),
yaxis = list(title = 'Drag Force'))
plot_vdisp_3
I solved a differential equation and created a dataframe but could not plot properly. Could you please explain what should I change? what does outlist$y1 and outlist$y1.1 mean? I created power1 data frame before. but is it ok? during plotting sometimes I could not see the orange line what is the reason for this?
Thanks.
Here is the part of data frame creation part.
times <- seq(from = 0, to = 25, by = 1)
out <- ode(func = model, y = yini, times = times, parms = parameters, method = "rk4")
model_out <- data.frame(times, out)
power1 <- model_out$y1
force1 <- model_out$y2
temp1 <- model_out$y3
I would like to localize a plotly graph date axis date format for a special country. Instead of labeling dates in the time axis as "Jan 8 Jan 9 Jan 10...", I want to label them "Oca 8 Oca 9 Oca 10" in Turkish locale. The sample plotly code in R is given below. I know that I need to use config(locale = "tr_TR") to do this. But I am not sure how to do this in the code below.
plotly::plot_ly() %>%
plotly::add_trace(
x = ~date,
y = ~Belgium,
type = "scatter",
mode = "lines+markers",
name = "Türkiye"
) %>%
plotly::add_trace(
x = ~date,
y = ~France,
type = "scatter",
mode = "lines+markers",
name = "Fransa"
) %>%
plotly::add_trace(
x = ~date,
y = ~Spain,
type = "scatter",
mode = "lines+markers",
name = "İspanya"
) %>%
plotly::add_trace(
x = ~date,
y = ~Italy,
type = "scatter",
mode = "lines+markers",
name = "Italya"
) %>%
plotly::layout(
title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Toplam Onaylı Vakalar"),
xaxis = list(title = "Date"),
# paper_bgcolor = "black",
# plot_bgcolor = "black",
# font = list(color = 'white'),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
)
)```
I just added the following
)%>%
plotly::config(
locale='tr')
)
This configuration sets the date axis labeling based on system locale.
I want to know how to mirror 2 kinds of bars(positive number and negative number) in vertical like the picture below:
So far I can only make them mirror but standing side by side like:
Data:
library(plotly)
time <- c("2018-10","2018-11","2018-12")
add <- c(20,15,20)
delete <- c(-5,-10,-2)
total <- c(60,65,83)
df <- data.frame(time,add,delete,total)
plot_ly(df) %>%
add_trace(x = ~time, y = ~add, type = 'bar', name = 'add',
marker = list(color = '#33adff'),
hovertemplate = paste('add: %{y}',
'<br>time: %{x}<br>')) %>%
add_trace(x = ~time, y = ~delete, type = 'bar', name = 'delete',
marker = list(color = '#ff6666'),
hovertemplate = paste('delete: %{y}',
'<br>time: %{x}<br>'))
The problem can be solved using barmode="overlay".
You need to set the df dataset in long data format.
library(plotly)
time <- c("2018-10","2018-11","2018-12")
add <- c(20,15,20)
delete <- c(-5,-10,-2)
total <- c(60,65,83)
n <- length(time)
df <- data.frame(time=rep(time,2), y=c(add, delete), grp=rep(c("Add","Delete"),each=n))
plot_ly(df) %>%
add_trace(x = ~time, y = ~y, color=~grp, text=~grp, type = 'bar',
marker = list(color = c(rep('#33adff',n), rep('#ff6666',n))),
hovertemplate = paste('%{text}: %{y}', '<br>time: %{x}<br>')) %>%
layout(barmode="overlay")
I am trying to generate multiple graphs in Plotly for 30 different sales offices. Each graph would have 3 lines: sales, COGS, and inventory. I would like to keep this on one graph with 30 buttons for the different offices. This is the closest solution I could find on SO:
## Create random data. cols holds the parameter that should be switched
l <- lapply(1:100, function(i) rnorm(100))
df <- as.data.frame(l)
cols <- paste0(letters, 1:100)
colnames(df) <- cols
df[["c"]] <- 1:100
## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly(df,
type = "scatter",
mode = "lines",
x = ~c,
y= ~df[[cols[[1]]]],
name = cols[[1]])
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[-1]) {
p <- p %>% add_lines(x = ~c, y = df[[col]], name = col, visible = FALSE)
}
p <- p %>%
layout(
title = "Dropdown line plot",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(cols, function(col) {
list(method="restyle",
args = list("visible", cols == col),
label = col)
})
)
)
)
print(p)
It works but only on graphs with single lines/traces. How can I modify this code to do the same thing but with graphs with 2 or more traces? or is there a better solution? Any help would be appreciated!
### EXAMPLE 2
#create fake time series data
library(plotly)
set.seed(1)
df <- data.frame(replicate(31,sample(200:500,24,rep=TRUE)))
cols <- paste0(letters, 1:31)
colnames(df) <- cols
#create time series
timeseries <- ts(df[[1]], start = c(2018,1), end = c(2019,12), frequency = 12)
fit <- auto.arima(timeseries, d=1, D=1, stepwise =FALSE, approximation = FALSE)
fore <- forecast(fit, h = 12, level = c(80, 95))
## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly() %>%
add_lines(x = time(timeseries), y = timeseries,
color = I("black"), name = "observed") %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
color = I("gray95"), name = "95% confidence") %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
color = I("gray80"), name = "80% confidence") %>%
add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction")
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[2:31]) {
timeseries <- ts(df[[col]], start = c(2018,1), end = c(2019,12), frequency = 12)
fit <- auto.arima(timeseries, d=1, D=1, stepwise =FALSE, approximation = FALSE)
fore <- forecast(fit, h = 12, level = c(80, 95))
p <- p %>%
add_lines(x = time(timeseries), y = timeseries,
color = I("black"), name = "observed", visible = FALSE) %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
color = I("gray95"), name = "95% confidence", visible = FALSE) %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
color = I("gray80"), name = "80% confidence", visible = FALSE) %>%
add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction", visible = FALSE)
}
p <- p %>%
layout(
title = "Dropdown line plot",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(cols, function(col) {
list(method="restyle",
args = list("visible", cols == col),
label = col)
})
)
)
)
p
You were very close!
If for example you want graphs with 3 traces,
You only need to tweak two things:
Set visible the three first traces,
Modify buttons to show traces in groups of three.
My code:
## Create random data. cols holds the parameter that should be switched
library(plotly)
l <- lapply(1:99, function(i) rnorm(100))
df <- as.data.frame(l)
cols <- paste0(letters, 1:99)
colnames(df) <- cols
df[["c"]] <- 1:100
## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly(df,
type = "scatter",
mode = "lines",
x = ~c,
y= ~df[[cols[[1]]]],
name = cols[[1]])
p <- p %>% add_lines(x = ~c, y = df[[2]], name = cols[[2]], visible = T)
p <- p %>% add_lines(x = ~c, y = df[[3]], name = cols[[3]], visible = T)
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[4:99]) {
print(col)
p <- p %>% add_lines(x = ~c, y = df[[col]], name = col, visible = F)
}
p <- p %>%
layout(
title = "Dropdown line plot",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(0:32, function(col) {
list(method="restyle",
args = list("visible", cols == c(cols[col*3+1],cols[col*3+2],cols[col*3+3])),
label = paste0(cols[col*3+1], " ",cols[col*3+2], " ",cols[col*3+3] ))
})
)
)
)
print(p)
PD: I only use 99 cols because I want 33 groups of 3 graphs