Plot a numeric vector with names like x ticks

I have a numeric vector and you want to plot each value on the 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 displayed with its index number along the x axis, i.e. 1,2,3. How can I show it ("apples", "bananas", "pears")?

+6
source share
4 answers

You can use the axis() function to add labels. The argument xaxt="n" inside plot() will make the graph without labels (digits) of the x axis.

 plot(quantity,xaxt="n") axis(1,at=1:3,labels=names(quantity)) 
+7
source

Are you looking for barplot ?

 barplot(quantity) 
+6
source

And another option with lattice :

 library(lattice) barchart(quantity) 

enter image description here

+3
source

You can use either barplot or ggplot2 and have a graph of the following

 quantity <- c(3, 5, 2) names(quantity) <- c("apples", "banans", "pears") barplot(quantity, main="Fruit Names vs. Quantity", xlab = "Names", ylab="Quantity", col=c("blue", "red", "yellow")) legend("topright", legend=c("apples", "banas", "pears"), fill=c("blue", "red", "yellow")) 

enter image description here

+1
source

Source: https://habr.com/ru/post/947948/


All Articles