0

我正在尝试使用 Geom_Segment 在 R 中创建一个绘图。我遇到了一个错误,说我需要输入 yend,但我已经在输入了……这是我的代码:

library(ggplot2) 
library(data.table)
library(magrittr)


dataset$From<-Sys.Date()
format(dataset$From, format="%Y-%m-%dT%H:%M:%OS")
dataset$To<-Sys.Date()
format(dataset$To, format="%Y-%m-%dT%H:%M:%OS")



ggplot(dataset, aes(x=datetime_start, y=dataset$Audit_Title, 
color=dataset$Employee_Name)) +
  geom_segment(aes(x=dataset$From,xend=dataset$To,y=dataset$Audit_Title,yend=dataset$Audit_Title),size=20)+
  scale_colour_discrete(guide=guide_legend(override.aes=list(size=15))) +
ggtitle("Audit by Employee Timeline") + xlab("") + ylab("") + theme_bw()

样本数据: 这是样本数据

这就是我如何更改下面的代码以从 Excel 中获取我输入到 Power BI 中的数据:

library(ggplot2)
library(dplyr)

# transform into date
dataset <- dataset %>% 
  mutate_at(vars(dataset$From, dataset$To),
            .funs = function(tt) readr::parse_date(as.character(tt),
                             format = "%m/%d/%Y"))

ggplot(dataset)+
  geom_segment(aes(x=dataset$From, xend=dataset$To,
                   y=dataset$Employee_Name, yend=dataset$Employee_Name))
4

1 回答 1

0

First of all, ideally you would share your data as a dput(dataset). If you can't share real data, you should make a minimal reproducible example and share that. See here

Here's your data

library(ggplot2)
library(dplyr)
df <- 
read.table(
text =  
"01/03/2020 03/16/2020 Supply_Chain John_Smith 
05/08/2020 08/20/2020 Business_Unit Karen_Scott")

names(df) <- c("From", "To", "Audit_Title", "Employee_Name")
# transform into date
df <- df %>% 
  mutate_at(vars(From, To),
            .funs = function(tt) readr::parse_date(as.character(tt),
                             format = "%m/%d/%Y"))

Now do the actual plot by selecting the proper x xend and having y be the employee (y=yend).

ggplot(df)+
  geom_segment(aes(x=From, xend=To,
                   y=Employee_Name, yend=Employee_Name))

Which produces

enter image description here

If you want fancy colors, labels and stuff go ahead and check the proper documentation for ggplot. See here

于 2020-12-07T23:40:12.010 回答