You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言如何绘制季节图:按年份区分不同线条

Hey there! I see you're looking to create a seasonal plot to compare monthly DENI011 values across years—let's get this done step by step. I'll walk you through loading your data, cleaning it up, and building that year-over-year comparison plot with ggplot2.

绘制DENI011月度数值的年份对比季节图

1. 加载所需工具包

First up, we'll need the tidyverse bundle (it includes ggplot2 for plotting and dplyr for data wrangling) plus lubridate to handle date formatting easily:

# Install packages if you haven't already
install.packages(c("tidyverse", "lubridate"))
library(tidyverse)
library(lubridate)

2. 读取并预处理数据

Let's pull in your dataset and tweak it to get the year/month breakdown we need for plotting:

# Load the dataset
df <- read_csv("https://megastore.uni-augsburg.de/get/JVu_V51GvQ/")

# Clean and reshape the data
clean_df <- df %>%
  # Convert your date column to a proper date format (adjust ymd() to dmy()/mdy() if your date format differs!)
  mutate(date = ymd(date)) %>% 
  # Extract year, month labels (Jan/Feb/etc.), and month numbers (for proper x-axis ordering)
  mutate(
    year = year(date),
    month_label = month(date, label = TRUE, abbr = TRUE),
    month_num = month(date)
  ) %>%
  # Keep only the columns we need and drop rows with missing DENI011 values
  select(year, month_label, month_num, DENI011) %>%
  drop_na(DENI011)

Pro tip: If your date column has a non-standard format (like DD/MM/YYYY), swap ymd() for dmy() to avoid errors.

3. 绘制季节图

Now let's build the core plot—we'll use lines to represent each year, with color to distinguish them:

ggplot(clean_df, aes(x = month_num, y = DENI011, color = factor(year), group = year)) +
  # Add lines with slight transparency to prevent clutter
  geom_line(alpha = 0.8, linewidth = 1) +
  # Add points to mark individual monthly values (optional but helpful)
  geom_point(size = 2) +
  # Convert x-axis from month numbers to readable abbreviations
  scale_x_continuous(breaks = 1:12, labels = month.abb) +
  # Use a color palette that's easy to distinguish (swap for your favorite if you want!)
  scale_color_viridis_d(option = "plasma", name = "Year") +
  # Add clear titles and labels
  labs(
    title = "Year-over-Year Monthly Comparison of DENI011",
    x = "Month",
    y = "DENI011 Value"
  ) +
  # Use a clean, minimal theme
  theme_minimal() +
  # Tweak layout for better readability
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    axis.title = element_text(size = 12),
    legend.position = "bottom" # Move legend to bottom to avoid blocking the plot
  )

Optional Tweaks for Extra Polish

  • Highlight a specific year: Add a bold line for a key year (e.g., 2023) by adding this layer:
    geom_line(data = filter(clean_df, year == 2023), linewidth = 1.5, color = "darkred")
    
  • Smooth out large value ranges: Switch to a logarithmic y-axis with scale_y_log10() if your DENI011 values have huge swings.
  • Add annual averages: Include a dashed line showing the overall monthly average across all years:
    stat_summary(fun = mean, geom = "line", linetype = "dashed", color = "black", linewidth = 1)
    

That should give you a clear, informative seasonal plot that lets you easily compare DENI011 trends across years!

内容的提问来源于stack exchange,提问作者Essi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:27:44