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

在R中绘制类Excel散点图的技术求助(含给定数据集)

Solution for Creating a Scatterplot in R (X=Row Number, Y=Your Dataset)

Hey Angelo, I’ve got you covered! It’s common to run into minor tweaks needed with base R’s plot() to get that Excel-style scatterplot looking just right. Let’s walk through two solid solutions—one with base R (since you tried plot() already) and another with ggplot2 for a more polished, customizable look.


Step 1: Prepare Your Data

First, let’s convert your raw dataset into a usable vector and create the X-axis values (row numbers):

# Paste your dataset into a numeric vector
y_values <- c(251, 63, 0, -109, 15, 0, 0, 139, -182, 0, 94, -110, 0, -155, -103, 39, -106, 0, -225, -99, 89, -128, 145, 122, 0, 0, -58, 158, -115, -115, 0, 0, 141, -33, -148, -41, 55, 0, 77, 92, -174, 91, -82, 130, 18, 119, 0, 128, 103, 35, -119, 0, -184, 88, 83, -35, -164, 129, 0, -94, -102, -129, 78, -58, 44, 0, 90, -262, -217, 0, 0, -157, 0, 0, -66, 0, 0, 76, 0, 36, -135, 0, 0, 0, 50, 0, 0, 55, 0, 0, 0, 0, 0, -123, 0, -92, -151, -119, 0, 0, 0, 57, 3, 0, 151, 0, 0, -102, -123, 75, -88, 0, 147, 2, 0, 0, 0, -89, 0, 0, -68, 159, -63, 86, -192, 114, -103, 0, -84, 97, 0, 0, 18, 0, 0, -103, 0, 0, -154, -64, 0, -151, 9, 35, 0, -140, 0, -122, -149, -209, 0, -98, 0, 107, 0, -104, 140, 70, 0, 96, 0, -37, 0, -205, 80, 0, -93, -68, -101, 12, -141, -92, 0, 0, 0, 88, 0, 0, 0, 0, 12, -133, 0, 0, 0, 56, 0, 0, 226, 0, 101, -124, 236, -30, 0, -143, 73, -176, 193, 0, 29, 67, 0, 149, 65, 0, 125, 65, -82, -126, 132, 0, 84, -103, 0, 9, 37, 91, -111, -125, -53, -194, 200, -63, 80, -97, -86, 163, 0, 0, 0, 60, 0, 132, 0, -162, 0, 0, 0, 98, 68, 0, 40, 0, 0, 87, -93, 77, -127, 0, 13, -148, 0, 88, 202, 0, -132, 0, -99, 0, -116, 0, -162, 0, 0, 0, 0, -36, 0, 0, 0, 0, 0, -75, 0, 100, 100, 48, -148, -44, 0, 0, 27, -111, 144, -156, 0, 0, -20, -97, -187, 56, -148, 0, -34, 0, 0, -163, 0, 6, 6, 208, 110, -102, -64, 93, 0, 0, 116, -105, -111, 80, -126, -203, -86, 0, 0, 1, -96, 0, 0, 0, 0, 187, 0, -76, 34, -67, 96, 0, 0, 0, 0, 0, 0, 0, 90, 72, 126, -71, -109, 0, 124, 0, 0, 0, 0, 0, 16, 65, -190, 0, 0, 0, 43, 93, -67, -87, 80, 154, 0, -210, 80, 203, -70, -154, 0, 0, -64, -84, -102, 0, -141, 0, 0, -141, 42, -84, 0, 89, 0, 0, -151, -97, -22, -100, 0, 140, -155, 0, 0, -191, 83, 130, 207, -48, -92, 29, 0, 0, 0, 0, -147, 0, -72)

# Create X-axis: row numbers (index of each value)
x_values <- seq_along(y_values)

Option 1: Base R Plot (Tweaked for Better Results)

If you want to stick with base R’s plot(), the key is adjusting axis limits, point style, and adding a reference line at Y=0 to highlight the center distribution:

# Generate the scatterplot with optimized settings
plot(x_values, y_values,
     main = "Values vs. Row Number",
     xlab = "Row Number",
     ylab = "Value",
     pch = 16,          # Solid circles (more visible than default hollow ones)
     col = "#2c3e50",   # Dark gray for points
     ylim = c(min(y_values) - 10, max(y_values) + 10)) # Add padding to Y-axis

# Add a dashed red reference line at Y=0
abline(h = 0, col = "#e74c3c", lwd = 2, lty = 2)

Why this works better:

  • ylim ensures all extreme values are visible (no cutoff points)
  • pch=16 makes points easier to distinguish
  • The reference line at Y=0 clarifies how values cluster around zero

Option 2: ggplot2 (More Polished & Customizable)

For a cleaner, Excel-like look with more flexibility, use ggplot2:

# Install ggplot2 if you haven't already
# install.packages("ggplot2")
library(ggplot2)

# Convert to a data frame (required for ggplot2)
df <- data.frame(
  row_number = x_values,
  value = y_values
)

# Create the scatterplot
ggplot(df, aes(x = row_number, y = value)) +
  geom_point(color = "#2c3e50", size = 2) +
  geom_hline(yintercept = 0, color = "#e74c3c", linetype = "dashed", linewidth = 1) +
  labs(
    title = "Values vs. Row Number",
    x = "Row Number",
    y = "Value"
  ) +
  theme_minimal() +
  ylim(min(y_values) - 10, max(y_values) + 10)

Bonus tweaks:

  • Swap theme_minimal() for theme_bw() if you prefer a white background like Excel
  • Adjust size in geom_point() to make points larger/smaller
  • Change color codes to match your preferred palette

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:25:48