使用带有重复Y因子的ggplot在基于geom_segment的甘特图中的排序条 [英] Ordering bars in geom_segment based gantt chart using ggplot with duplicate y-factors

查看:60
本文介绍了使用带有重复Y因子的ggplot在基于geom_segment的甘特图中的排序条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我有一个数据集, se.df (问题底部的数据),我通过使用 ggplot facet_grid 可视化为分解甘特图。但是,y标签没有按照我指定的 aes

  library(ggplot2)
base<-ggplot(
se.df,
aes(
x = Start.Date,reorder(Action,Start.Date),color = Comms.Type
))
base + geom_segment(aes(
xend = End.Date,ystart = Action,yend = Action
),size = 5)+
facet_grid(来源〜。,scale = free_y,space = free_y,drop = TRUE)

在此详细图像中,您可以看到其中的条形图:


  1. 开始中未显示。日期订单

  2. 操作未排序。为了明确起见,条形图应按Start.Date排序,然后按 Action



如何根据开始在每个因子中对条形进行排序日期,然后按操作



更新



@heathobrien提供了一个解决方案,解决了我通过 Start.Date 订购酒吧的问题,而不是由重复因素引起的问题-



在<$ c中有两个通知大学 的实例。 $ c>动作,会导致以下@heathobrien代码混乱,在图像中以红色椭圆形突出显示:

  se.df< -se.df [order(se.df $ Start.Date,se.df $ Action),] 
se.df $ Action< ;-factor(se.df $ Action,level = unique(se.df $ Action))
ggplot(se.df,aes(x = Start.Date,color = Comms.Type))+
geom_segment(aes(xend = End.Date,y = Action,yend = Action),size = 5)+
facet_grid(Source〜。,scale = free_y,space = free_y,drop = TRUE )



如何将此data.frame提供给 ggplot ,以使每个 facet_grid



更多详细信息



制作甘特有很多问题图表和排序因素,我已经根据其他答案做出了一些决定:


  1. geom_segment

许多提问者都使用了 geom_linerange ,但由于无法使用



我必须创建一个合成的 taskID 来传递顺序(通过增加Start.Date,通过Action增加alpha)。顺便说一句,如果要按操作按字母顺序排序,则需要更改因子的顺序或转换为字符。

 #首先,让我们以希望它出现的方式订购DF 
#(第一个以更高的taskID开头)

#无dplyr版本
se.df $ Action< -as.character(se.df $ Action)
se.df<--se.df [order(se.df $ Start.Date,se.df $ Action),]
se.df $ taskID<-as.factor(nrow(se.df):1)


库(ggplot2)
ggplot(se.df,aes(x =开始。日期,y =任务ID,颜色=通讯类型))+
scale_y_discrete(breaks = se.df $ taskID,标签= se.df $ Action)+
geom_segment(aes(xend = End.Date ,y = taskID,yend = taskID),大小= 5)+
facet_grid(来源〜。,scale = free_y,space = free_y,drop = TRUE)


Question

I have a dataset, se.df (data at bottom of question), that I'm visualising as a factored gantt chart through the use of ggplot and facet_grid. However, the y-labels are not ordered as I've specified to aes

library(ggplot2)
base <- ggplot(
  se.df,
  aes(
    x = Start.Date, reorder(Action,Start.Date), color = Comms.Type
  ))
base + geom_segment(aes(
  xend = End.Date,ystart = Action, yend = Action
), size = 5) + 
facet_grid(Source ~ .,scale = "free_y",space = "free_y", drop = TRUE)

In this detail image you can see that there are bars that are:

  1. Not shown in Start.Date order
  2. Not ordered by Action. To clarify, bars should be ordered by Start.Date and then alphabetically by Action

How can I order the bars within each factor according to Start.Date and then by Action?

Update

@heathobrien provided a solution that solves my problem of ordering bars by Start.Date other than an issue arising from duplicate factors - which is something that my actual data has.

There are two instances of "Inform colleges" in Action, which result in a misordering in the following code from @heathobrien, highlighted in the image with a dashed red oval:

se.df <-se.df[order(se.df$Start.Date,se.df$Action),]
se.df$Action <- factor(se.df$Action, levels=unique(se.df$Action))
ggplot(se.df, aes(x = Start.Date, color = Comms.Type)) +
  geom_segment(aes(xend = End.Date, y = Action, yend = Action), size = 5) +
  facet_grid(Source ~ .,scale = "free_y",space = "free_y", drop = TRUE)

How can this data.frame be provided to ggplot such that ordering is consistent within each facet_grid?

Further Detail

There are a lot of questions about making Gantt charts and ordering factors, I've made a few decisions based on other's answers:

  1. geom_segment

Many questioners have used geom_linerange but suffer from it not being possible to use coord_flip with non-cartesian coordinate systems. Solutions to this are complicated and I've mitigated these with geom_segment.

  1. reorder within aes

The almost canonical bar ordering question uses reorder. However, this does not work for my data, even if using transform rather than specifying order to aes directly. I would be very happy to find any solution that worked.

Data

se.df <- structure(list(Source = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("a", "b", "c"), class = c("ordered", 
"factor")), Action = structure(c(21L, 30L, 19L, 27L, 16L, 17L, 
18L, 13L, 12L, 3L, 1L, 8L, 4L, 21L, 20L, 27L, 15L, 17L, 18L, 
14L, 26L, 2L, 8L, 5L, 22L, 26L, 2L, 8L, 5L, 22L, 22L, 11L, 7L, 
24L, 29L, 6L, 23L, 25L, 25L, 10L, 28L, 9L), .Label = c("Add OA \"Act on Acceptance\" to websites", 
"Add RDM liaison presece to divisional and departmental websites", 
"All-staff message from VC and/or Pro-VC (Research)", "Arrange OA Briefing for every department", 
"Arrange RDM Briefing for every department", "Brief Communication Officers Network", 
"Brief Conference of Colleges", "Brief divisional board/commitees", 
"Brief Faculty IT Officers", "Brief Research Committee", "Brief Senior Tutors", 
"Brief/mobilise internal comms officers", "Brief/mobilise ORFN", 
"Brief/mobilise Subject Librarians", "Ceate template slides for colleagues to use in delivering RDM Briefings", 
"Create template slides for colleagues to use in delivering OA Briefings", 
"Create template text & icon for use on websites", "Draft material for use in staff induction", 
"Ensure webpages for ORA & Symplectic Elements  are updated & consistent", 
"Ensure webpages for ORA-Data are updated & consistent", "Finalise key messages and draft campaign text", 
"Inform colleges ", "Inform Heads of Departments and Research Directors", 
"Present at Departmental Administrator's Meeting", "Present at HAF meeting", 
"Present at UAS Conference", "Produce hard copy materials to promote message ", 
"Update Divisional Board", "Update Library Committee (CLIPS)", 
"Update OAO website content for HEFCE/REF"), class = "factor"), 
    Start.Date = structure(c(1435705200, 1435705200, 1438383600, 
    1441062000, 1441062000, 1441062000, 1441062000, 1444518000, 
    1444518000, 1425168000, 1420070400, 1444518000, 1444518000, 
    1441062000, 1441062000, 1441062000, 1441062000, 1441062000, 
    1441062000, 1438383600, 1441062000, 1420070400, 1444518000, 
    1444518000, 1443654000, 1441062000, 1420070400, 1444518000, 
    1444518000, 1443654000, 1441062000, 1444518000, 1449273600, 
    1444518000, 1444518000, 1445036400, 1441062000, 1443740400, 
    1443740400, 1443740400, 1447459200, 1443740400), class = c("POSIXct", 
    "POSIXt"), tzone = ""), End.Date = structure(c(1440975600, 
    1440975600, 1443567600, 1443567600, 1443567600, 1443567600, 
    1443567600, 1449273600, 1449273600, 1430348400, 1446249600, 
    1449273600, 1449273600, 1446249600, 1446249600, 1443567600, 
    1443567600, 1443567600, 1443567600, 1443567600, 1443567600, 
    1443567600, 1449014400, 1449014400, 1451520000, 1443567600, 
    1443567600, 1449014400, 1449014400, 1451520000, 1443567600, 
    1449014400, 1449619200, 1449014400, 1449014400, 1446249600, 
    1446249600, 1449792000, 1449792000, 1449792000, 1447804800, 
    1449792000), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Comms.Type = structure(c(3L, 7L, 7L, 6L, 5L, 7L, 8L, 4L, 
    4L, 2L, 7L, 1L, 1L, 3L, 7L, 6L, 5L, 7L, 8L, 4L, 5L, 7L, 1L, 
    1L, 5L, 5L, 7L, 1L, 1L, 5L, 1L, 1L, 1L, 5L, 3L, 1L, 1L, 5L, 
    5L, 1L, 1L, 1L), .Label = c("Briefing", "Email", "Mixed Media", 
    "Mobilisation", "Presentations", "Printed Materials", "Website", 
    "Workshop"), class = "factor")), .Names = c("Source", "Action", 
"Start.Date", "End.Date", "Comms.Type"), row.names = c(NA, -42L
), class = c("tbl_df", "tbl", "data.frame"))

解决方案

I think this is what OP is looking for:

I had to create a synthetic taskID to pass the order (by increasing Start.Date, alpha by Action). By the way, if you want to order alphabetically by Action, you'll need to change the order of factors or convert to a char.

# first let's order the DF the way we want it to appear 
#    (higher taskID's first)

# dplyr-free version
se.df$Action <- as.character(se.df$Action)  
se.df <- se.df[order(se.df$Start.Date, se.df$Action), ]
se.df$taskID <- as.factor(nrow(se.df):1)


library(ggplot2)
ggplot(se.df, aes(x = Start.Date, y=taskID, color = Comms.Type)) +
  scale_y_discrete(breaks=se.df$taskID, labels = se.df$Action) + 
  geom_segment(aes(xend = End.Date, y = taskID, yend = taskID), size = 5) +
  facet_grid(Source ~ .,scale = "free_y",space = "free_y", drop = TRUE)

这篇关于使用带有重复Y因子的ggplot在基于geom_segment的甘特图中的排序条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆