Struggling to create a scatter plot with time span

No worries, @dillalan :slight_smile: if you're making a reprex it generally helps us a lot for you to make the data available, but I think in this case it's enough that we can see the structure of your data frame.

The reason my example isn't working for you in that the x aesthetic isn't defined. geom_errorbarh only requires xmin and xmax (since the errorbar doesn't necessarily have a "middle"), but the plot still needs x. If you add x = DataDistACR to the aes() inside ggplot(), that should do the trick.

Also note that you don't need to prefix your column names with your data frame when building a ggplot2 plot—since you specify the data frame at the start with ggplot(Tabela_Tempo_Julgamento), you can (and should) just write things like y = ID, rather than y = Tabela_Tempo_Julgamento$ID.

If you want to plot the length of time between your start and end dates, I'm assuming you'll want to it using a single unit (number of days, or number of years), rather than using "x years, y months, z days" (which works well in text but not in a plot). If that's the case, you needn't bother with interval or as.period(): you can get the number of days by simply subtracting the start date from the end date. So you could do:

library(readxl)
setwd("D:/Alan/UFRGS/Projetos RStudio")
Tabela_Tempo_Julgamento <- read_excel("Tabela Tempo Julgamento.xlsx", 
                                      col_types = c("date", "date", "date", 
                                                    "date", "text", "text"))

# so if start date is DataDistACR and end date is DataJulgACR…
Tabela_Tempo_Julgamento$length =
  Tabela_Tempo_Julgamento$DataDistACR - Tabela_Tempo_Julgamento$DataJulgACR

And then length would be either your x or y inside ggplot's aes(). There's no need to convert the length between days and years if it's just for plotting; ggplot2 can intelligently do this if you add a scale_*() function to your plot.

If you want a scatter plot where case length is on one axis, what would you have on your other axis?

3 Likes