Avoiding a legend in plotly bar chart in r - 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))

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.

Trouble with creating bar & pie subplot with R plotly

I've created a Plotly bar and pie chart and want to combine them to form one chart.
When I use subplot() to combine these Plotly charts, the pie & bar charts overlap.
Any advice on how to present these plots so that each is in its own row? Thank you.
Here's a picture of what I'm currently experiencing:
Reprex below:
#Pie chart example
pie_tibble <- tibble(donuts = c(49050, 66924),
group = c("Group A", "Group B"))
pie <- plot_ly(data = pie_tibble, labels = ~group, values = ~donuts, type = 'pie',
showlegend = F,
hoverinfo = "none",
marker = ~list(colors = c('#404040', '#24608B'))) %>%
layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
bar_tibble <- tibble(category = c("Cat 1", "Cat 1", "Cat 2", "Cat 2"),
pct_increase = c(0.17, 0.25, 0.64, 0.85),
week = c(1, 2, 1, 2))
#Bar chart example
bar <- plot_ly(data = bar_tibble, hoverinfo = "none") %>%
layout(
barmode = 'stack',
showlegend = F) %>%
add_trace(
x = ~pct_increase,
y = ~category,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~category,
aggregations = list(
list(
target = "x", func = "avg", enabled = T)))))
#Combine charts
subplot(bar, pie, nrows = 2)
Pie charts and plotly::subplot() are notoriously challenging - though you can get around many of the issues by specifying the domain manually. Below I have changed the pie code by specifying the domain = list(...) as so:
pie <- plot_ly(data = pie_tibble, labels = ~group, values = ~donuts, type = 'pie',
# Specify the domain here
domain = list(x = c(0.5, 0.5), # centered on x axis
y = c(0.0, 0.4)),
showlegend = F,
hoverinfo = "none",
marker = ~list(colors = c('#404040', '#24608B'))) %>%
layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
subplot(bar, pie, nrows = 2) now gives:
Sorry I don't have a more elegant answer, but hoping someone else might!

Plot the line graph only for the non-zero columns using plot_ly() in R

I'm having a dataframe as follows
avgHours <- data.frame("QuarterYear" = c('2019 Q4','2020 Q1','2020 Q2'), average = c(44.5,44.5,42.0), leave = c(24.0,9.3,0.0), freeTime = c(0,0,0))
I need to plot 3 line graphs. In this case, I'm having a non-zero columns and 1 column with all the rows as 0. I need to omit that column while plotting.
This is the code for plotting the graph
plot_ly(avgHours) %>%
add_trace(x = avgHours$QuarterYear,
y = avgHours$average,
type = 'scatter',
mode = 'lines+markers+text',
hovertext = paste('Quarter: ', avgHours$QuarterYear,
'<br>Average hours per week: ', avgHours$average),
line = list(color = 'rgb(242,142,43)',
width = 3),
marker = list(color = 'rgb(242,142,43)',
size = 8),
hoverinfo = "text",
text = avgHours$average,
textposition = "top center",
name = "Average Hours")%>%
add_trace(x = avgHours$QuarterYear,
y = avgHours$leave,
type = 'scatter',
mode = 'lines+markers+text',
hovertext = paste('Quarter: ', avgHours$QuarterYear,
'<br>Average OOO hours per week: ', avgHours$leave),
line = list(color = 'rgb(205, 12, 24)',
width = 3),
marker = list(color = 'rgb(205, 12, 24)',
size = 8),
hoverinfo = "text",
text = avgHours$leave,
textposition = "top center",
name = "Leave Average Hours")%>%
add_trace(x = avgHours$QuarterYear,
y = avgHours$freeTime,
type = 'scatter',
mode = 'lines+markers+text',
hovertext = paste('Quarter: ', avgHours$QuarterYear,
'<br>Average Free Time per week: ', avgHours$freeTime),
line = list(color = '#003049',
width = 3),
marker = list(color = '#003049',
size = 8),
hoverinfo = "text",
text = avgHours$freeTime,
textposition = "top center",
name = "Free Time Average Hours")%>%
layout(
yaxis = list(
range = c(0,70),
title = "Average Hours Per Week"
)
) %>%
layout(hoverlabel = list(bgcolor= 'white'), showlegend = TRUE) %>%
layout(yaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
layout(legend = list(orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.13))
}
How can I plot the non-zero columns dynamically using plot_ly() in R?
Thanks in advance!!
You can have the add_trace in loop and specify the column selection by if condition as below:
p <- plot_ly(data = avgHours)
for(i in colnames(avgHours[-1])){
if(sum(avgHours[[i]])>0){
p <- p %>%
add_trace(x = avgHours[[1]],
y = avgHours[[i]],
type = 'scatter',
mode = 'lines+markers+text',
hovertext = paste('Quarter: ', avgHours[[1]],
'<br>Average hours per week: ', avgHours[[i]]),
line = list(width = 3),
marker = list(size = 8),
hoverinfo = "text",
text = avgHours[[i]],
textposition = "top center",
name = i)
}
}
p <- p %>%
layout(yaxis = list(
range = c(0,70),
title = "Average Hours Per Week")) %>%
layout(hoverlabel = list(bgcolor= 'white'), showlegend = TRUE) %>%
layout(yaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
layout(legend = list(orientation = "h", xanchor = "center", x = 0.5, y = -0.13))
If you want to have specific colours for each line and markers, you can add the list of colours and sub-setting it.

Allow user to select the data to plot in plotly 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'))

Two X-axis in Plotly for R

https://community.plot.ly/t/how-to-plot-multiple-x-axis-in-plotly-in-r/3014/3?u=him4u324
I have posted my question on Plotly community as well
I am trying to display two x-axis with common Y-axis on plotly for R. I was able to do so as well but starting point for each x-axis is separated from each other Whereas I wish them to be represented a common y-axis.
f1 <- list(
family = "Arial, sans-serif",
size = 18,
color = "grey"
)
f2 <- list(
family = "Old Standard TT, serif",
size = 14,
color = "#4477AA"
)
# bottom x-axis
ax <- list(
title = "Number of PBIs",
titlefont = f1,
anchor = "y",
side = "bottom",
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
# top x-axis
ax2 <- list(
title = " ",
overlaying = "x",
anchor = "y",
side = "top",
titlefont = f1,
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
# common y-axis
ay <- list(
title = "Process & Sub-Process Areas",
titlefont = f1,
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
plot_ly(data = scrum %>%
group_by(Master.Project) %>%
summarise(Total_PBIs_Planned=sum(PBIs.Planned.in.Sprint, na.rm = TRUE),
Total_PBIs_Delivered = sum(Actual.PBI.Delivery,na.rm = TRUE)) %>% inner_join(scrum %>% count(Master.Project)), # creating the desired data frame
color = I("#149EF7")) %>%
# for bottom x-axis
add_segments(x = ~Total_PBIs_Planned, xend = ~Total_PBIs_Delivered,
y = ~Master.Project, yend = ~Master.Project, showlegend = FALSE) %>%
add_trace(x = ~Total_PBIs_Planned, y = ~Master.Project,
name = "Total_PBIs_Planned", type = 'scatter',mode = "markers",
marker = list(color = "#149EF7", size = 15,
line = list(color = '#FFFFFF', width = 1))) %>%
add_trace(x = ~Total_PBIs_Delivered, y = ~Master.Project,
name = "Total_PBIs_Delivered",type = 'scatter',mode = "markers",
marker = list(symbol ="circle-dot",color = "#F71430", size = 10,
line = list(color = '#FFFFFF', width = 1))) %>%
# for top x-axis
add_trace(x = ~n, y = ~Master.Project, xaxis = "x2",
name = "No._of_Sub_projects",type = 'bar',
marker = list(color = "#149EF7"),
opacity = 0.1,
hoverinfo = 'text',
text = ~paste(
Master.Project,
'<br> Total Sub Projects: ',n,
'<br> PBIs Planned: ',Total_PBIs_Planned,
'<br> PBIs Delivered: ',Total_PBIs_Delivered
)
) %>%
plotly::layout(
title = "Product Backlog Items - Planned Vs Delivered", titlefont = f1,
xaxis = ax,
yaxis = ay,
xaxis2 = ax2,
margin = list(l = 250)
)
You want to use rangemode = "tozero" in your axis layout.
ax <- list(
title = "Number of PBIs",
titlefont = f1,
anchor = "y",
side = "bottom",
showticklabels = TRUE,
tickangle = 0,
tickfont = f2,
rangemode = 'tozero')
Or you can specify your specific ranges using
range = c(0, 5)
See Plotly help here: https://plot.ly/r/axes/#rangemode

Resources