带有 R 的简单甘特图(绘制多条线)
Simple Gantt chart with R (plotting multiple lines)
我想用 RShiny 和 ggplot2 创建一个非常简单的甘特图。我不想使用包或预定义的甘特图 like here。我宁愿通过绘制一条在另一条上方且全部平行的多条线来创建图表。应该不难,就是图表后面的数据框有点问题。
我有一个非常简单的数据框,例如:
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()
})
我确定我没有 "imported" 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()
})
我得到了预期的 result。
如何使用第一个数据框 (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))
我想用 RShiny 和 ggplot2 创建一个非常简单的甘特图。我不想使用包或预定义的甘特图 like here。我宁愿通过绘制一条在另一条上方且全部平行的多条线来创建图表。应该不难,就是图表后面的数据框有点问题。
我有一个非常简单的数据框,例如:
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()
})
我确定我没有 "imported" 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()
})
我得到了预期的 result。
如何使用第一个数据框 (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))