Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot numeric vector with names as x ticks

Tags:

plot

r

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")?

like image 703
felixmc Avatar asked Jun 23 '13 17:06

felixmc


3 Answers

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))
like image 136
Didzis Elferts Avatar answered Nov 14 '22 06:11

Didzis Elferts


Do you look for barplot?

barplot(quantity)
like image 7
sgibb Avatar answered Nov 14 '22 04:11

sgibb


And another option using lattice:

library(lattice)
barchart(quantity)

enter image description here

like image 4
agstudy Avatar answered Nov 14 '22 05:11

agstudy