0

I'm trying to show in a linear chart in R from a Y (project) given the maximum date that corresponds to that Y, but it only shows me the highest date of that dataframe and not of each of the projects in order.

Example of Dataframe :

enter image description here

Chart:

enter image description here

Code:

 output$grafica5 <-renderPlotly({
   p <- ggplot(data(),aes(x=max(as.Date(data()[,names(data())[3]], format = "%d/%m/%Y")), y=as.factor(data()[,names(data())[2]]), group=1))+
   geom_line()+
     geom_point()+
     theme_classic()+
     theme(axis.text.x=element_text(angle=45, hjust=1))+scale_color_viridis(discrete = TRUE)+
     labs(
       y="Proyectos", 
       x="Fecha")})

Can you help me? Thanks in advance

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. (Pictures of data are not helpful because we cannot copy-paste those values to test your code) – MrFlick Feb 03 '20 at 19:33

1 Answers1

0

You should do your data manipulation prior to the plotting (as.date and filtering the max values)

library(dplyr)
library(ggplot2)

# creation of a data.frame with repeated projects with due dates
df <- data.frame(
  Projects = paste0("projects", sample(1:9, 20, T)),
  Duedate = paste0(sample(10:30, 20, T), "/01/2020" )
)

# creation of a variable `dates` and filtering of the max date per project
df <- df %>%
  mutate(dates = as.Date(Duedate, format = "%d/%m/%Y")) %>%
  group_by(Projects) %>%
  filter(dates == max(dates)) %>%
  ungroup()

ggplot(df, aes(x = dates, y = Projects, group = 1)) +
  geom_line() +
  geom_point()
Gallarus
  • 476
  • 3
  • 9