从 R 中的函数调用对象 - 找不到错误对象

时间:2021-03-03 16:25:11

标签: r object

我正在尝试创建一个可变宽度的柱形图,每一列代表一个非重叠的步骤。宽度将是步骤的平均值,高度是每个步骤的标准偏差。这是我的原始代码:

df <- data.frame (Steps = c( "Step2", "Step3", "Step4", "Step5", "Step6", "Step7"), Variability = c(sd(dataset$PS02_days_creation_to_validation, na.rm = TRUE), sd(dataset$PS03_days_validation_to_sourcing, na.rm = TRUE), sd(dataset$PS04_days_sourcing_to_confirmation, na.rm = TRUE), sd(dataset$PS05_days_confirmation_to_ship_created, na.rm = TRUE),sd(dataset$PS06_days_ship_created_to_shipped, na.rm = TRUE), sd(dataset$PS07_days_shipped_to_reception,na.rm = TRUE) ), width = c(mean(dataset$PS02_days_creation_to_validation, na.rm = TRUE), mean(dataset$PS03_days_validation_to_sourcing, na.rm = TRUE), mean(dataset$PS04_days_sourcing_to_confirmation, na.rm = TRUE), mean(dataset$PS05_days_confirmation_to_ship_created,na.rm = TRUE), mean(dataset$PS06_days_ship_created_to_shipped,na.rm = TRUE), mean(dataset$PS07_days_shipped_to_reception,na.rm = TRUE) ))

df$w <- cumsum(df$width) #cumulative sums to gt the end points of each column.
df$wm <- df$w - df$width #giving the starting point of each column

library(ggplot2)
p  <- ggplot(df, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = Variability, fill = Steps))
p1

当所有步骤都为正时,此代码工作正常。但是,当第一步的宽度为负数时,我的列会重叠。
即具有以下宽度 =(25,50,75,100) 的 4 个步骤,使用上述代码创建:终点 w= (25,75,150,250) 起点 wm= (0,25,75,150)。太棒了!

现在,如果第一步的宽度更改为 (-25,50,75,100) 则代码生成 w:(-25,25,100,200) 和 wm:(0,-25,25,100) 这意味着步骤重叠!!相反,我想修改代码以生成:w=(-25,50,125,225) 和 wm:(0,0,50,125)

一位 R 大师善意地建议使用此功能:

widths_to_starts_ends <- function(widths){
  
  widths <- widths[widths != 0]
  pos_widths <- widths[widths > 0]
  neg_widths <- widths[widths < 0]
  
  pos_ends <- cumsum(pos_widths)
  pos_starts <- cumsum(pos_widths) - pos_widths
  
  neg_widths <- abs(neg_widths)
  neg_starts <- cumsum(neg_widths)
  neg_ends <- cumsum(neg_widths) - neg_widths
  
  neg_starts <- (-1)*neg_starts
  neg_ends <- (-1)*neg_ends
  
  return( list(s = c(neg_ends, pos_starts), e = c(neg_starts, pos_ends)) )
  
}

widths_to_starts_ends(c(-25,50,75,100))

结果

$s 
[1] 0   0  50 125

$e
[1] -25  50 125 225 

我想将此函数嵌入到我的原始代码中(用 e 和 s 替换 w 和 wm 行)并制作图形,但我不确定如何调用对象 E 和 S。这就是我在做什么:

library(ggplot2)
p  <- ggplot(df, aes(ymin = 0))

p1 <- p + geom_rect(aes(xmin = s, xmax = e, ymax = Variability, fill = Steps))
p1

但它说找不到对象 S。你能帮我吗?

2 个答案:

答案 0 :(得分:1)

s 和 e 似乎是列表的元素,而不是独立的对象,我想这就是为什么找不到它们的原因。在您的函数中,您可以改为返回一个包含 s 和 e 列的数据框 df,然后进行 ggplot 调用

ggplot(df, aes(xmin = s, xmax = e...)) + geom_rect()

答案 1 :(得分:0)

如果我理解正确的话,您想制作一系列连续的间隔,如果列表的第一个值为负,则这些间隔会移动。

这里有一些比您已经生成的间隔并将它们存储在数据帧中的方法更紧凑:

df$e <- cumsum(abs(df$width)) + min(0, df$width[1])
df$s <- c(min(0, df$width[1]), df$e[-length(df$e)])
df$s #starting points
#[1] -25   0  50 125
df$e #ending points
#[1]   0  50 125 225

然后,将获得相应的情节:

p  <- ggplot(df, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = s, xmax = e, ymax = Variability, fill = Steps))
p1

enter image description here

我使用的数据:

set.seed(123456789)
df <- data.frame (Steps = c( "Step2", 
                             "Step3", 
                             "Step4", 
                             "Step5"), 
                  Variability = c(sd(1:500/sample(1:10, 1)), 
                                  sd(1:500/sample(1:10, 1)), 
                                  sd(1:500/sample(1:10, 1)), 
                                  sd(1:500/sample(1:10, 1))), 
                  width = c(-25,50,75,100))
相关问题