h-y-jp
h-y-jp

Reputation: 339

yticks rename and remove yaxis label in ggplot-R

I am trying to visualize whether the work was executed as scheduled or not.
However, I couldn't find a good package, so I decided to use ggplot's segment instead.
However, I can't find a way to edit the detailed labels of yaxis, so I have a few questions.

No1.
I made a diagram by shifting when work is scheduled and when it is done.
I've added 0.1 to the yaxis to shift them around, but it creates an unwanted axis label.
Is there any way to remove the labels 1.1,2.1,3.1,...?

No2.

I want to use the work_name column instead of 1,2,3,....
Is there any way to do this?

No3.
I want to change the xaxis to display "M/D" or "YMD" instead of "M D" format,
is there any way to do this?

I have this dataset.

> test$work_name
[1] "work-11" "work-12" "work-13" "work-14" "work-15"
> test$work_order_scedule
[1] 1 2 3 4 5
> test$scedule
[1] "2019-10-10 06:00:01 UTC" "2019-10-10 14:00:03 UTC" "2019-10-10 14:00:03 UTC"
[4] "2019-10-10 14:00:03 UTC" "2019-10-10 14:00:03 UTC"
> test$scedule_end
[1] "2019-10-10 14:00:02 UTC" "2019-10-10 22:00:00 UTC" "2019-10-10 22:00:00 UTC"
[4] "2019-10-10 22:00:00 UTC" "2019-10-10 22:00:00 UTC"
> test$do
[1] "2019-10-10 06:00:01 UTC" "2019-10-10 14:00:03 UTC" "2019-10-10 14:00:03 UTC"
[4] "2019-10-10 14:00:03 UTC" "2019-10-10 14:00:03 UTC"
> test$do_end
[1] "2019-10-11 20:50:08 UTC" "2019-10-11 20:49:57 UTC" "2019-10-11 20:49:54 UTC"
[4] "2019-10-11 20:49:52 UTC" "2019-10-11 20:49:50 UTC"
test<- data.frame(work_name=c("work-11","work-12","work-13","work-14","work-15"),
           work_order_scedule=c(1 ,2 ,3 ,4 ,5),
           scedule =c("2019-10-10 06:00:01" ,"2019-10-10 14:00:03", "2019-10-10 14:00:03","2019-10-10 14:00:03","2019-10-10 14:00:03"),
           scedule_end=c("2019-10-10 14:00:02", "2019-10-10 22:00:00" ,"2019-10-10 22:00:00","2019-10-10 22:00:00" ,"2019-10-10 22:00:00"),
           do=c("2019-10-10 06:00:01", "2019-10-10 14:00:03", "2019-10-10 14:00:03","2019-10-10 14:00:03" ,"2019-10-10 14:00:03"),
           do_end=c("2019-10-11 20:50:08", "2019-10-11 20:49:57" ,"2019-10-11 20:49:54","2019-10-11 20:49:52" ,"2019-10-11 20:49:50")
)

and plot code


test %>% 
ggplot() +
  geom_segment(
    aes(y=reorder(work_order_scedule,scedule), yend=reorder(work_order_scedule,scedule),
        x=scedule, xend=scedule_end), 
    color="blue", 
    size=0.1)+ 
  theme(axis.text.x = element_text(angle=90, hjust=1))+
  
  geom_segment(
    aes(y=reorder(work_order_scedule+0.1,scedule), yend=reorder(work_order_scedule+0.1,scedule),
        x=do, xend=do_end), 
    color="black", 
    size=0.1)+ 
  theme(axis.text.x = element_text(angle=90, hjust=1))
  

enter image description here

Upvotes: 0

Views: 172

Answers (1)

pavlopavlin
pavlopavlin

Reputation: 120

To simplify the process I first reorganised the data

test2 <-
full_join(test%>%transmute(work_name, work_order_scedule, what = "scedule", 
                           start = as.POSIXct(scedule), end = as.POSIXct(scedule_end)),
          test%>%transmute(work_name, work_order_scedule, what = "do", 
                           start = as.POSIXct(do), end = as.POSIXct(do_end))) %>%
  arrange(start)

test2
##    work_name work_order_scedule    what               start                 end
## 1    work-11                  1 scedule 2019-10-10 06:00:01 2019-10-10 14:00:02
## 2    work-11                  1      do 2019-10-10 06:00:01 2019-10-11 20:50:08
## 3    work-12                  2 scedule 2019-10-10 14:00:03 2019-10-10 22:00:00
## 4    work-13                  3 scedule 2019-10-10 14:00:03 2019-10-10 22:00:00
## 5    work-14                  4 scedule 2019-10-10 14:00:03 2019-10-10 22:00:00
## 6    work-15                  5 scedule 2019-10-10 14:00:03 2019-10-10 22:00:00
## 7    work-12                  2      do 2019-10-10 14:00:03 2019-10-11 20:49:57
## 8    work-13                  3      do 2019-10-10 14:00:03 2019-10-11 20:49:54
## 9    work-14                  4      do 2019-10-10 14:00:03 2019-10-11 20:49:52
## 10   work-15                  5      do 2019-10-10 14:00:03 2019-10-11 20:49:50

Then I plotted it:

ggplot(mapping = aes(x = start, xend = end, 
                     y = work_order_scedule, yend = work_order_scedule, 
                     color = what)) +
  geom_segment(data = test2%>%filter(what == "scedule"), 
               position = position_nudge(y = -0.1), #move down by 0.1
               size = 2) +
  geom_segment(data = test2%>%filter(what == "do"), 
               position = position_nudge(y = 0.1), #move up y 0.1
               size = 2) +
  scale_x_datetime(name = "Schedule", # rename x axis
                   date_breaks = "days", # daily time-step
                   date_labels = "%Y-%m-%d") + #time format of the axis label
  scale_y_continuous(name = NULL, # remove y title
                     breaks = unique(as.integer(test2$work_order_scedule)), 
                     labels = unique(test2$work_name)) + #replace 1:5 by the work name
  scale_color_discrete(name = NULL) + #remove legend name
  theme(legend.position = "top") #move the legend to the top

enter image description here

Upvotes: 1

Related Questions