11.7 Low-level Plotting Functions - YaRrr! The Pirate's Guide To R

11.7.2 points()

To add new points to an existing plot, use the points() function. The points function has many similar arguments to the plot() function, like x (for the x-coordinates), y (for the y-coordinates), and parameters like col (border color), cex (point size), and pch (symbol type). To see all of them, look at the help menu with ?points().

Let’s use points() to create a plot with different symbol types for different data. I’ll use the pirates dataset and plot the relationship between a pirate’s age and the number of tattoos he/she has. I’ll create separate points for male and female pirates:

# Create a blank plot plot(x = 1, type = "n", xlim = c(100, 225), ylim = c(30, 110), pch = 16, xlab = "Height", ylab = "Weight", main = "Adding points to a plot with points()") # Add coral2 points for male data points(x = pirates$height[pirates$sex == "male"], y = pirates$weight[pirates$sex == "male"], pch = 16, col = transparent("coral2", trans.val = .8)) # Add steelblue points for female data points(x = pirates$height[pirates$sex == "female"], y = pirates$weight[pirates$sex == "female"], pch = 16, col = transparent("steelblue3", trans.val = .8)) Using points() to add points with different colors

Figure 11.8: Using points() to add points with different colors

Từ khóa » How To Use Segments Function In R