R ggplot2瀑布问题
R ggplot2 waterfall problems
嗨,我遇到了一个错误
Error: Discrete value supplied to continuous scale
当我在下面执行时。按照 https://www.jigsawacademy.com/waterfall-charts-using-ggplot2-in-r/.
上的简单 R 瀑布教程
想知道这是否过时了,我遗漏了一些简单的东西。
library(ggplot2)
library(dplyr)
balance <- data.frame(
desc = c(
"Starting Cash",
"Sales",
"Refunds",
"Payouts",
"Court Losses",
"Court Wins",
"Contracts",
"End Cash"
),
amount = c(2000, +3400, -1100, -100, -6600, 3800, 1400, 2800)
)
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "in","out")
balance[balance$desc %in% c("Starting Cash", "End Cash"),+"type"] <-
"net"
balance$end <- cumsum(balance$amount)
balance$end <- c(head(balance$end, -1), 0)
balance$start <- c(0, head(balance$end, -1))
balance <- balance[, c(3, 1, 4, 6, 5, 2)]
ggplot(balance, aes(desc, fill = type, x = desc)) + geom_rect(aes(
xmin = id - 0.45,
xmax = id + 0.45,
ymin = end,
ymax = start
))
Rstudio version 1.3.1073
R - version 4.0.2
ggplot2 - version 3.3.2
有什么想法吗?
谢谢
对您的代码稍作修改,我得到了这个:
library(ggplot2)
library(dplyr)
#Data
balance <- data.frame(
desc = c(
"Starting Cash",
"Sales",
"Refunds",
"Payouts",
"Court Losses",
"Court Wins",
"Contracts",
"End Cash"
),
amount = c(2000, +3400, -1100, -100, -6600, 3800, 1400, 2800),
stringsAsFactors = F
)
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "in","out")
balance[balance$desc %in% c("Starting Cash", "End Cash"),"type"] <- "net"
balance$end <- cumsum(balance$amount)
balance$end <- c(head(balance$end, -1), 0)
balance$start <- c(0, head(balance$end, -1))
balance <- balance[, c(3, 1, 4, 6, 5, 2)]
#Plot
ggplot(balance, aes(desc, fill = type)) + geom_rect(aes(
x = desc,
xmin = id - 0.45,
xmax = id + 0.45,
ymin = end,
ymax = start
))
输出:
我按照您在问题中包含的网页中的说明进行操作。
嗨,我遇到了一个错误
Error: Discrete value supplied to continuous scale
当我在下面执行时。按照 https://www.jigsawacademy.com/waterfall-charts-using-ggplot2-in-r/.
上的简单 R 瀑布教程想知道这是否过时了,我遗漏了一些简单的东西。
library(ggplot2)
library(dplyr)
balance <- data.frame(
desc = c(
"Starting Cash",
"Sales",
"Refunds",
"Payouts",
"Court Losses",
"Court Wins",
"Contracts",
"End Cash"
),
amount = c(2000, +3400, -1100, -100, -6600, 3800, 1400, 2800)
)
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "in","out")
balance[balance$desc %in% c("Starting Cash", "End Cash"),+"type"] <-
"net"
balance$end <- cumsum(balance$amount)
balance$end <- c(head(balance$end, -1), 0)
balance$start <- c(0, head(balance$end, -1))
balance <- balance[, c(3, 1, 4, 6, 5, 2)]
ggplot(balance, aes(desc, fill = type, x = desc)) + geom_rect(aes(
xmin = id - 0.45,
xmax = id + 0.45,
ymin = end,
ymax = start
))
Rstudio version 1.3.1073
R - version 4.0.2
ggplot2 - version 3.3.2
有什么想法吗?
谢谢
对您的代码稍作修改,我得到了这个:
library(ggplot2)
library(dplyr)
#Data
balance <- data.frame(
desc = c(
"Starting Cash",
"Sales",
"Refunds",
"Payouts",
"Court Losses",
"Court Wins",
"Contracts",
"End Cash"
),
amount = c(2000, +3400, -1100, -100, -6600, 3800, 1400, 2800),
stringsAsFactors = F
)
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "in","out")
balance[balance$desc %in% c("Starting Cash", "End Cash"),"type"] <- "net"
balance$end <- cumsum(balance$amount)
balance$end <- c(head(balance$end, -1), 0)
balance$start <- c(0, head(balance$end, -1))
balance <- balance[, c(3, 1, 4, 6, 5, 2)]
#Plot
ggplot(balance, aes(desc, fill = type)) + geom_rect(aes(
x = desc,
xmin = id - 0.45,
xmax = id + 0.45,
ymin = end,
ymax = start
))
输出:
我按照您在问题中包含的网页中的说明进行操作。