King, Barry
2020-Dec-28 17:03 UTC
[R] Error: Discrete value supplied to continuous variable
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]]
jim holtman
2020-Dec-29 02:21 UTC
[R] Error: Discrete value supplied to continuous variable
You setup your X & Y axis incorrectly. in your call to ggplot you have:
g <-df %>%
ggplot(aes(x=reorder(job,-span), y=span, fill=factor(job))) +
but in your call to geom_rect you are using a completely different set
of variables that is causing the error:
geom_rect(aes(xmin = ID - w/2,
xmax = ID + w/2,
ymin = ymin,
ymax = ymax,
fill = factor(job)), alpha=0.25)
I changed the call to ggplot to at least have the same variables types
and got a plot out of it; is this what you were expecting:
ggplot(aes(x=ID, y=ymin, fill=factor(job))) +
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Mon, Dec 28, 2020 at 4:33 PM King, Barry <king at butler.edu>
wrote:>
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.