0

Today I want to learn a little bit about the R statistical programming language.

I'm not finding the tutorials or anything that i have looked up online to be helpful. I want to create a frequency polygon that shows the date on the X axis , the Count on the y axis and each line representing each vehicle. to summarize, I want-

          X axis=  date 
          Y axis=  count
          line=    vehicle type

bellow is an image of the data frame I am working with

enter image description here

Ideally, it should look something like this-

enter image description here

mb67
  • 51
  • 1
  • 1
  • 6
  • 1
    You might want to take a look at [how to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). For `R`, [`dput` will help you share data](https://stackoverflow.com/questions/49994249/example-of-using-dput) so that users can answer your questions (by using `dget`). – hrvg Mar 25 '22 at 16:32

2 Answers2

1

I created a small reproducible dataset:

# A tibble: 29 × 3
   vehicle Count1 TIME1              
   <chr>    <dbl> <dttm>             
 1 PCL          1 1899-12-31 07:00:00
 2 MCL          9 1899-12-31 07:00:00
 3 CAR          2 1899-12-31 07:00:00
 4 TAXI         7 1899-12-31 07:00:00
 5 LGV          0 1899-12-31 07:00:00
 6 OGV1         3 1899-12-31 07:00:00
 7 OGV2         0 1899-12-31 07:00:00
 8 CDB          4 1899-12-31 07:00:00
 9 BEB          0 1899-12-31 07:00:00
10 OB          10 1899-12-31 07:15:00
# … with 19 more rows

dput of the data:

structure(list(vehicle = c("PCL", "MCL", "CAR", "TAXI", "LGV", 
"OGV1", "OGV2", "CDB", "BEB", "OB", "PCL", "MCL", "CAR", "TAXI", 
"LGV", "OGV1", "OGV2", "CDB", "BEB", "OB", "PCL", "MCL", "CAR", 
"TAXI", "LGV", "OGV1", "OGV2", "CDB", "BEB"), Count1 = c(1, 9, 
2, 7, 0, 3, 0, 4, 0, 10, 4, 4, 2, 11, 10, 6, 10, 8, 10, 10, 4, 
4, 2, 11, 10, 6, 10, 8, 10), TIME1 = structure(c(-2209050000, 
-2209050000, -2209050000, -2209050000, -2209050000, -2209050000, 
-2209050000, -2209050000, -2209050000, -2209049100, -2209049100, 
-2209049100, -2209049100, -2209049100, -2209049100, -2209049100, 
-2209049100, -2209049100, -2209049100, -2209048200, -2209048200, 
-2209048200, -2209048200, -2209048200, -2209048200, -2209048200, 
-2209048200, -2209048200, -2209048200), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -29L))

You can use the following code to plot that graph using ggplot:

library(tidyverse)

df %>%
  ggplot(aes(x = TIME1, y = Count1, color = vehicle)) +
  geom_line() +
  ylab("Count") +
  xlab("time") +
  ggtitle("Your plot")

Output:

enter image description here

The lines look weird, because the dataset is very small.

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • I may be wrong but I think the question means that the counts are grouped by timestamp. – hrvg Mar 25 '22 at 16:34
0

Using the reproducible dataset from Quinten and grouping by timestamp:

library(dplyr)
library(ggplot2)

count_df <- df %>% group_by(TIME1) %>% dplyr::summarize(n = sum(Count1))

ggplot(count_df, aes(x = TIME1, y = n)) +
geom_line() +
ylab("count") +
xlab("time")

enter image description here

hrvg
  • 476
  • 3
  • 6