【问题标题】:yticks rename and remove yaxis label in ggplot-Ryticks 重命名并删除 ggplot-R 中的 yaxis 标签
【发布时间】:2021-03-10 02:16:56
【问题描述】:

我正在尝试想象工作是否按计划执行。
但是,我找不到好的包,所以我决定改用 ggplot 的段。
但是,我找不到编辑 yaxis 详细标签的方法,所以我有几个问题。

No1.
我通过安排工作时间和完成时间来制作图表。
我已将 0.1 添加到 yaxis 以将它们移动,但它会创建一个不需要的轴标签。
有什么办法可以去掉标签 1.1,2.1,3.1,...?

No2.

我想使用 work_name 列而不是 1,2,3,....
有没有办法做到这一点?

No3.
我想将 xaxis 更改为显示“M/D”或“YMD”而不是“M D”格式,
有没有办法做到这一点?

我有这个数据集。

> 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")
)

和绘图代码


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))
  

【问题讨论】:

  • 我已经添加了数据。这是你的要求正确吗? (你能教我dput吗?我不知道)。谢谢你的评论!! @RonakShah
  • dput 用于共享数据,所以您所做的也是正确的。您可以在控制台中运行 dput(test) 并通过编辑您的帖子将输出复制到此处。如果您有很多行,您可以通过dput(head(test, 10)) 选择前 10 行或前 15 行。
  • dput 真的很有用!谢谢你!我总是自己打字。我总是很累。

标签: r ggplot2 tidyverse


【解决方案1】:

为了简化流程,我首先重新组织了数据

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

然后我绘制了它:

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

【讨论】:

  • 谢谢。我不知道更改更长的数据表!
猜你喜欢
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 2016-03-18
  • 2012-03-20
  • 2016-05-07
  • 2019-08-25
  • 2014-09-15
  • 2016-05-22
相关资源
最近更新 更多