Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 瀑布图与ggplot_R_Ggplot2 - Fatal编程技术网

R 瀑布图与ggplot

R 瀑布图与ggplot,r,ggplot2,R,Ggplot2,我正在尝试使用我在互联网上找到并改编的以下代码绘制瀑布图: waterfall <- function(balance){ balance$desc <- factor(balance$desc, levels = balance$desc) balance$id <- seq_along(balance$amount) balance$type <- ifelse(balance$amount > 0, "increase","decrease") balance

我正在尝试使用我在互联网上找到并改编的以下代码绘制瀑布图:

waterfall <- function(balance){
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "increase","decrease")
balance[balance$id %in% c(1,dim(balance)[1]),"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)]
balance
balance$type <- factor(balance$type, levels = c("decrease","increase", "net"))
p1 <- ggplot(balance, aes(desc, fill = type)) + 
geom_rect(aes(x = desc,xmin = id - 0.45, xmax = id + 0.45, ymin = end,ymax = start))+
xlab("") + 
ylab("") + 
geom_text(subset = .(type == "increase"), aes(id,end, label = comma(amount)), vjust = 1, size = 4,fontface="bold") +
geom_text(subset = .(type == "decrease"), aes(id,end, label = comma(amount)), vjust = -0.3,size = 4,fontface="bold")+
geom_text(data = subset(balance,type == "net" & id == min(id)), aes(id, end, label = comma(end), vjust = ifelse(end <start, 1, -0.3)), size = 4,fontface="bold") + 
geom_text(data = subset(balance,type == "net" & id == max(id)), aes(id, start, label = comma(start), vjust = ifelse(end < start, -0.3, 1)), size = 4,fontface="bold")+
theme_bw()+
theme(legend.position = "none")
return(p1)
}
我猜这与geom_文本中的subset参数有关,但我不知道如何处理子集为空的情况

我还有第二个问题。我对第一种情况下的三种颜色很满意。但是,如果我在第二个示例中运行代码,通过删除代码中的错误部分,我会得到一个只有两种颜色的图形,这两种颜色与前一种情况不同。是否可以像第一个示例中那样固定蓝色/红色/绿色等颜色


谢谢。

这确实是因为“增加”的子集是空的。避免此类空子集的简单方法是使用if语句:

waterfall <- function(balance){
  balance$desc <- factor(balance$desc, levels = balance$desc)
  balance$id <- seq_along(balance$amount)
  balance$type <- ifelse(balance$amount > 0, "increase","decrease")
  balance[balance$id %in% c(1,dim(balance)[1]),"type"] <- "net"
  balance$end <- cumsum(balance$amount)
  balance$end <- c(head(balance$end, -1), 0)
  balance$start <- c(0, head(balance$end, -1))
  balance$type <- factor(balance$type, levels = c("decrease","increase", "net"))
  p1 <- ggplot(balance, aes(desc, fill = type)) + 
    geom_rect(aes(x = desc,xmin = id - 0.45, xmax = id + 0.45, ymin = end,ymax = start))+
    xlab("") + 
    ylab("") + 
    geom_text(data = subset(balance,type == "net" & id == min(id)), aes(id, end, label = comma(end), vjust = ifelse(end <start, 1, -0.3)), size = 4,fontface="bold") + 
    geom_text(data = subset(balance,type == "net" & id == max(id)), aes(id, start, label = comma(start), vjust = ifelse(end < start, -0.3, 1)), size = 4,fontface="bold")+
    theme_bw()+
    theme(legend.position = "none")
  if ("increase" %in% balance$type){
    p1 <- p1 + geom_text(subset = .(type == "increase"), aes(id,end, label = comma(amount)), vjust = 1, size = 4,fontface="bold")
  }
  if ("decrease" %in% balance$type){
    p1 <- p1 + geom_text(subset = .(type == "decrease"), aes(id,end, label = comma(amount)), vjust = -0.3,size = 4,fontface="bold")
  }
  p1
}

请提供逗号函数或包,使其成为可独立复制的代码块。对此,我深表歉意。逗号函数来自“scales”包。谢谢。你确定吗?我在“天平”里找不到这个函数
balance <- data.frame(desc = c("Starting Cash", "Court Losses","Court Wins", "Contracts", "End Cash"), 
                  amount = c(2000, -1100, -100, -6600, 2800))
Error in if (nrow(layer_data) == 0) return() : argument is of length zero
waterfall <- function(balance){
  balance$desc <- factor(balance$desc, levels = balance$desc)
  balance$id <- seq_along(balance$amount)
  balance$type <- ifelse(balance$amount > 0, "increase","decrease")
  balance[balance$id %in% c(1,dim(balance)[1]),"type"] <- "net"
  balance$end <- cumsum(balance$amount)
  balance$end <- c(head(balance$end, -1), 0)
  balance$start <- c(0, head(balance$end, -1))
  balance$type <- factor(balance$type, levels = c("decrease","increase", "net"))
  p1 <- ggplot(balance, aes(desc, fill = type)) + 
    geom_rect(aes(x = desc,xmin = id - 0.45, xmax = id + 0.45, ymin = end,ymax = start))+
    xlab("") + 
    ylab("") + 
    geom_text(data = subset(balance,type == "net" & id == min(id)), aes(id, end, label = comma(end), vjust = ifelse(end <start, 1, -0.3)), size = 4,fontface="bold") + 
    geom_text(data = subset(balance,type == "net" & id == max(id)), aes(id, start, label = comma(start), vjust = ifelse(end < start, -0.3, 1)), size = 4,fontface="bold")+
    theme_bw()+
    theme(legend.position = "none")
  if ("increase" %in% balance$type){
    p1 <- p1 + geom_text(subset = .(type == "increase"), aes(id,end, label = comma(amount)), vjust = 1, size = 4,fontface="bold")
  }
  if ("decrease" %in% balance$type){
    p1 <- p1 + geom_text(subset = .(type == "decrease"), aes(id,end, label = comma(amount)), vjust = -0.3,size = 4,fontface="bold")
  }
  p1
}
scale_fill_manual(values = c(decrease = "indianred",increase ="forestgreen", net = "dodgerblue2"))