TAGS :Viewed: 14 - Published at: a few seconds ago

[ dot function in waterfall plots with ggplot ]

I'm trying to reproduce this example: http://www.r-bloggers.com/ggplot2-waterfall-charts/

And it should give this result: enter image description here

And this is my code:

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
)

But it gives me an error:

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

I looked for the package with "dot function" with no result. Maybe an updated package has it with a different name?

Answer 1


Simply following @lukeA and @Heroka's directions to get the question closed (code below)

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
)