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

RStudio中双函数同图绘制及函数合并绘图技术咨询

Hey there! Let's work through your two R plotting requests one by one—they're straightforward once you break them down.

1. Plotting fun1 and fun2 on the same graph

First, let's confirm your original functions (I've simplified the math a bit since 2*sin(x)*cos(x) is equivalent to sin(2*x)—it's cleaner and works the same way):

fun1 <- function(x) -sin(2*x)
fun2 <- function(x) sin(2*x)

You can plot these together using either base R or ggplot2. Here are both approaches:

Method 1: Base R Plotting

This is quick and doesn't require any extra packages:

# Generate a sequence of x-values (0 to 2π, 100 points for smooth curves)
x_vals <- seq(0, 2*pi, length.out = 100)

# Start by plotting fun1
plot(x_vals, fun1(x_vals), type = "l", col = "navy", lwd = 2,
     xlab = "x", ylab = "Function Value", main = "fun1 vs fun2")

# Add fun2 to the same plot with lines()
lines(x_vals, fun2(x_vals), col = "firebrick", lwd = 2)

# Add a legend to distinguish the curves
legend("topright", legend = c("fun1(x) = -sin(2x)", "fun2(x) = sin(2x)"),
       col = c("navy", "firebrick"), lwd = 2)

Method 2: Using ggplot2

If you prefer a more polished, customizable plot:

library(ggplot2)

# Combine the data into a single data frame (easier for ggplot)
plot_data <- data.frame(
  x = rep(x_vals, 2),
  y = c(fun1(x_vals), fun2(x_vals)),
  function_name = rep(c("fun1(x)", "fun2(x)"), each = length(x_vals))
)

# Build the plot
ggplot(plot_data, aes(x = x, y = y, color = function_name)) +
  geom_line(linewidth = 1) +
  labs(x = "x", y = "Function Value", title = "fun1 vs fun2") +
  theme_minimal() +
  scale_color_manual(values = c("navy", "firebrick"))
2. Creating a combined parametric function (fun3) and plotting it

Your parametric expression γ(x) = (-2*sin(x)*cos(x), 2*cos(x)*sin(x)) translates to a function that returns both x and y coordinates for a given parameter value. Let's define fun3 to return a list of these coordinates (again, simplified using sin(2x)):

fun3 <- function(t) {
  # Using t instead of x to avoid confusion with the x-coordinate
  list(
    x = -sin(2*t),
    y = sin(2*t)
  )
}

Parametric curves are plotted by generating a sequence of parameter values, computing the corresponding (x,y) pairs, then drawing a path through those points. Here's how to do it:

Method 1: Base R

# Generate parameter values (0 to 2π, 200 points for smoothness)
t_vals <- seq(0, 2*pi, length.out = 200)

# Calculate (x,y) coordinates for each t
curve_coords <- lapply(t_vals, fun3)
x_coords <- sapply(curve_coords, function(p) p$x)
y_coords <- sapply(curve_coords, function(p) p$y)

# Plot the parametric curve
plot(x_coords, y_coords, type = "l", col = "forestgreen", lwd = 2,
     xlab = "γ_x(t)", ylab = "γ_y(t)", main = "Parametric Curve γ(t)")

# Mark the starting point (t=0) for clarity
points(fun3(0)$x, fun3(0)$y, pch = 19, col = "black")
text(fun3(0)$x + 0.1, fun3(0)$y, "t=0", col = "black")

Method 2: ggplot2

# Convert coordinates to a data frame
param_data <- data.frame(
  t = t_vals,
  x = x_coords,
  y = y_coords
)

# Plot the curve
ggplot(param_data, aes(x = x, y = y)) +
  geom_path(linewidth = 1, color = "forestgreen") +
  geom_point(aes(x = fun3(0)$x, y = fun3(0)$y), color = "black", size = 3) +
  geom_text(aes(x = fun3(0)$x + 0.1, y = fun3(0)$y), label = "t=0") +
  labs(x = "γ_x(t)", y = "γ_y(t)", title = "Parametric Curve γ(t)") +
  theme_minimal()

A quick note: Since γ_y(t) = -γ_x(t), this parametric curve is just a back-and-forth path along the line y = -x! As t goes from 0 to 2π, sin(2t) completes two full cycles, so the curve travels from (0,0) → (-1,1) → (0,0) → (1,-1) → (0,0).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:15:52