I am trying to produce a ribbon on my highcharter chart (roughly following is there an equivalent to geom_ribbon in highcharter?).
However, the following example to produce a highcharter graph in R produces an error:
library(quantmod)
library(dplyr)
library(highcharter)
getSymbols("VOD")
bb_data = BBands(Cl(VOD), n=20)
highchart(type = "stock") %>%
hc_add_series(bb_data, type = "arearange", hcaes(low = dn, high=up))
The error is:
Error: 'hcaes(low = dn, high = up)' argument is not named in hc_add_series
I have think this is because it is a time series object (xts).
It works if I cast it to a data.frame, but then I lose the date.
highchart(type = "stock") %>%
hc_add_series(as.data.frame(bb_data), type = "arearange", hcaes(low = dn, high=up))
I cannot combine it to with the moving average or price data as I would wish, as the ribbon is then missing from the subsequent plot:
highchart(type = "stock") %>%
hc_add_series(Cl(VOD), name = "VOD") %>%
hc_add_series(bb_data$mavg, name = "20d MA") %>%
hc_add_series(as.data.frame(bb_data), type = "arearange", hcaes(low = dn, high=up))
ok, so I had to first extract the date from the time series object and bind it with the time series object to form a data frame or data table and then plot using that.
bb_data2 = cbind(date = as.Date(index(bb_data)), data.table(bb_data))
highchart(type = "stock") %>%
hc_add_series(bb_data2, type = "arearange", hcaes(x=date, low = dn, high=up)) %>%
hc_add_series(Cl(VOD), name = "VOD") %>%
hc_add_series(bb_data$mavg, name = "20d MA")
Related
I have a dataset, more especifically the Unicorn Company data set. I want to create an interactive choropleth that has countries with higher mean valuation would have darker color, that when user clicks on the country it would display the name + valuation of that country.
output$map_plot <- renderPlotly({
# Get the average valuation for each country
industry_investors_data <- unicorn_countries_clustering_cleaned %>%
group_by(Country) %>%
summarize(Valuation = mean(Valuation...B.))
world_map_data <- map_data("world2")
#print(sort(unique(ggplot2::map_data("world")$region)))
# Merge the map data with your data and fill in missing values
world_map_valuation <- world_map_data %>%
right_join(industry_investors_data, by = c("region" = "Country")) %>%
mutate(Valuation = coalesce(Valuation, 0.0))
plot_ly(data = world_map_valuation,
locations = ~region,
z = ~Valuation,
type = "choropleth",
locationmode = "country names",
color = ~Valuation,
colors = "Blues",
title = "Map of the world by country valuation",
showlegend = FALSE)
})
This shows a map on the worldly valuation however it takes very long to render and is not interactive in any way. Before I had left_join instead of right join the result was the same.
I have this data frame
df <- data.frame(subjects = 12:23,
Why_are_you_not_happy =
c(1,2,"1,2,5",5,1,2,"3,4",3,2,"1,5",3,4),
why_are_you_sad =
c("1,2,3",1,2,3,"4,5,3",2,1,4,3,1,1,1))
df
that is coverted into this format
df1 <- df %>%
separate(Why_are_you_not_happy,
sep = ",", into = c("Why_are_you_not_happy_1",
"Why_are_you_not_happy_2",
"Why_are_you_not_happy_3")) %>%
separate(why_are_you_sad,
sep = ",", into = c("why_are_you_sad_1",
"why_are_you_sad_2",
"why_are_you_sad_3"))
when applying the MCA function we take all the columns except the first one
library(FactoMineR)
library(factoextra)
#> Loading required package: ggplot2
results <- MCA(df1[,2:7])
using the following function, I wish to assign a specific shape and a specific colour for each group of categories (meaing for each variable )
fviz_mca_var(results,
repel = TRUE,
ggtheme = theme_minimal())
I am using highstock with highcharts R api. https://jkunst.com/highcharter/articles/stock.html
I have multiple time series on my graph and I would like a way to toggle them on and off like in the usual hchart() function. Below is a reproducible example. I would like to be able to toggle on and off the blue line.
library(quantmod)
library(highcharter)
library(tidyverse)
getSymbols(c("SPY", 'QQQ'))
highchart(type = "stock") %>%
hc_title(text = "Charting some Symbols") %>%
hc_subtitle(text = "Data extracted using quantmod package") %>%
hc_add_series(Cl(SPY), id = "spy", name = "SPY") %>%
hc_add_series(Cl(QQQ), id = "qqq", name = "QQQ")
TLDR: I want to label the frame slider with the three letter abbreviation instead of the number for each month.
I created a bar chart showing average snow depth each month over a 40 year period. I'm pulling my data from NOAA and then grouping by year and month using lubridate. Here is the code:
snow_depth <- govy_data$snwd %>%
replace_na(list(snwd = 0)) %>%
mutate(month_char = month(date, label = TRUE, abbr = TRUE)) %>%
group_by(year = year(date), month = month(date), month_char) %>%
summarise(avg_depth = mean(snwd))
The mutate function creates a column (month_char) in the data frame holding the three letter abbreviation for each month. The class for this column is an ordered factor.
The code below shows how I'm creating the chart/animation:
snow_plot <- snow_depth %>% plot_ly(
x = ~year,
y = ~avg_depth,
color = ~avg_temp,
frame = ~month,
text = ~paste('<i>Month</i>: ', month_char,
'<br><b>Avg. Depth</b>: ', avg_depth,
'<br><b>Avg. Temp</b>: ', avg_temp),
hoverinfo = 'text',
type = 'bar'
)
snow_plot
This code generates a plot that animates well and looks like this:
What I'd like to do is change the labels on the slider so instead of numbers, it shows the three letter month abbreviation. I've tried switching the frame to ~month_char which is the ordered factor of three letter month abbreviations. What I end up with, isn't right at all:
The data frame looks like:
I fear, with the current implementation of animation sliders in R's plotly API the desired behaviour can't be realized. This is due to the fact, that no custom animation steps are allowed (this includes the labels). Please see (and support) my GitHub FR for further information.
This is the best I was currently able to come up with:
library(plotly)
DF <- data.frame(
year = rep(seq(1980L, 2020L), each = 12),
month = rep(1:12, 41),
month_char = rep(factor(month.abb), 41),
avg_depth = runif(492)
)
fig <- DF %>%
plot_ly(
x = ~year,
y = ~avg_depth,
frame = ~paste0(sprintf("%02d", month), " - ", month_char),
type = 'bar'
) %>%
animation_slider(
currentvalue = list(prefix = "Month: ")
)
fig
(Edit from OP) Here's the resulting graph using the above code:
I have a large shiny app that allows users to filter through an API and spark table aggregate (dumped to an .Rdata) simultaneously using the same set of initially selectized parameters. Fitting all this into a reproducible example is going to be tough, but, this is the function that is grouping and summming my metric of interest (try to resist asking me to paste in partitionFiltered()):
df <- reactive({partitionFiltered() %>%
dplyr::group_by(updatedTimeHour, direction) %>%
dplyr::mutate(count_dir = sum(n_flows)) %>%
dplyr::ungroup() %>%
dplyr::select(updatedTimeHour, direction, count_dir) %>%
dplyr::arrange(updatedTimeHour) %>%
unique()})
(Eventually, updatedTimeHour and direction will be replaced by input$periodicity and input$dimension, respectively, but that is beyond the scope of this question.)
The df() object looks like:
updatedTimeHour direction count_dir
6 1 525071.00
6 2 3491.00
6 0 498816.00
6 3 5374.00
7 2 2432.00
7 0 303818.00
7 1 340768.00
7 3 4852.00
8 1 1969048.00
My highcharter calls do not seem to be grouping and coloring the aesthetics as I would expect:
hc <- highchart() %>%
hc_add_series(data = df()$count_dir,
type = input$plot_type,
name = factor(df()$direction)
showInLegend = TRUE,
# ??group = df()$direction,
# ??color = df()$direction,
# ??x = df()$updatedTimeHour, y = df()$count_dir, color = df()$direction,
# ??hcaes(x = df()$updatedTimeHour, y = df()$count_dir, color = df()$direction)
) %>%
hc_xAxis(type = 'datetime',
# ??group = factor(df()$direction),
categories = df()$updatedTimeHour,
tickmarkPlacement = "on",
opposite = FALSE) %>%
hc_title(text = "NetFlows, by Hour",
style = list(fontWeight = "bold")) %>%
hc_exporting(enabled = TRUE, filename = "threat_extract")
As you can probably tell, I'm very confused about where/how to map the x-grouping udpatedTimeHour, or color the different direction levels appropriately and make sure their group ends up mapped correctly to the labels in the legend and hover.
I have also attempted to map these aesthetics using the hcaes() call I see included as an argument to hc_add_series() in some of the documentation, but I get errors thrown saying that that argument is not (any longer?) named in that hc_ function...
Any help is appreciated, and a related question is here.
You are trying to add as one series multiple objects that's the reason why is not working. Just changing a little bit your code and using the "magic" function hchart it should work:
df = data_frame(updatedTimeHour = c(6,6,6,6,7,7,7,7,8), direction = c(1,2,0,3,2,0,1,3,1), count_dir = rnorm(9))
type = "line"
hchart(df, type, hcaes(x = updatedTimeHour, y = count_dir, group = as.factor(direction))) %>%
hc_title(text = "NetFlows, by Hour",
style = list(fontWeight = "bold")) %>%
hc_exporting(enabled = TRUE, filename = "threat_extract")