#I want to use gglot instead of qplot
plot_gantt <- qplot(ymin = start,
ymax = end,
x = shift,
colour = shift,
geom = "linerange",
data = activities,
size = I(20)) +
coord_flip() +
theme_bw() +
theme(panel.grid = element_blank()) +
xlab("") +
ylab("") +
ggtitle("Intreruption")
plot_gantt
Here are good learning resources for ggplot2 :
https://r4ds.hadley.nz/ [sections 1 and 9]
Theres a cheat sheet here for after :
Data visualization with ggplot2 :: Cheat Sheet
Enjoy learning, I think its a great skill to develop, and suprisingly easy to pick up once you get practicing.
I studied but i did not find solution.Dataframes names:
"shift" "start" "end"
You could try to provide a reprex. Dont forget activities also
FAQ: How to do a minimal reproducible example ( reprex ) for beginners
A handy way to supply data is to use the dput() function. Do dput(mydata) where "mydata" is the name of your dataset. For really large datasets probably dput(head(mydata, 100)) will do. Paste the output between
```
```
shift | start | end |
---|---|---|
1 | 23-12-2021 7:09 | 23-12-2021 7:10 |
1 | 23-12-2021 7:10 | 23-12-2021 7:10 |
1 | 23-12-2021 7:15 | 23-12-2021 7:15 |
1 | 23-12-2021 7:30 | 23-12-2021 7:37 |
1 | 23-12-2021 7:37 | 23-12-2021 7:40 |
1 | 23-12-2021 8:57 | 23-12-2021 9:00 |
1 | 23-12-2021 9:03 | 23-12-2021 9:04 |
1 | 23-12-2021 9:17 | 23-12-2021 9:18 |
1 | 23-12-2021 9:39 | 23-12-2021 9:39 |
1 | 23-12-2021 9:50 | 23-12-2021 9:51 |
1 | 23-12-2021 10:00 | 23-12-2021 10:30 |
1 | 23-12-2021 10:30 | 23-12-2021 10:32 |
1 | 23-12-2021 11:41 | 23-12-2021 11:42 |
1 | 23-12-2021 13:01 | 23-12-2021 13:02 |
1 | 23-12-2021 13:14 | 23-12-2021 13:16 |
1 | 23-12-2021 13:21 | 23-12-2021 13:21 |
1 | 23-12-2021 13:25 | 23-12-2021 13:27 |
1 | 23-12-2021 13:35 | 23-12-2021 13:35 |
1 | 23-12-2021 13:44 | 23-12-2021 13:44 |
1 | 23-12-2021 13:45 | 23-12-2021 13:45 |
1 | 23-12-2021 13:49 | 23-12-2021 13:55 |
2 | 23-12-2021 13:57 | 23-12-2021 14:08 |
2 | 23-12-2021 14:09 | 23-12-2021 14:12 |
2 | 23-12-2021 14:29 | 23-12-2021 14:29 |
2 | 23-12-2021 15:15 | 23-12-2021 15:16 |
2 | 23-12-2021 15:41 | 23-12-2021 15:42 |
2 | 23-12-2021 15:51 | 23-12-2021 15:53 |
2 | 23-12-2021 16:13 | 23-12-2021 16:13 |
2 | 23-12-2021 16:33 | 23-12-2021 16:34 |
2 | 23-12-2021 16:38 | 23-12-2021 16:38 |
2 | 23-12-2021 16:41 | 23-12-2021 16:43 |
2 | 23-12-2021 17:01 | 23-12-2021 17:02 |
2 | 23-12-2021 17:11 | 23-12-2021 17:11 |
2 | 23-12-2021 17:31 | 23-12-2021 17:33 |
2 | 23-12-2021 17:43 | 23-12-2021 17:44 |
2 | 23-12-2021 17:49 | 23-12-2021 17:49 |
2 | 23-12-2021 17:55 | 23-12-2021 18:25 |
2 | 23-12-2021 18:25 | 23-12-2021 18:28 |
2 | 23-12-2021 18:30 | 23-12-2021 18:52 |
2 | 23-12-2021 18:54 | 23-12-2021 19:07 |
2 | 23-12-2021 19:07 | 23-12-2021 19:07 |
2 | 23-12-2021 19:08 | 23-12-2021 19:14 |
2 | 23-12-2021 19:15 | 23-12-2021 19:20 |
2 | 23-12-2021 19:54 | 23-12-2021 19:56 |
2 | 23-12-2021 20:02 | 23-12-2021 20:03 |
2 | 23-12-2021 20:08 | 23-12-2021 20:08 |
2 | 23-12-2021 20:18 | 23-12-2021 20:20 |
2 | 23-12-2021 20:22 | 23-12-2021 20:26 |
2 | 23-12-2021 20:55 | 23-12-2021 21:00 |
2 | 23-12-2021 21:21 | 23-12-2021 21:24 |
2 | 23-12-2021 21:31 | 23-12-2021 21:31 |
2 | 23-12-2021 21:37 | 23-12-2021 21:37 |
2 | 23-12-2021 21:54 | 23-12-2021 21:55 |
2 | 23-12-2021 21:55 | 23-12-2021 21:57 |
library(ggplot2) | ||
library(lubridate) | ||
#> | ||
#> Attaching package: 'lubridate' | ||
#> The following objects are masked from 'package:base': | ||
#> | ||
#> date, intersect, setdiff, union | ||
library(readr) | ||
activities <- read_csv("d://temp/Proiecte R/stati.csv") | ||
str(activities) | ||
names(activities) |
head(activities)
#> Rows: 54 Columns: 3
#> -- Column specification --------------------------------------------------------
#> Delimiter: ","
#> chr (2): start, end
#> dbl (1): shift
#>
#> i Use spec()
to retrieve the full column specification for this data.
#> i Specify the column types or set show_col_types = FALSE
to quiet this message.
activities$start <- dmy_hm(activities$start)
activities$end <- dmy_hm(activities$end)
activities$shift <- factor(activities$shift)
#levels = activities$shift[nrow(activities):1])
str(activities)
###########
plot_gantt <- qplot(ymin = start,
ymax = end,
x = shift,
colour = shift,
geom = "linerange",
data = activities,
size = I(20)) +
coord_flip() +
theme_bw() +
theme(panel.grid = element_blank()) +
xlab("") +
ylab("") +
ggtitle("Intreruption")
plot_gantt
Try this. I've changed the name of your dataset to DT
to cut down on typing.
ggplot(DT, aes( x = shift, ymin = start, ymax = end,
colour = shift, linewidth = I(20))) +
geom_linerange() +
coord_flip() + theme_bw() +
theme(panel.grid = element_blank()) +
xlab("") + ylab("") +
ggtitle("Intreruption")
How to format yaxis?maybe time format
Maybe this:
ggplot(DT, aes( x = shift, ymin = start, ymax = end,
colour = shift, linewidth = I(20))) +
geom_linerange() +
coord_flip() + theme_bw() +
theme(panel.grid = element_blank()) +
xlab("") + ylab("") + scale_y_datetime()
ggtitle("Intreruption")
BTW, I think "Intreruption" is spelt incorrectly
OOPs forgot. You need to load the {scales} library.
My complete code would be
suppressMessages(library(data.table))
suppressMessages(library(tidyverse))
library(scales)
DT <- fread("raw_data.csv")
DT[, c("start", "end", "shift") := .(dmy_hm(start), dmy_hm(end), as.factor(shift) )]
ggplot(DT, aes( x = shift, ymin = start, ymax = end,
colour = shift, linewidth = I(20))) +
geom_linerange() +
coord_flip() + theme_bw() +
theme(panel.grid = element_blank()) +
xlab("") + ylab("") + scale_y_datetime()
ggtitle("Intreruption")