繁体   English   中英

带R的简单甘特图(绘制多条线)

[英]Simple Gantt chart with R (plotting multiple lines)

我想用RShiny和ggplot2创建一个非常简单的甘特图。 我不想像这里使用软件包或预定义的甘特图。 我宁愿通过在一条线之上,另一条线并平行地绘制多条线来创建图表。 这应该不是很困难,但是我对图表后面的数据框感到困难。

我有一个非常简单的数据框,例如:

test_df_1 <- data.frame("x_1" = c(1,2,3),
                        "x_2" = c(2,4,5),
                         "y" = c(1,2,3),
                         "group" = c("A","B","C"))

对于y = 1,线应从1到2,对于y = 2,线应从2到4,依此类推。使用这些代码行,我得到一个空图(但没有错误消息):

  output$test <- renderPlot({
       df_test <- data.frame(x_1=unlist(test_df_1$x_1), x_2=unlist(test_df_1$x_2), 
                             y=unlist(test_df_1$y), group=unlist(test_df_1$group))

       ggplot(data=df_test, aes(x=x_1, y=y, group=y)) +
          geom_line() +
          theme_bw()
       })

我可以肯定的事实是,我还没有将x_2“导入”到ggplot中。 但是我不知道该怎么做。

当我以略有不同的顺序尝试数据帧时(我实际上不希望这样):

test_df_2 <- data.frame("x_1" = c(1,2,2,4,3,5),
                        "y" = c(1,1,2,2,3,3),
                         "group" = c("A","","B","","C",""))

并绘制:

  output$test <- renderPlot({
       df_test <- data.frame(x_1=unlist(test_df_2$x_1), 
                             y=unlist(test_df_2$y), group=unlist(test_df_2$group))

       ggplot(data=df_test, aes(x=x_1, y=y, group=y)) +
          geom_line() +
          theme_bw()
       })

我得到了预期的结果

如何获得第一个数据帧(test_df_1)的结构所需的多线图?

ggplot(data=df_test) +
  geom_linerange(aes(x = y, ymin = x_1, ymax = x_2)) +
  coord_flip()+
  theme_bw()

或不翻转:

ggplot(df_test, aes(x = x_1, y = y, group = group)) + 
  geom_segment(aes(xend = x_2, yend = y))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM