Related
I created a graph with two traces in plotly and I use the hovermode x unified. At some point there is no data for line 2, but there is for line 1. In the hoover I only want to see the information about line 1 then, but it also shows information about the nearest point of line 2. When I zoom in the issue dissapears.
So in short, I want to disable the closest function of the hovermode. How can I do this?
Some sample data + code:
library(plotly)
# dummy data
df_data1 = data.frame(date_input = seq(as.Date('2020/01/01'), as.Date('2022/07/01'), by="week")
, value=1:131)
df_data2 = data.frame(date_input = seq(as.Date('2020/01/01'), as.Date('2022/06/01'), by="week")
, value2=3:129)
plot1 <- plot_ly()
plot1 <- plot1 %>%
add_trace(data = df_data1, x = ~date_input, y = ~value
, type = 'scatter', mode = "lines", yaxis = "y", line = list(color = '#E64B35FF', opacity = 0.8)
, showlegend = FALSE
, hovertemplate = ~paste('# Value1: %{y:.0f}<extra></extra>')) %>%
add_trace(data = df_data2, x = ~date_input, y = ~value2
, type = 'scatter', mode = "lines", yaxis = "y2", line = list(color = '#4DBBD5FF', opacity = 0.8)
, showlegend = FALSE
, hovertemplate = ~paste('# Value2: %{y:.0f}<extra></extra>'))
plot1 %>%
layout(
font = list(size = 10),
xaxis = list(title = list(text = '<b>Date<b>', font = list(size = 12))
, fixedrange = T, showgrid = FALSE, ticks = 'inside', type = 'date'
),
yaxis = list(title = list(text = '<b>Number of value1<b>', font = list(size = 12, color = '#E64B35FF'))
, fixedrange = T
, rangemode = 'tozero', showgrid = FALSE, showline = T, ticks = 'inside'),
yaxis2 = list(overlaying = 'y', side = 'right', fixedrange = T, rangemode = 'tozero', showgrid = FALSE, showline = T, ticks = 'inside'
, title = list(text = '<b>Number of value2<b>', font = list(size = 12, color = '#4DBBD5FF'))),
hovermode = "x unified"
)
As you can see the hovermode shows the value2 information still at 15 june (but it shows the closest data from 1 june.
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.
Is there a way to round decimals in plotly? For example,
I am getting the values as 24.32544 M.
Instead of this, can I get as 24.32 M?
asd <- data.frame(a = c(1,2,3,4), b = c(24325443,35345442,3245353453,345353523), c = c(5435352345,234534534,324534534,23453532))
plot_ly(asd, x = ~a, y = ~b, name = 'b', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~c, name = 'c', mode = 'lines') %>%
layout(xaxis = list(title = paste0("Week"),showgrid = F,rangemode = "tozero"),
yaxis = list(title = "",showgrid = F,rangemode = "tozero"),
legend = legend_cus,
hovermode = 'x unified')
p <- ggplot(asd, aes(a, b,
text = paste0("a: ", round(a), "</br></br>b: ", round(b))))
+ geom_point()
ggplotly(p, tooltip = "text")
To conver to million format see: Format numbers with million (M) and billion (B) suffixes
I am trying to plot a chart where the user can select which column data they want to view in the plot. So if you see in the data below I want to plot a chart where the user can see the numbers by date for differnt fruits . So x axis should be the date, y axis should me either market1 , market2 or market3 which ever the user select and a area chart where the areas as split by fruit type.
test_data <-data.frame(
stringsAsFactors = FALSE,
market1 = c(0L,0L,1L,0L,0L,1L,1L,0L,
2L,2L,0L,0L,1L,0L,8L,8L,0L,4L,3L,0L,4L,2L,
0L,1L,2L,0L,3L,2L,0L,5L,0L,7L,1L,8L,4L,0L,
16L,22L,0L,35L,17L,57L,19L,0L,125L,15L,0L,49L,
18L,0L),
market2 = c(34L,5L,71L,3L,3L,30L,54L,
0L,15L,28L,0L,24L,53L,0L,169L,46L,0L,134L,40L,
0L,123L,18L,2L,54L,72L,0L,131L,44L,0L,111L,
35L,194L,19L,182L,65L,0L,462L,313L,0L,524L,300L,
1366L,216L,0L,2165L,297L,0L,1188L,196L,0L),
market3 = c(1762L,1139L,2562L,294L,235L,
890L,1619L,1L,453L,1434L,6L,872L,1933L,12L,
3951L,1563L,20L,2934L,2358L,126L,5182L,2264L,208L,
3897L,2735L,3L,3156L,2727L,8L,3667L,1273L,4819L,
679L,5470L,1829L,7L,9847L,7309L,30L,9235L,7933L,
30486L,4551L,2L,35029L,7018L,3L,25591L,3516L,1L),
fruit = c("Apple",
"Apple","Apple",
"Apple","Apple","Apple",
"Apple","Apple",
"Apple","Apple","Apple",
"Apple","Banana",
"Banana","Banana","Banana",
"Banana","Banana",
"Banana","Banana","Banana",
"Banana","Banana",
"Banana","Banana","Banana",
"Orange","Orange",
"Orange","Orange","Orange",
"Orange","Orange",
"Orange","Orange","Orange",
"Orange","Orange",
"Orange","Berries","Berries",
"Berries","Berries",
"Berries","Berries","Berries",
"Berries","Berries",
"Berries","Berries"),
date = as.factor(c("1/10/20",
"1/10/20","1/10/20","1/10/20","1/10/20",
"1/10/20","1/11/20","1/11/20","1/11/20",
"1/11/20","1/11/20","1/11/20","1/12/20","1/12/20",
"1/12/20","1/12/20","1/12/20","1/12/20",
"1/13/20","1/13/20","1/13/20","1/13/20",
"1/13/20","1/13/20","1/14/20","1/14/20",
"1/14/20","1/14/20","1/14/20","1/14/20","1/15/20",
"1/15/20","1/15/20","1/15/20","1/16/20",
"1/16/20","1/16/20","1/16/20","1/16/20",
"1/16/20","1/17/20","1/17/20","1/17/20",
"1/17/20","1/17/20","1/18/20","1/18/20","1/18/20",
"1/18/20","1/18/20"))
)
I tried doing it for just one market but I am unable to split it by fruit. How can I go about it.Below is the code for the plot.
plot_ly(test_data, x = test_data$date, y = test_data$market1, name = 'Tastemade', type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = '#F5FF8D')
layout(title = 'Conversions By Day',
# xaxis = list(title = 'Date',rangeslider = list(type = "date")),
xaxis = list(title = 'Date'),
yaxis = list(title = 'Conversions'))
I don't think you can create a plot where the user will be allowed to change the y-axis variable, unless you create a shiny app.
Here is an example of a scatter plot for market1, where the markers are coloured by fruit:
plot_ly(data = test_data, x = ~date, y = ~market1, color = ~fruit, colors = "RdYlGn",
type = 'scatter', mode = 'markers') %>%
layout(title = 'Conversions By Day',
# xaxis = list(title = 'Date',rangeslider = list(type = "date")),
xaxis = list(title = 'Date'),
yaxis = list(title = 'Conversions'))
I am trying to visualize a line chart using ploy_ly() function. I need to display the label above the line chart. But the labels are displayed over the points and it looks messy.
The dataframe used is as follows:
Quarter average
1 2018 Q1 47.6
2 2018 Q2 46.4
3 2018 Q3 45.7
4 2018 Q4 45.5
5 2019 Q1 45.7
6 2019 Q2 46.3
7 2019 Q3 45.7
The code used for visualizing the line chart using plot_ly() is as follows:
plot_ly(teamAverageperweek, x = teamAverageperweek$Quarter,
text = teamAverageperweek$average,
hoverinfo = 'text',
hovertext = paste('Quarter of Date: ', teamAverageperweek$Quarter,
'<br> Avg Hours Per Week: ',teamAverageperweek$average),
showlegend = FALSE)%>%
add_trace(y = teamAverageperweek$average,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgb(242,142,43)',
width = 3)) %>%
add_trace(x = teamAverageperweek$Quarter,
y = teamAverageperweek$average,
type = 'scatter',
mode = 'markers',
marker = list(color = 'rgb(242,142,43)',
size = 8)) %>%
layout(
yaxis = list(
range = c(20,70),
title = "Avg Hours Per Week"
)
) %>%
layout(hoverlabel = list(bgcolor= 'white')) %>%
layout(showlegend = FALSE) %>%
add_annotations(x = teamAverageperweek$Quarter,
y = teamAverageperweek$average,
text = teamAverageperweek$average,
xref = "x",
yref = "y",
yanchor = 'center',
xanchor = 'top',
showarrow = FALSE)
Is it possible to display the text label above the points?
Thanks in advance!!
Yes it is possible, you can use textposition = "bottom center".
Here you can find the options for textposition.
Well, I modified yours code quite a bit, but I think It is more readable that way. I hope it can help you.
For example you can use:
teamAverageperweek %>% plot_ly(...) insted of plot_ly(data = teamAverageperweek)
x = ~Quarter insted of teamAverageperweek$Quarter
You can use only one trace and set mode = 'lines+markers+text' insted of add 2 traces and use annotations()
You also can set all parameters of layout(... , ...) separating it with commas.
Here is your code, but modified:
library(dplyr)
library(plotly)
# Yours data
teamAverageperweek = data.frame(
Quarter = c("2018 Q1","2018 Q2","2018 Q3","2018 Q4",
"2019 Q1","2019 Q2","2019 Q3"),
average = c(47.6,46.4,45.7,45.5,
45.7,46.3,45.7)
)
# The Plot
teamAverageperweek %>%
plot_ly(x = ~Quarter,
text = ~average,
hoverinfo = 'text',
hovertext = ~paste('Quarter of Date: ', Quarter,
'<br> Avg Hours Per Week: ',average),
showlegend = FALSE)%>%
add_trace(y = ~average,
type = 'scatter',
mode = 'lines+markers+text',
line = list(color = 'rgb(242,142,43)',
width = 3),
textposition = "bottom center", # here the text position
marker = list(color = 'rgb(242,142,43)',
size = 8)) %>%
layout(
yaxis = list(range = c(20,70),
title = "Avg Hours Per Week"
),
hoverlabel = list(bgcolor= 'white')
)
Here the output: