| name | r-plotter |
| description | Skill for modeling and rendering mathematical functions and data visualizations using R. Generates R scripts to create high-quality, appropriately scaled images for LaTeX documents. Handles directory management for image assets. |
R Plotter Skill
When to use this skill
ALWAYS use this skill when the user requests:
- To graph mathematical functions (contours, surfaces, 2D plots).
- To visualize data distributions or statistical models.
- To create high-quality images for LaTeX or other documents.
- To replicate existing graphs from screenshots using R.
1. Environment Setup
Ensure R is installed. If not, request installation:
sudo apt install r-base-core
Verify installation with Rscript --version.
2. Script Generation Protocol
- Create a dedicated R script for the specific visualization (e.g.,
plot_q9.R).
- Define the Output Path:
- Identify the target directory for images (e.g.,
images/ or figures/ within the LaTeX project).
- Ensure the directory exists or create it in the script.
- Configure the Device:
- Use
png() or pdf() devices.
- CRITICAL: Set appropriate dimensions and resolution for LaTeX inclusion.
- Recommended for LaTeX:
width = 6, height = 6, units = "in", res = 300 (for PNG).
- Plotting:
- Use
ggplot2 for complex, layered plots or base graphics for simple mathematical functions if dependencies are limited.
- For Contour Plots (as in Q9): Use
filled.contour() or levelplot() from lattice, or geom_contour_filled() from ggplot2.
- Export:
- Close the device with
dev.off().
3. Image Placement Strategy
4. Replicating Specific Graphs (Reverse Engineering)
- Analyze the Screenshot: Identify the function type (hyperbolas, parabolas, etc.), domains, and ranges.
- Estimate Parameters: Guess coefficients to match the visual curves.
- Iterate: Run the script, check the output, adjust parameters until it matches the screenshot.
5. Example Script (Template)
output_file <- "images/example_plot.png"
dir.create(dirname(output_file), showWarnings = FALSE, recursive = TRUE)
png(output_file, width = 6, height = 5, units = "in", res = 300)
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)
grid <- expand.grid(x = x, y = y)
z <- with(grid, x * y)
filled.contour(x, y, matrix(z, nrow = 100),
color.palette = function(n) hcl.colors(n, "YlOrRd", rev = TRUE),
plot.title = title(main = "Question 9: Level Curves"),
xlab = "x", ylab = "y")
dev.off()
print(paste("Image saved to:", output_file))