R highcharter x-axis date issue - r

I have a data frame contains month-end data. I'm using the stacked column chart but have an issue with the x-axis date labels. For example, it shows April under the March column.
library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30','2020-05-31','2020-05-31','2020-06-30')), Value = c(1,2,3,4,5,6), Country = c('US','Mexico','US','Canada','US','Canada'))
hchart(df, "column", hcaes(Date, Value, group = Country)) %>%
hc_plotOptions(column = list(stacking = "normal"))
I tried to specify the type and labels by adding hc_xAxis(type = 'datetime', labels = list(format = '{value:%m-%Y}')), but it doesn't help. The tooltip shows the correct dates.

A simple and valuable solution is to transform dates into characters.
library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30',
'2020-05-31','2020-05-31','2020-06-30')),
Value = c(1,2,3,4,5,6),
Country = c('US','Mexico','US','Canada','US','Canada'))
df$Date <- as.character(df$Date)
hchart(df, "column", hcaes(Date, Value, group = Country)) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_xAxis(type="category", categories=unique(df$Date))

Related

Plotly wrongly filling gaps based on color factor in R

I am simply trying to get every month of data to plot the same color.
Plotly is falsely plotting lines that connect each factor color together, generating a giant mess on the chart.
connectgaps = FALSE does not work, because the gaps are not in the data. They are being generated by plotly on the ~factor(month), which colors each month the same.
This has something to do with multiple observations of the factor, rather than just one. If the factor was based on year, rather than month, no line traces would occur.
How can I remove these lines? Thank you!
library(tidyverse)
library(lubridate)
library(plotly)
start_date <- as.Date('2020-1-1')
end_date <- as.Date('2023-2-5')
d1 <- tibble::tibble(date = seq(min(start_date), max(end_date), by = "1 day"))
d2 <- tibble::tibble(rnorm(1132,2))
d3 <- data.frame(d1,d2)
d3 <- d3 %>% mutate(month = month(date))
colnames(d3) <- c('date', 'var', 'month')
plot_ly(d3, x = ~date, y = ~var, type = "scatter", mode = "lines", color = ~factor(month), connectgaps =FALSE)
You could add another column which is the year so you can group on each year using group_by to prevent connecting the lines per month for each year like this:
start_date <- as.Date('2020-1-1')
end_date <- as.Date('2023-2-5')
library(lubridate)
library(dplyr)
library(plotly)
d1 <- tibble::tibble(date = seq(min(start_date), max(end_date), by = "1 day"))
d2 <- tibble::tibble(rnorm(1132,2))
d3 <- data.frame(d1,d2)
d3 <- d3 %>% mutate(month = month(date),
year = year(date))
colnames(d3) <- c('date', 'var', 'month', 'year')
d3 %>%
group_by(year) %>%
plot_ly(x = ~date, y = ~var, type = "scatter", mode = "lines", color = ~factor(month))
Created on 2023-02-05 with reprex v2.0.2
Idea nr. 2:
library(dplyr)
library(lubridate)
library(plotly)
d3 %>%
mutate(year = year(date)) %>%
group_by(year) %>%
plot_ly(x = ~ yday(date)) %>%
add_lines(y = ~var,
color = ~ factor(month))
To answer you question with the lines:
In ggplot when we use group = 1 like:
ggplot(d3, aes(x = date, y = var, group = 1, color = factor(month)))+
geom_line()
We get:

How can arrange the date of a column in ascending order using R

I have a dataset containing details about date, continent and continental_sum(total death in each continent) which is show in the image.
But the date column is not arranged in an order, how can I arrange should I use lubridate?
typeof(continental_death_total$date)
[1] "character"
library(lubridate)
continental_death_total%>%
mutate(date = dmy(date))
Tried the above code and the date column gets arranged in ascending order
but when I tried to plot a barplot it shows different values.
The code for barplot
df <- continental_death_total
fig <- df %>%
plot_ly(
x = ~continent,
y = ~continental_sum,
frame = ~date,
type = 'bar',
mode = 'markers',
showlegend = F
)
fig
Output of Bar Plot
Try to change your edge x with date and add varible color with continent, if you want a grouped barplot:
df <- continental_death_total
fig <- df %>%
plot_ly(
x = ~date,
y = ~continental_sum,
color = ~continent,
type = 'bar',
mode = 'markers',
showlegend = F
)
fig

Add average line/plot line to area highchart R

I know there are a few similar questions to this out there but they all seem to use javascript (?) or something besides the normal R coding so I don't know how to use it in my code... anyways all I want to do is add a plotline to my area chart that shows the average of the values, how do I do that? I know that highcharter itself can not calculate the average so I can do that myself but how do I create the plotline .... thank you so much. (i tried to make the code so that it is easily 'reproducible' ? hope it is ok). I attached a picture of the current chart if that helps.
library(tidyverse)
library(highcharter)
library(ggplot2)
data("diamonds", package = "ggplot2")
df <- diamonds %>%
group_by(cut)%>%
count()
head(df, 4)
# Create chart
hc <- df %>%
hchart(
'area', hcaes(x = cut, y = n),
color = "lightblue"
) %>%
hc_yAxis(title = list(text = "cut"))
# Display chart
hc
Below is a mini example of using the highcharts widget. You can add each series using hc_add_series. In this case, we have two series and two y-axes. Using two y-axes helps to differentiate between the series. I'm not sure what values you're trying to calculate the average so I chose price.
Hope this helps add some clarity to highcharter!
library(tidyverse)
library(highcharter)
df <- diamonds %>%
group_by(cut)%>%
summarise(
n = n(),
avg_price = round(mean(price),2)
)
# create hc widget
highchart(type = "chart") %>%
# add both series
hc_add_series(df, hcaes(x = cut, y = n), color = "lightblue", yAxis = 0, type = "area", name = "N") %>%
hc_add_series(df, hcaes(x = cut, y = avg_price), yAxis = 1, type = "line", name = "Avg Price") %>%
# set type to categories since we're looking at categorical data
hc_xAxis(type = "category", categories = df$cut) %>%
hc_title(text = "Cut Freq vs Avg Price") %>%
# add each y-axis which is linked above in 'hc_add_series'
hc_yAxis_multiples(
list(title = list(text = "Cut")), # yAxis = 0
list(title = list(text = "Average Price"), opposite = TRUE) # yAxis = 1
) %>%
hc_tooltip(shared = TRUE, split = FALSE)
Ex:
Haha I got it. basically just this.
plotline <- list(
color = "red", value = mean(diamonds$cut), width = 2, zIndex = 5
)
hc_yAxis(plotLines = list(plotline))

Spaghetti plot using ggplot in R?

I would like to produce a speghatii plot where i need to see days of the year on the x-axis and data on the y-axis for each Year. I would then want a separate year that had data for only 3 months (PCPNewData) to be plotted on the same figure but different color and bold line. Here is my sample code which produce a graph (attached) where the data for each Year for a particular Day is stacked- i don't want bar graph. I would like to have a line graph. Thanks
library(tidyverse)
library(tidyr)
myDates=as.data.frame(seq(as.Date("2000-01-01"), to=as.Date("2010-12-31"),by="days"))
colnames(myDates) = "Date"
Dates = myDates %>% separate(Date, sep = "-", into = c("Year", "Month", "Day"))
LatestDate=as.data.frame(seq(as.Date("2011-01-01"), to=as.Date("2011-03-31"),by="days"))
colnames(LatestDate) = "Date"
NewDate = LatestDate %>% separate(Date, sep = "-", into = c("Year", "Month", "Day"))
PCPDataHis = data.frame(total_precip = runif(4018, 0,70), Dates)
PCPNewData = data.frame(total_precip = runif(90, 0,70), NewDate)
PCPDataHisPlot =PCPDataHis %>% group_by(Year) %>% gather(key = "Variable", value = "Value", -Year, -Day,-Month)
ggplot(PCPDataHisPlot, aes(Day, Value, colour = Year))+
geom_line()+
geom_line(data = PCPNewData, aes(Day, total_precip))
I would like to have a Figure like below where each line represent data for a particular year
UPDATE:
I draw my desired figure with hand (see attached). I would like to have all the days of the Years on x-axis with its data on the y-axis
You have few errors in your code.
First, your days are in character format. You need to pass them in a numerical format to get line being continuous.
Then, you have multiple data for each days (because you have 12 months per year), so you need to summarise a little bit these data:
Pel2 <- Pelly2Data %>% group_by(year,day) %>% summarise(Value = mean(Value, na.rm = TRUE))
Pel3 <- Pelly2_2011_3months %>% group_by(year, day) %>% summarise(total_precip = mean(total_precip, na.rm = TRUE))
ggplot(Pel2, aes(as.numeric(day), Value, color = year))+
geom_line()+
geom_line(data = Pelly2_2011_3months, aes(as.numeric(day), y= total_precip),size = 2)
It looks better but it is hard to apply a specific color pattern
To my opinion, it will be less confused if you can compare mean of each dataset, such as:
library(tidyverse)
Pel2 <- Pelly2Data %>% group_by(day) %>%
summarise(Mean = mean(Value, na.rm = TRUE),
SEM = sd(Value,na.rm = TRUE)/sqrt(n())) %>%
mutate(Name = "Pel_ALL")
Pel3 <- Pelly2_2011_3months %>% group_by(day) %>%
summarise(Mean = mean(total_precip, na.rm = TRUE),
SEM = sd(total_precip, na.rm = TRUE)/sqrt(n())) %>%
mutate(Name = "Pel3")
Pel <- bind_rows(Pel2,Pel3)
ggplot(Pel, aes(x = as.numeric(day), y = Mean, color = Name))+
geom_ribbon(aes(ymin = Mean-SEM, ymax = Mean+SEM), alpha = 0.2)+
geom_line(size = 2)
EDIT: New graph based on update
To get the graph you post as a drawing, you need to have the day of the year and not the day of the month. We can get this information by setting a date sequence and extract the day of the year by using yday function from `lubridate package.
library(tidyverse)
library(lubridate)
Pelly2$Date = seq(ymd("1990-01-01"),ymd("2010-12-31"), by = "day")
Pelly2$Year_day <- yday(Pelly2$Date)
Pelly2_2011_3months$Date <- seq(ymd("2011-01-01"), ymd("2011-03-31"), by = "day")
Pelly2_2011_3months$Year_day <- yday(Pelly2_2011_3months$Date)
Pelly2$Dataset = "ALL"
Pelly2_2011_3months$Dataset = "2011_Dataset"
Pel <- bind_rows(Pelly2, Pelly2_2011_3months)
Then, you can combine both dataset and represent them with different colors, size, transparency (alpha) as show here:
ggplot(Pel, aes(x = Year_day, y = total_precip, color = year, size = Dataset, alpha = Dataset))+
geom_line()+
scale_size_manual(values = c(2,0.5))+
scale_alpha_manual(values = c(1,0.5))
Does it answer your question ?

Make a boxplot in highchart with a date object in the x axis

I am trying to make a boxplot in highchart to include it in a shiny app, along with another graph I already have.
The problem is that boxplot, as far as I can tell, do not behave like other plots and when you map a date to the x-axis, it is treated as a character string, this mean: the plot display the entire date ex: "2018-04-01" an not Apr'18 like it does in other plots.
Here I put a little reprex of what I have done
# Packages
library(tidyverse)
library(lubridate)
library(highcharter)
library(magrittr)
library(plotly)
# Data
stocks <- data.frame(
time = rep(as.Date('2009-01-01') + month(1:12), times = 10),
stock_price = rnorm(120, 0, 1)
)
# line plot
stocks %>%
group_by(time) %>%
summarise(mean_price = mean(stock_price)) %>%
hchart(.,
type = "line",
hcaes(x = "time",
y = "mean_price"))
# Box plot first try
# hchart boxplot
stocks %$%
hcboxplot(x = stock_price, time) %>%
hc_chart(type = "column")
After doing this first try, I try to create an abbreviated date and map it to the x-axis as follows, but the boxes are shown ordered alphabetically not chronologically
# hchart boxplot
stocks %>%
mutate(month = month(time, label = T),
year = str_extract(as.character(year(time)), "..$"),
time2 = paste(month, year, sep = "'")) %$%
hcboxplot(x = stock_price, time2) %>%
hc_chart(type = "column")
My desired output is a plot with x-axis like the line plot or like plotly's output
stocks %>%
group_by(time) %>%
plot_ly(x = ~time, y = ~stock_price, type = "box")
With the help of arrange() and fct_inorder(), I believe I've achieved your desired outcome:
stocks %>%
arrange(time) %>%
mutate(
month = month(time, label = T),
year = str_extract(as.character(year(time)), "..$"),
time2 = fct_inorder(paste(month, year, sep = "'"))
) %$%
hcboxplot(x = stock_price, time2) %>%
hc_chart(type = "column")

Resources