简体   繁体   中英

In a barplot in R, is there a way to choose which labels will be shown in X axis?

I have data to be shown on a barplot, with groups varying between 1 and 50. However, not all labels appear on X axis. I really don't want all of it, but from 5 to 5. Can I choose this? R is naming bars on a random order (sometimes from 4 to 4, sometimes 3 to 3).

Here is a simplified example of four values. First, you need to create a vector of X labels and assign those values to missing that you wont to show. Here I want to just show the first and third labels on the X axis:

data <- c(5, 7, 3, 9)
labels <- c(1,NA, 3, NA)

then plot the data using the barplot function and choose labels using names.arg option:

barplot(data, xticks = 1:4, names.arg = labels)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM