rggplot2plotgeom

R plot date data multiple geom_line


Plotting the below data. trying to include the lines per tech, Without using the facetwrap. Plot both tech and files per enm --

Date emmm task coll_task tech filescount
2023-06-01 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-01 vnmenm1 tr e4g_tr_vnmenm1 4g 3475
2023-06-01 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 317
2023-06-03 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-03 vnmenm1 tr e4g_tr_vnmenm1 4g 8899
2023-06-03 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 296
2023-06-04 vnmenm1 tr e3g_tr_vnmenm1 3g 1136
2023-06-04 vnmenm1 tr e4g_tr_vnmenm1 4g 9034
2023-06-04 vnmsgsn1 tr e4g_tr_vnmsgsn1 4g 292

Using below code -

data %>% group_by(Date, emmm, tech, filescount) %>%  summarize(filescount = sum(filescount)) %>% ggplot(aes(Date, emmm, tech, color = filescount)) + geom_point(size = 2.4, alpha = 0.5) + geom_line(aes(x = Date, y = emmm), size = 1,  alpha = 0.4, stat = "identity", na.rm = TRUE) 

gives an image like - enter image description here

Any way to present with both tech and showing the filescount, i.e - geom_line per tech and filescount !


Solution

  • library(ggplot2)
    library(dplyr)
    
    # Your given test data as a dataframe
    df <- data.frame(
        Date = as.Date(c("2023-06-01", "2023-06-01", "2023-06-01", "2023-06-03", "2023-06-03", "2023-06-03", "2023-06-04", "2023-06-04", "2023-06-04")),
        emmm = c("vnmenm1", "vnmenm1", "vnmsgsn1", "vnmenm1", "vnmenm1", "vnmsgsn1", "vnmenm1", "vnmenm1", "vnmsgsn1"),
        task = rep("tr", 9),
        coll_task = c("e3g_tr_vnmenm1", "e4g_tr_vnmenm1", "e4g_tr_vnmsgsn1", "e3g_tr_vnmenm1", "e4g_tr_vnmenm1", "e4g_tr_vnmsgsn1", "e3g_tr_vnmenm1", "e4g_tr_vnmenm1", "e4g_tr_vnmsgsn1"),
        tech = c("3g", "4g", "4g", "3g", "4g", "4g", "3g", "4g", "4g"),
        filescount = c(1136, 3475, 317, 1136, 8899, 296, 1136, 9034, 292)
    )
    
    # Create a dataframe to plot
    df_sum <- df %>%
        group_by(emmm, tech, Date) %>%
        summarize(total_files = sum(filescount), .groups = 'drop')
    
    # Plot the data
    ggplot(df_sum, aes(x = Date, y = total_files, group = interaction(emmm, tech), color = interaction(emmm, tech), linetype = interaction(emmm, tech))) +
        geom_line() +
        geom_point() +
        labs(x = "Date", y = "Files per Tech", color = "Tech", linetype = "Tech") +
        scale_color_discrete(name = "Emmm - Tech", labels = c("vnmenm1 - 3g", "vnmenm1 - 4g", "vnmsgsn1 - 4g")) +
        scale_linetype_discrete(name = "Emmm - Tech", labels = c("vnmenm1 - 3g", "vnmenm1 - 4g", "vnmsgsn1 - 4g")) +
        theme_minimal()
    

    Output:

    enter image description here