0

I am trying to create a plot in R using Geom_Segment. I am stuck with an error that says I need to input yend but I am inputting it already... this is my code:

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

SAMPLE DATA: Here is the sample data

This is how I changed the code below to take in the data from Excel I inputted into 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))
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
Natalie
  • 1
  • 1
  • 2
  • Hello, in order to reproduce your error we would need a `dput(dataset)` or at least a partial dataset. Another thing, you don't need to do `y=dataset$Audit_Title`, try `y=Audit_Title`. Also, From and To in your dataset are only one value (`Sys.Date()`), is that expected? What does the plot you produce look like and what's the expected output? – Matias Andina Dec 07 '20 at 22:01
  • Hello! Thanks for your help. Sample Data would look like: From To Audit_Title Employee_Name 1/3/2020 3/16/2020 Supply Chain John Smith 5/8/2020 8/20/2020 Business Unit Karen Scott The dates should be different. I removed the part of the code with Sys.Date() and nothing changes, so I do not think I need that. My plot does not produce anything because of the errors. I am trying to do something similar to this: https://stackoverflow.com/questions/45112019/how-to-add-label-to-geom-segment-at-the-start-of-the-segment/65189582#65189582 – Natalie Dec 07 '20 at 22:14

1 Answers1

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

Matias Andina
  • 4,029
  • 4
  • 26
  • 58
  • Thank you so much for letting me know how to better my posts. I will definitely follow on my future posts. I have tried to use this code and when I connect my database(instead of inputting the data in the code like above) I still get the error of "geom_segment requires the following missing aesthetics: x, xend, y, yend" **I am using the R capabilities in Power BI to make this visual. and R in PowerBI also requires me to put "dataset$Employee_Name" and etc.** – Natalie Dec 08 '20 at 01:08
  • I put the code I tried from yours above where my question is. It seems like R in Power BI is not recognizing the x, y, xend, yend. – Natalie Dec 08 '20 at 01:14
  • I am not familiar with Power BI, I think this should be a central part of your question. What happens if you don't use `dataset$Employee_Name` and use instead the normal way of doing this with ggplot2 (just `Employee_Name`)? Are you reading the dataset from a local copy? Can you reproduce this code in R (either from a terminal, RGui or Rstudio), just to make sure the problem is with Power BI and not with the code? – Matias Andina Dec 08 '20 at 01:31