基于给定DataFrame数据集,在同一绘图框架绘制双函数的技术咨询
No problem at all! Let's break down two straightforward methods to visualize your S and D columns against the Date values in the same plot. I'll cover both base R (great for quick, no-frills plots) and ggplot2 (ideal for customizable, polished graphs).
First, let's make sure your data is properly formatted—your Date column is numeric (like 199011 for November 1990), so we'll convert that to a proper date type first. I've completed the truncated row in your sample data for consistency:
# Load the full sample data data2 <- read.table(text = "Date S D 199011 1.023247 1.009845 199012 1.050828 1.015818 199101 1.066754 1.023077 199102 1.147112 1.033462 199103 1.160859 1.042610 199104 1.164412 1.049691 199105 1.204586 1.058778 199106 1.173015 1.063795 199107 1.220449 1.074115 199108 1.210946 1.075537 199109 1.219717 1.076117 199110 1.256516 1.080941 199111 1.220505 1.087333 199112 1.288720 1.100406 199201 1.306862 1.106454 199202 1.304459 1.108409 199203 1.250000 1.110000", header = TRUE) # Convert numeric Date to actual date format (using first day of each month) data2$Date <- as.Date(paste0(data2$Date, "01"), "%Y%m%d")
Method 1: Base R Plotting
This is perfect for quick checks when you don't need fancy customization:
# First plot the S series as a blue line plot(data2$Date, data2$S, type = "l", col = "blue", xlab = "Date", ylab = "Value", main = "S vs D Over Time") # Add the D series as a red line on top lines(data2$Date, data2$D, type = "l", col = "red") # Add a legend to clarify which line is which legend("topleft", legend = c("S", "D"), col = c("blue", "red"), lty = 1)
type = "l"tells R to draw a line instead of individual pointslines()adds the second series without overwriting the initial plot- The legend ensures readers can distinguish between the two lines easily
Method 2: ggplot2 (Recommended for Customization)
ggplot2 is part of the tidyverse and excels at creating flexible, publication-ready visuals. We'll first reshape our data to "long" format (tidy data) which plays better with ggplot's syntax:
# Load required packages library(ggplot2) library(tidyr) # Reshape data from wide to long format data_long <- pivot_longer(data2, cols = c(S, D), names_to = "Series", values_to = "Value") # Create the plot ggplot(data_long, aes(x = Date, y = Value, color = Series)) + geom_line(linewidth = 1) + labs(title = "S vs D Over Time", x = "Date", y = "Value") + scale_color_manual(values = c("S" = "blue", "D" = "red")) + theme_minimal()
pivot_longer()stacks theSandDcolumns into a singleValuecolumn, with aSeriescolumn to track which value belongs to which groupaes(color = Series)automatically assigns distinct colors to each seriestheme_minimal()gives a clean, modern look—swap it fortheme_bw()ortheme_classic()if you prefer a different style
Both methods will give you a single figure with both lines plotted against the date axis. Base R is faster for quick checks, while ggplot2 lets you tweak every detail (like fonts, line thickness, or legend placement) to match your needs.
内容的提问来源于stack exchange,提问作者Mads

