R 图中缺少错误条

R 图中缺少错误条,r,ggplot2,shiny,R,Ggplot2,Shiny,我的闪亮应用程序中有一个互动情节。在这个图中,我可以将数据点标记为人工制品。部分数据绘制为折线图,部分数据绘制为误差条 我使用以下ggplot代码: ggplot(plotdat, aes(x = time, y = value, color = type)) + labs(title = "vitals from test") + geom_errorbar(data = nibpdat, aes(x = time,

我的闪亮应用程序中有一个互动情节。在这个图中,我可以将数据点标记为人工制品。部分数据绘制为折线图,部分数据绘制为误差条

我使用以下
ggplot
代码:

ggplot(plotdat,
       aes(x = time, y = value, color = type)) +
  labs(title = "vitals from test") +
  geom_errorbar(data = nibpdat, 
                aes(x = time, 
                    ymin = dianibp, 
                    ymax = sysnibp), 
                position = position_dodge(.1)) +
  scale_color_manual(values = vitalpalette) +
  geom_point() +
  geom_line(data = plotdat %>% filter(!grepl("NIBP$", type))) +
  geom_point(data = plotdat %>% filter(artefact),mapping = aes(x = time, y = value, color = type),
             shape = 4, size = 2, stroke = 2) +
  theme_bw()
当我在闪亮的应用程序外测试这个绘图时,它可以工作。所有错误条都保持可见。但是在闪亮的应用程序中,如果标记了
nibpdat
中的一个点(列
artifact
),则不会绘制错误条

这是标准图(模拟标记点)

这是用同样的代码制作的图,当错误条的几个点被标记时

用户界面


此示例应用程序中出现以下问题:

单击点时,
artifact$status
中的值
artifact
将从TRUE更改为FALSE

在下面的一段代码中,数据是分散的,但由于现在三个值中的一个对字段
artifact
具有不同的值,因此生成了两个单独的行。因此,使用
geom_errorbar()
至少缺少一个美学(y、ymax或ymin)

nibpdat%过滤器(grepl(“nibp$”,类型))%>%
排列(类型、值)%>%
突变(类型=因子(匹配(“平均NIBP”,vitaltypes$字段),
级别=序号(nrow(生命类型)),
标签=生命类型$label),
值=平均NIBP,
人工制品(假)
应改为:

nibpdat <- plotvitals %>% filter(grepl("nibp$",type)) %>% 
  select(-artefact) %>%
  spread(type, plotvalue) %>%
  mutate(type = factor(match("meannibp", vitaltypes$field), 
                       levels = seq_len(nrow(vitaltypes)), 
                       labels = vitaltypes$label),
         plotvalue = meannibp,
         artefact = FALSE)
nibpdat%过滤器(grepl(“nibp$”,类型))%>%
选择(-artifact)%>%
排列(类型、打印值)%>%
突变(类型=因子(匹配(“平均NIBP”,vitaltypes$字段),
级别=序号(nrow(生命类型)),
标签=生命类型$label),
plotvalue=meanibp,
人工制品(假)

您是否能够共享数据plotdat和nibpdat?不完全是:-)。我希望有人能在没有数据的情况下看到这个问题。@Wietze314您能共享您的操作系统吗?R Version
sessionInfo()
?我在代码中发现了这个问题。请看我的答案。@Wietze314听到机器问题很抱歉,但为解决这个问题“万岁”。
shinyServer(function(input, output) {

  vitals <- reactive({

    structure(list(time = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
                            14, 15, 16, 17, 18, 19, 20, 21, 22, 3, 4, 5, 6, 7, 8, 9, 10, 
                            11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 4, 7, 10, 12, 
                            14, 16, 18, 21, 22, 23, 25, 26, 27, 29, 30, 31, 32, 34, 35, 36, 
                            4, 7, 10, 12, 14, 16, 18, 21, 22, 23, 25, 26, 27, 29, 30, 31, 
                            32, 34, 35, 36, 4, 7, 10, 12, 14, 16, 18, 21, 22, 23, 25, 26, 
                            27, 29, 30, 31, 32, 34, 35, 36), 
                   type = c("heartrate", "heartrate", 
                            "heartrate", "heartrate", "heartrate", "heartrate", "heartrate", 
                            "heartrate", "heartrate", "heartrate", "heartrate", "heartrate", 
                            "heartrate", "heartrate", "heartrate", "heartrate", "heartrate", 
                            "heartrate", "heartrate", "heartrate", "saturation", "saturation", 
                            "saturation", "saturation", "saturation", "saturation", "saturation", 
                            "saturation", "saturation", "saturation", "saturation", "saturation", 
                            "saturation", "saturation", "saturation", "saturation", "saturation", 
                            "saturation", "saturation", "saturation", "sysnibp", "sysnibp", 
                            "sysnibp", "sysnibp", "sysnibp", "sysnibp", "sysnibp", "sysnibp", 
                            "sysnibp", "sysnibp", "sysnibp", "sysnibp", "sysnibp", "sysnibp", 
                            "sysnibp", "sysnibp", "sysnibp", "sysnibp", "sysnibp", "sysnibp", 
                            "meannibp", "meannibp", "meannibp", "meannibp", "meannibp", "meannibp", 
                            "meannibp", "meannibp", "meannibp", "meannibp", "meannibp", "meannibp", 
                            "meannibp", "meannibp", "meannibp", "meannibp", "meannibp", "meannibp", 
                            "meannibp", "meannibp", "dianibp", "dianibp", "dianibp", "dianibp", 
                            "dianibp", "dianibp", "dianibp", "dianibp", "dianibp", "dianibp", 
                            "dianibp", "dianibp", "dianibp", "dianibp", "dianibp", "dianibp", 
                            "dianibp", "dianibp", "dianibp", "dianibp"), 
                   value = c(97, 101, 
                             92, 95, 85, 93, 87, 87, 87, 92, 93, 90, 88, 83, 82, 72, 68, 62, 
                             66, 83, 98.3, 98, 98.3, 98, 98.9, 98.5, 99.8, 99.2, 99, 99.4, 
                             98.8, 98.7, 99, 94.7, 98, 98.5, 95.9, 98.1, 99.1, 98.2, 142, 
                             132, 126, 128, 136, 107, 107, 108, 121, 87, 102, 107, 100, 112, 
                             115, 114, 110, 102, 103, 105, 93, 86, 86, 86, 70, 70, 82, 76, 
                             76, 51, 57, 62, 66, 63, 70, 75, 65, 64, 71, 65, 71, 64, 72, 74, 
                             57, 55, 74, 61, 59, 32, 31, 55, 50, 47, 48, 58, 48, 48, 61, 50
                   ), case = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 
              class = c("tbl_df", 
                        "tbl", "data.frame"), .Names = c("time", "type", "value", "case"
                        ), row.names = c(NA, -100L))
  }) 

  observe({
    n <- nrow(vitals())
    artefacts$numberofvitals <- n
    artefacts$status <- rep(FALSE,n)
  })

  artefacts <- reactiveValues(
    numberofvitals = 1,
    status = rep(FALSE, 1)
  )

  observeEvent(input$VitalsPlot_click, {
    res <- nearPoints(vitals(), input$VitalsPlot_click, allRows = TRUE)[1:artefacts$numberofvitals,]

    artefacts$status <- xor(artefacts$status, res$selected_)
  })

  output$VitalsPlot <- renderPlot({
    plotvitals <- vitals()
    plotvitals$artefact <- artefacts$status

    plotdat <- plotvitals %>% mutate(type = factor(match(type, vitaltypes$field), 
                                                   levels = seq_len(nrow(vitaltypes)), 
                                                   labels = vitaltypes$label))

    nibpdat <- plotvitals %>% filter(grepl("nibp$",type)) %>%
      spread(type, value) %>%
      mutate(type = factor(match("meannibp", vitaltypes$field), 
                           levels = seq_len(nrow(vitaltypes)), 
                           labels = vitaltypes$label),
             value = meannibp,
             artefact = FALSE)

    plotid <- "test"

    ggplot(plotdat,
           aes(x = time, y = value, color = type)) +
      labs(title = paste0("vitals from ",plotid)) +
      geom_errorbar(data = nibpdat, 
                    aes(x = time, 
                        ymin = dianibp, 
                        ymax = sysnibp), 
                    position = position_dodge(.1)) +
      scale_color_manual(values = vitalpalette) +
      geom_point() +
      geom_line(data = plotdat %>% filter(!grepl("NIBP$", type))) +
      geom_point(data = plotdat %>% filter(artefact),mapping = aes(x = time, y = value, color = type),
                 shape = 4, size = 2, stroke = 2) +
      theme_bw()

  })

  output$artefacts <- renderTable({
    vitals()[artefacts$status,] %>%
      arrange(type, time) %>%
      group_by(type) %>%
      mutate(vital = if_else(row_number()==1,unlist(vitaltypes[match(type, vitaltypes$field),"label"]),""),
             time = floor(time)) %>%
      ungroup() %>%
      select(vital, time, value)
  })
})
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
  [1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C                       LC_TIME=Dutch_Netherlands.1252    

attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
  [1] compiler_3.4.1 tools_3.4.1   
nibpdat <- plotvitals %>% filter(grepl("nibp$",type)) %>%
  spread(type, value) %>%
  mutate(type = factor(match("meannibp", vitaltypes$field), 
                       levels = seq_len(nrow(vitaltypes)), 
                       labels = vitaltypes$label),
         value = meannibp,
         artefact = FALSE)
nibpdat <- plotvitals %>% filter(grepl("nibp$",type)) %>% 
  select(-artefact) %>%
  spread(type, plotvalue) %>%
  mutate(type = factor(match("meannibp", vitaltypes$field), 
                       levels = seq_len(nrow(vitaltypes)), 
                       labels = vitaltypes$label),
         plotvalue = meannibp,
         artefact = FALSE)