I have a numeric vector and wish to plot each value on y-axis by their name on the x-axis.
Example:
quantity <- c(3,5,2)
names(quantity) <- c("apples","bananas", "pears")
plot(quantity)
Each value is plotted with it's index number along the x-axis ie. 1,2,3. How can I get it to show ("apples","bananas", "pears")?
You can use function axis() to add labels. Argument xaxt="n" inside plot() will make plot without x axis labels (numbers).
plot(quantity,xaxt="n")
axis(1,at=1:3,labels=names(quantity))
Do you look for barplot?
barplot(quantity)
And another option using lattice:
library(lattice)
barchart(quantity)

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