基于R的日期时间序列:CSV犯罪数据频次可视化高效方法咨询
Hey there! Since you're new to R, let's walk through the most straightforward, efficient way to create your time series plots using the tidyverse ecosystem—it's perfect for data wrangling and visualization, and it's beginner-friendly too.
Step 1: Set Up Your Tools
First, install and load the tidyverse package (it bundles dplyr for data cleaning and ggplot2 for plotting, which are exactly what we need):
# Install the package if you haven't already install.packages("tidyverse") # Load it into your R session library(tidyverse)
Step 2: Read and Clean Your CSV Data
Use read_csv() to import your data, then make sure your date column is formatted as a proper date (critical for accurate time series plots):
# Replace "your_crime_data.csv" with your actual file path/name crime_data <- read_csv("your_crime_data.csv") # Check the structure of your data to confirm column types glimpse(crime_data) # Convert the date column to a Date format (adjust the column name if yours is different) crime_data <- crime_data %>% mutate(date = as.Date(date)) # If your date uses a non-standard format (e.g., "mm/dd/yyyy"), specify it like this: # crime_data <- crime_data %>% # mutate(date = as.Date(date, format = "%m/%d/%Y"))
Step 3: Wrangle Data for Your Plots
We'll create two datasets: one for daily total crimes, and another for daily counts broken down by crime type.
# Calculate daily total crime frequency daily_total_crimes <- crime_data %>% group_by(date) %>% summarize(total_crimes = n()) # n() counts the number of crimes per date # Calculate daily crime frequency by type daily_crimes_by_type <- crime_data %>% group_by(date, crime_type) %>% summarize(count = n())
Step 4: Create the Time Series Plots
Plot 1: Daily Total Crimes
This shows the overall trend of crimes throughout the year:
ggplot(daily_total_crimes, aes(x = date, y = total_crimes)) + geom_line(color = "#2980b9", linewidth = 1) + # Customize line color and thickness labs( title = "Daily Total Crimes (Yearly Trend)", x = "Date", y = "Total Number of Crimes" ) + theme_minimal() # Clean, easy-to-read theme
Plot 2: Daily Crimes by Type (Color-Coded)
This overlays all crime types on one plot to compare their trends side-by-side:
ggplot(daily_crimes_by_type, aes(x = date, y = count, color = crime_type)) + geom_line(linewidth = 0.8) + labs( title = "Daily Crimes by Type", x = "Date", y = "Number of Crimes", color = "Crime Type" ) + theme_minimal() + theme(legend.position = "bottom") # Move legend to bottom for better readability
Plot 3: Daily Crimes by Type (Faceted)
If you prefer separate plots for each crime type (great for focusing on individual trends):
ggplot(daily_crimes_by_type, aes(x = date, y = count)) + geom_line(color = "#e74c3c", linewidth = 0.8) + facet_wrap(~crime_type) # Creates a separate plot for each crime type labs( title = "Daily Crimes by Type (Individual Plots)", x = "Date", y = "Number of Crimes" ) + theme_minimal()
Quick Troubleshooting Tips
- If you have days with no crimes (missing dates in your data), use
complete()to add those days with 0 counts:daily_total_crimes <- crime_data %>% group_by(date) %>% summarize(total_crimes = n()) %>% complete(date = seq(min(date), max(date), by = "day"), fill = list(total_crimes = 0)) - Add a smoothing line to highlight long-term trends: just add
geom_smooth(method = "loess", se = FALSE, color = "#f39c12")to your ggplot code.
内容的提问来源于stack exchange,提问作者Taylor Wilde

