与ggplot的瀑布图中的点函数

时间:2015-08-04 11:51:18

标签: r ggplot2

我正在尝试重现此示例:http://www.r-bloggers.com/ggplot2-waterfall-charts/

它应该给出这个结果: enter image description here

这是我的代码:

library(ggplot2)
library(scales)

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)) + geom_rect(aes(x = desc,xmin = id - 0.45, xmax = id + 0.45, ymin = end,
        ymax = start))

balance$type <- factor(balance$type, levels = c("out", "in", "net"))
 strwr <- function(str) gsub(" ", "\n", str)

(p1 <- ggplot(balance, aes(fill = type)) + geom_rect(aes(x = desc, xmin = id - 0.45, xmax = id + 0.45, ymin = end,
 ymax = start)) + scale_y_continuous("",
labels  = percent_format()) +
 scale_x_discrete("", breaks = 
levels(balance$desc),
 labels = strwr(levels(balance$desc))) +
theme(legend.position = "none"))

p1 + geom_text(subset = .(type == "in"), aes(id,
     end, label = comma(amount)), vjust = 1, size = 3) +
     geom_text(subset = .(type == "out"), aes(id,
         end, label = comma(amount)), vjust = -0.3,
         size = 3) + geom_text(data = subset(balance,
     type == "net" & id == min(id)), aes(id, end,
     colour = type, label = comma(end), vjust = ifelse(end <
         start, 1, -0.3)), size = 3.5) + geom_text(data = subset(balance,
     type == "net" & id == max(id)), aes(id, start,
     colour = type, label = comma(start), vjust = ifelse(end <
             start, -0.3, 1)), size = 3.5
)

但它给了我一个错误:

Error in do.call("layer", list(mapping = mapping, data = data, stat = stat,  : 
  could not find function "."

我找了一个没有结果的“点功能”包。也许更新的包装有不同的名称?

1 个答案:

答案 0 :(得分:0)

只需关注@lukeA和@Heroka的指示即可解决问题(代码如下)

jkdsj

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
# install.packages("plyr", dependencies = TRUE)
library(plyr)
# install.packages("scales", dependencies = TRUE)
library(scales)

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)) + geom_rect(aes(x = desc,xmin = id - 0.45, xmax = id + 0.45, ymin = end,
        ymax = start))

balance$type <- factor(balance$type, levels = c("out", "in", "net"))
 strwr <- function(str) gsub(" ", "\n", str)

(p1 <- ggplot(balance, aes(fill = type)) + geom_rect(aes(x = desc, xmin = id - 0.45, xmax = id + 0.45, ymin = end,
 ymax = start)) + scale_y_continuous("",
labels  = percent_format()) +
 scale_x_discrete("", breaks = 
levels(balance$desc),
 labels = strwr(levels(balance$desc))) +
theme(legend.position = "none"))

p1 + geom_text(data = subset(balance, type == "in"), aes(id,
     end, label = comma(amount)), vjust = 1, size = 3) +
     geom_text(data = subset(balance, type == "out"), aes(id,
         end, label = comma(amount)), vjust = -0.3,
         size = 3) + geom_text(data = subset(balance,
     type == "net" & id == min(id)), aes(id, end,
     colour = type, label = comma(end), vjust = ifelse(end <
         start, 1, -0.3)), size = 3.5) + geom_text(data = subset(balance,
     type == "net" & id == max(id)), aes(id, start,
     colour = type, label = comma(start), vjust = ifelse(end <
             start, -0.3, 1)), size = 3.5
)