If I am using R to plot. How can I assign a particular shape to data points belonging to one category (using the pch
argument to plot()
) based on a column in my data frame that has the categorical data? Will using as.factor()
to group data and then using pch
help ?
In the R Language, we can use “+”, “*“,”-“,”. “,”#, “%”, and “o” for plotting different symbols.
Plot character or pch is the standard argument to set the character that will be plotted in a number of R functions. Explanatory text can be added to a plot in several different forms, including axis labels, titles, legends, or a text added to the plot itself.
The pch stands for plot character. The pch contains numeric values rangers from 0 to 25 or character symbols (“+”, “.”, “;”, etc.) specifying in symbols (or shapes). The pch is the standard argument to set the character plotted in several R functions.
Change R base plot point shapes The default R plot pch symbol is 1, which is an empty circle. You can change this to pch = 19 (solid circle) or to pch = 21 (filled circle). To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name).
You mean something like this...?
plot(Sepal.Length ~ Petal.Length,
xlab = "Petal Length (cm)",
ylab = "Sepal Length (cm)",
pch = c(16, 17, 18)[as.numeric(Species)], # different 'pch' types
main = "Anderson Iris Dataset",
col = c("red", "green","blue")[as.numeric(Species)],
data = iris)
Which produces:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With