[R] Error: Discrete value supplied to continuous variable
King, Barry
k|ng @end|ng |rom but|er@edu
Mon Dec 28 18:03:29 CET 2020
I am attempting to convert an original schedule to longest operating time next schedule then create waterfall plot. I am having trouble with the plot. I get an Error: Discrete vale supplied to continuous variable message. My example code appears below.
library(tidyverse)
library(ggplot2)
# original schedule of four jobs
df <- data.frame(job=c("A","B","C","D"),
original_begin=c("2021-01-05 07:00:00", "2021-05-01 08:30:00",
"2021-05-01 10:30:00", "2021-05-01 14:00:00"),
original_end=c("2021-01-05 08:30:00", "2021-05-01 10:30:00",
"2021-05-01 14:00:00", "2021-05-01 16:00:00"))
# represent date/times as POSIXct objects
df$original_begin <- as.POSIXct(df$original_begin)
df$original_end <- as.POSIXct(df$original_end)
# calculate span, length of time each job takes
df$span <- as.numeric(difftime(df$original_end,df$original_begin))
# sort by span descending
df <- arrange(df,-span)
# assign ID now that df is correcly sorted
df$ID <- as.numeric(rownames(df))
# calculate ymin and ymax
df$ymin[1] <- min(df$original_begin)
for (i in 1:(nrow(df)-1)) {
df$ymax[i] <- df$ymin[i] + df$span[i]
df$ymin[i+1] <- df$ymax[i]
}
df$ymax[nrow(df)] <- df$ymin[nrow(df)] +
df$span[nrow(df)]
# following is loosely based on tutorial found at
# https://www.stomperusa.com/2019/05/27/basic-waterfall-graphs-in-r/
# set up plot canvas, longest job first (see x=reorder(job,-span))
g <-df %>%
ggplot(aes(x=reorder(job,-span), y=span, fill=factor(job))) +
theme_classic() +
theme(legend.title=element_blank())+
theme(legend.position = "right", panel.grid = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5)) +
labs(y = "Hours", x = "Job")
g # seems to be working as expected through here
w <- 0.5 # use to set width of bars
# attempt to create waterfall plot
g <- g +
geom_rect(aes(xmin = ID - w/2,
xmax = ID + w/2,
ymin = ymin,
ymax = ymax,
fill = factor(job)), alpha=0.25)
g
Any assistance is appreciated
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
[[alternative HTML version deleted]]
More information about the R-help
mailing list