Allow user to select the data to plot in plotly r - r

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'))

Related

Hover is not spot on in R plotly with hovermode 'x unified'

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.

Localize plotly plot time axis for dates in R

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.

R plotly: Stacked Area Chart with Cumulative Values not stacking properly

I've got three columns of data I would like to plot as a cumulative stacked area chart over a 10 day sampling period.
ID variable value
dallas sample.01 0.0012
austin sample.01 0.23
seattle sample.01 0.01
I'd like it to look something like this:
But it's coming out like this:
What am I doing wrong with my code?
melted_dat %>%
group_by(value,ID) %>%
plot_ly(
x = ~variable,
y = ~value,
color = ~ID,
type='scatter',
mode = 'none',
fill = 'tonexty',
stackgroup = 'one',
fillcolor = ~ID) %>%
layout(showlegend = FALSE)
I think you need to add the groups trace by trace. As in the following example (from here):
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)
p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', stackgroup = 'one', groupnorm = 'percent', fillcolor = '#F5FF8D') %>%
add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86') %>%
add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9') %>%
add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961') %>%
add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44') %>%
layout(title = 'United States Personal Expenditures by Categories',
xaxis = list(title = "",
showgrid = FALSE),
yaxis = list(title = "Proportion from the Total Expenditures",
showgrid = FALSE,
ticksuffix = '%'))
# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="area-stackedcum")
chart_link
If you are following the cumulative example from plotly, don't do the group_by .... cumsum, I also get rid of fillcolor

Avoiding a legend in plotly bar chart in r

In this graph Bar chart with description I would like to hide the text that appearing into the square in the attached image.
p <- plot_ly(
x = ~df1$x1,
y = ~df1$y1,
name = "Most frequent words",
type = "bar",
mode="markers" ,
color = ~df1$x1 ,
colors=c("red","blue") , showlegend = FALSE,
) %>%
layout(
title = "Most frequent words for #Dante2018",
yaxis = list(title = 'Frequency'),
xaxis = list(title = 'Word', tickangle = -45))
Try adding hoverinfo = 'x+y' like so -
p <- plot_ly(
x = ~df1$x1,
y = ~df1$y1,
name = "Most frequent words",
type = "bar",
mode="markers" ,
color = ~df1$x1 ,
colors=c("red","blue") , showlegend = FALSE,
hoverinfo = 'x+y'
) %>%
layout(
title = "Most frequent words for #Dante2018",
yaxis = list(title = 'Frequency'),
xaxis = list(title = 'Word', tickangle = -45))

R Plotly Rangeslider covers tick-labels - Is it possible to set location of Rangeslider?

I´m using the Plotly package in R to make a plot with both a rangeslider and rangeselector.
The problem is that the rangeslider tends to cover the x-axis tick labels.
A solution could be to manually set the location of the rangeslider, but I can't seem to find any documentation on how to do that.
Below you find a minimal working example of the problem together with a picture of it.
# Make Some Data:
Dates = as.POSIXct(c("2017-08-08 00:00")) + (0:71)*60^2
Values = rep_len(mtcars$mpg, 72)
tb = dplyr::tibble(Values, Dates)
# Plot
p = tb %>% plot_ly(type = "scatter", mode = 'markers', x = ~Dates, y = ~Values) %>%
layout(xaxis = list(
rangeslider = list(type = "date"),
rangeselector = list(
buttons = list(list(step = "all", label = "All")))
))
p
Any help will be greatly appreciated!
Cheers,
By messing around with various options/settings, I found that increasing the tick lengths and let the ticks appear on the inside of x-axis rather than on the outside solved my problem.
See the code below for an example
# Make Some Data:
Dates = as.POSIXct(c("2017-08-08 00:00")) + (0:71)*60^2
Values = rep_len(mtcars$mpg, 72)
tb = dplyr::tibble(Values, Dates)
# Plot
p = tb %>% plot_ly(type = "scatter", mode = 'markers', x = ~Dates, y = ~Values) %>%
layout(xaxis = list(ticks = "inside", ticklen = 10,
rangeslider = list(type = "date", thickness=0.1),
rangeselector = list(
buttons = list(list(step = "all", label = "All")))
))
p
However, this is a hacker solution, and I´m still on the lookout for a method to set the position of the rangeslider.
Try to solve the overlapping problem tuning the thickness parameter of rangeslider:
# Make Some Data:
Dates = as.POSIXct(c("2017-08-08 00:00")) + (0:71)*60^2
Values = rep_len(mtcars$mpg, 72)
tb = dplyr::tibble(Values, Dates)
# Plot
p = tb %>% plot_ly(type = "scatter", mode = 'markers', x = ~Dates, y = ~Values) %>%
layout(xaxis = list(
rangeslider = list(type = "date", thickness=0.3),
rangeselector = list(
buttons = list(list(step = "all", label = "All")))
))
p
Hope it can help you.

Resources