R plotly to add traces conditionally based on available columns in dataframe - r

I have a dataframe in my Shiny app that gets filtered based on various user inputs.
global_evolution=reactive({
results_combined %>%
filter(!is.na(SVM_LABEL_QOL) & SVM_LABEL_QOL=='QoL' & globalsegment==input$inp_pg1segment & Account==input$inp_pg1clientsfiltered & Date >=input$inp_pg1daterange[1] & Date <=input$inp_pg1daterange[2]) %>% #Inputs
select(Account,Date,SVM_LABEL_DIMENSION) %>%
mutate(Month=month(as.Date(format(as.POSIXct(Date),format = "%d/%m/%Y"),"%d/%m/%Y"))) %>%
select(Account,Month,SVM_LABEL_DIMENSION,-Date) %>%
group_by(Month,SVM_LABEL_DIMENSION) %>%
summarise(Monthly_Count=n()) %>%
spread(SVM_LABEL_DIMENSION,Monthly_Count) %>%
ungroup() %>%
mutate(Month=month.abb[Month]) %>%
mutate_all(funs(replace(., is.na(.), 0)))
})
In the next step, I pass this filtered dataframe through a plot_ly function
Here is where I need help
I am trying to get plot_ly to conditionally add lines (add traces), based on whether the given column is available in the dataframe or not. At the moment, plot_ly throws an error if any of the columns included in the add_traces is not available after the dataframe is filtered.
Here is the part of my Shiny app with the plot_ly output.
I attempted to add if-else statements between the add_trace arguments, but my attempts haven't been successful.
output$pg1evolution <- renderPlotly({
global_evolution_final() %>%
plot_ly(x = ~Month, y = ~`COLUMN_1`, name = 'Column 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~`COLUMN_2`, name = 'Column 2') %>%
add_trace(y = ~`COLUMN_3`, name = 'Column 3') %>%
add_trace(y = ~`COLUMN_4`, name = 'Column 4') %>%
add_trace(y = ~`COLUMN_5`, name = 'Column 5') %>%
add_trace(y = ~`COLUMN_6`, name = 'Column 6') %>%
layout(title = "Trend Over Time",
xaxis = list(title = ""),
yaxis = list (title = "Monthly Count of Products Sold"))
})
My apologies for not being able to include a reproducible dataframe, I realise that would make things easier.
Very grateful for any tips/pointers you might have.

One way to do it could be to use a for loop to add a trace per column
output$pg1evolution <- renderPlotly({
colNames <- names(global_evolution_final())[-1] #Assuming Month is the first column
p <- plotly::plot_ly(data = global_evolution_final(), x = ~Month, type = "scatter",
mode = "lines")
for(trace in colNames){
p <- p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}
p %>%
layout(title = "Trend Over Time",
xaxis = list(title = ""),
yaxis = list (title = "Monthly Count of Products Sold"))
})

Related

How to create a tooltip chart in r using highcharter?

I am using highcharter library and referred to below link to create an interactive tooltip chart in a bubble chart
https://jkunst.com/blog/posts/2019-02-04-using-tooltips-in-unexpected-ways/
Plot image:
Using gapminder data as shown in link I was able to reproduce the same but when I use my other data then the tool tip chart doesn't appear.
Code for my other data:
libs
library(tidyverse)
library(highcharter)
data
grouped_cases_df <- read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/grouped_cases.csv")
tt_base <- grouped_cases_df %>%
arrange(desc(Date)) %>%
distinct(Country.Region, .keep_all = TRUE)
tt_base
tt_inner <- grouped_cases_df %>%
select(Country.Region, Date, Daily_cases) %>%
nest(-Country.Region) %>%
mutate(
data = map(data, mutate_mapping, hcaes(x = Date, y = Daily_cases), drop = TRUE),
data = map(data, list_parse)
) %>%
rename(tt_nestdata = data)
tt_inner
tt_daily <- left_join(tt_base, tt_inner, by = "Country.Region")
tt_daily
hchart(
tt_daily,
"point",
hcaes(x = Active, y = Confirmed, name = Country.Region,
size = Daily_cases, group = continent, name = Country.Region)
) %>%
hc_yAxis(type = "logarithmic") %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = "<b>{point.key}</b>",
pointFormatter = tooltip_chart(accesor = "tt_nestdata")
) %>%
hc_title(text = "Active Vs Confirmed Cases as of latest Date") %>%
hc_subtitle(text = "Size of bubble based on Deaths <br> (ttchart: population growth)")
Issue: Getting blank tooltip chart for every country.
I also tried by changing Country.Region to as.factor() but didn't help. I am not sure whats wrong with this.
It's needed make two changes:
The tooltip data needs to be ready to highcharter. So you need to transform the Date column from text to date then to a numeric value which highcharts can interpret as date:
mutate(Date = highcharter::datetime_to_timestamp(lubridate::ymd(Date)))
Then, in the hc_opts argument in the tooltip_chart function you need to specify the x Axis treat the values as date.
pointFormatter = tooltip_chart(accesor = "tt_nestdata", hc_opts = list(xAxis = list(type = "datetime")))
Then:

Plotly animated map not showing countries with NA values

I posted this in the plotly community forum but got absolutely no activity! Hope you can help here:
I have map time-series data, some countries don’t have data and plotly does not plot them at all. I can have them outlined and they look different but it appears nowhere that the data is missing there (i.e. I want a legend entry). How can I achieve this? Here is a reprex:
library(plotly)
library(dplyr)
data = read.csv('https://github.com/lc5415/COVID19/raw/master/data.csv')
l <- list(color = toRGB("grey"), width = 0.5)
g <- list(
scope = 'world',
countrycolor = toRGB('grey'),
showframe = T,
showcoastlines = TRUE,
projection = list(type = 'natural earth')
)
map.time = data %>%
plot_geo() %>%
add_trace(z = ~Confirmed, color = ~Confirmed, frame = ~Date, colors = 'Blues',
text = ~Country, locations = ~Alpha.3.code, marker = list(line = l)) %>%
colorbar(title = 'Confirmed') %>%
layout(
title = 'Number of confirmed cases over time',
geo = g
) %>%
animation_opts(redraw = F) %>%
animation_slider(
currentvalue = list(
prefix = paste0("Days from ",
format(StartDate, "%B %dnd"),": "))) %>%
plotly_build()
map.time
Note that the countries with missing data (e.g. Russia) have as many data points as all other countries, the issue is not that they do not appear in the dtaframe passed to plotly.
The obvious way to handle this is to create a separate labels column for the tooltip that reads "No data" for NA values (with the actual value otherwise), then make your actual NA values 0. This will give a uniform appearance to all the countries but correctly tells you when a country has no data.
map.time = data %>%
mutate_if(is.numeric, function(x) {x[is.na(x)] <- -1; x}) %>%
plot_geo() %>%
add_trace(z = ~Confirmed, color = ~Confirmed, frame = ~Date, colors = 'Blues',
text = ~Country, locations = ~Alpha.3.code,
marker = list(line = l)) %>%
colorbar(title = 'Confirmed') %>%
layout(
title = 'Number of confirmed cases over time',
geo = g
) %>%
animation_opts(redraw = F) %>%
animation_slider(
currentvalue = list(
prefix = paste0("Days from ",
format(StartDate, "%B %dnd"),": "))) %>%
plotly_build()
Which gives:

Highcharter stacked column with series length == 1: Category name is not correct

I'm trying to draw stacked column chart with series length == 1.
Category name is not correct (this.category.name). It's not just about labels, I use this.category.name in return. Please help.
chart <- highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = c("Apples")) %>%
hc_add_series(c(5), name = "John") %>%
hc_add_series(c(3), name = "Jane") %>%
hc_add_series(c(2), name = "Joe") %>%
hc_plotOptions(column = list(stacking = "normal"))
chart
You need to pass list instead of vector in categories for hcXaxis:
library(dplyr)
library(highcharter)
chart <- highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = list("Apples")) %>%
hc_add_series(c(5), name = "John") %>%
hc_add_series(c(3), name = "Jane") %>%
hc_add_series(c(2), name = "Joe") %>%
hc_plotOptions(column = list(stacking = "normal"))
chart
The problem is in defining the x axis labels, called categories in highcharter. I have made other changes to the posted code.
The main change is to have categories as a list.
vectors with just one element, c("Apple") or c(5), were simplified.
And the code becomes:
library(highcharter)
chart <- highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = list("Apples")) %>%
hc_add_series(5, name = "John") %>%
hc_add_series(3, name = "Jane") %>%
hc_add_series(2, name = "Joe") %>%
hc_plotOptions(column = list(stacking = "normal"))
chart

Plotting two columns using highchart in R shiny returns argument object is missing with no value

I tried to make interactive map using R shiny which will show plot of male and female citizens in some cities. Data frame sample is shown below.
df1 <- read.table(header = TRUE, text = "city,year,male,female,long,lat
A,2017,1038,876,35.54331,139.12333
A,2018,1281,911,35.54331,139.12333
B,2017,832,517,35.14189,140.664113
B,2018,914,589,35.14189,140.664113", sep = ",")
df2 <- df1
The interactive map is built by using leaflet package and if the city marker is clicked, a plot which built by highchart will be shown.
output$chart <- renderHighchart({
df2 <- df1[df1$city == click_marker(),]
hchart() %>%
hc_add_series(df2, "column", hcaes(x = year, y = male, group = city, name = "Male")) %>%
hc_add_series(df2, "column", hcaes(x = year, y = female, group = city, name = "Female")) %>%
hc_xAxis(title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "Amount (Thousands)"))
})
highchartOutput('chart')
I expect the output is a plot that show amount of male and female in the city for each year given but the output that I got is "argument object is missing, with no default."
What if you change hchart to highchart and the names to lowercase? Apparently they have to be found in the data.frame.
highchart() %>%
hc_add_series(df2, "column", hcaes(x = year, y = male, group = city, name = "male")) %>%
hc_add_series(df2, "column", hcaes(x = year, y = female, group = city, name = "female")) %>%
hc_xAxis(title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "Amount (Thousands)"))

R Plotly unable to remove trace 0 from bar chart

In my Shiny app, plotly produces a trace 0 in the legend that imbalances my graph.
This is what the graph looks like (notice the trace 0 in the legend).
However on clicking on trace 0 in the legend, the graph returns back to normal
Is there a way of removing this trace 0 from my plot completely?
Here is my code:
1) My dataframe is first filtered inside a reactive function
global_evolution=reactive({
results_combined %>%
filter(!is.na(SVM_LABEL_QOL) & SVM_LABEL_QOL=='QoL' & globalsegment==input$inp_pg1segment & Account==input$inp_pg1clientsfiltered & Date >=input$inp_pg1daterange[1] & Date <=input$inp_pg1daterange[2]) %>% #Input: Account
select(Account,Date,SVM_LABEL_DIMENSION) %>%
mutate(Month=month(as.Date(format(as.POSIXct(Date),format = "%d/%m/%Y"),"%d/%m/%Y"))) %>%
select(Account,Month,SVM_LABEL_DIMENSION,-Date) %>%
group_by(Month,SVM_LABEL_DIMENSION) %>%
summarise(Monthly_Count=n()) %>%
spread(SVM_LABEL_DIMENSION,Monthly_Count) %>%
ungroup() %>%
mutate(Month=month.abb[Month]) %>%
mutate_all(funs(replace(., is.na(.), 0)))
})
2) Then some more changes are made to the filtered dataframe inside another reactive function
global_evolution_final=reactive({
global_evolution() %>%
mutate(Month=factor(Month,levels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")))
})
3) Finally I use plot_ly to construct the bar chart. However the trace 0 cannot be removed
output$pg1evolution <- renderPlotly({
colNames <- names(global_evolution_final())[-1] #Assuming Month is the first column
p <- plotly::plot_ly(data = global_evolution_final(), x = ~Month, type = "bar")
for(trace in colNames){
p <- p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}
p %>%
layout(title = "Trend Over Time",showlegend = FALSE,
xaxis = list(title = ""),
yaxis = list (title = "Monthly Count of QoL Tweets"))
})
Any help with this would be greatly appreciated.
My apologies in advance for not being able to include reproducible data.
There's a problem with your approach.
Check this below reproducible code to fix yours.
df <- iris
p <- plotly::plot_ly()
colNames <- names(df)
colNames <- colNames[-which(colNames == 'Species')]
for(trace in colNames){
p <- p %>% plotly::add_trace(data= df, x = ~ Species, y = as.formula(paste0("~`", trace, "`")), name = trace)
print(paste0("~`", trace, "`"))
}
p
Ideally, Your modified code should be something like this:
output$pg1evolution <- renderPlotly({
colNames <- names(global_evolution_final())[-1] #Assuming Month is the first column
p <- plotly::plot_ly()
for(trace in colNames){
p <- p %>% plotly::add_trace(data = global_evolution_final(), x = ~Month, y = as.formula(paste0("~`", trace, "`")), name = trace, type = "bar")
}
p %>%
layout(title = "Trend Over Time",showlegend = FALSE,
xaxis = list(title = ""),
yaxis = list (title = "Monthly Count of QoL Tweets"))
})

Resources