I want to create a very simple Gantt chart with RShiny and ggplot2. I don't want to use packages or predefined Gantt charts like here. I rather want create the chart by plotting multiple lines one above the other and all parallel. It shouldn't be very difficult, but I have trouble with the data frame behind the chart.
I have a very simple data frame e.g.:
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"))
The lines should be from 1 to 2 for y = 1, from 2 to 4 for y = 2, etc. With these lines of code I get an empty plot (but no error message):
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()
})
I'm sure about the fact, that I have not "imported" x_2 into ggplot. But I don't know how to do it.
When I try the data frame in a slightly different order (which I actually don't want):
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",""))
And plot it:
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()
})
I get the expected result.
How do I get the desired multiple line plot with the structure of the first data frame (test_df_1 )?
ggplot(data=df_test) +
geom_linerange(aes(x = y, ymin = x_1, ymax = x_2)) +
coord_flip()+
theme_bw()
or without flipping:
ggplot(df_test, aes(x = x_1, y = y, group = group)) +
geom_segment(aes(xend = x_2, yend = y))