如何设置条形图的x轴 [英] how to set the x-axis of barplot

查看:139
本文介绍了如何设置条形图的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经被多次质疑,就像这样一个

另一个例子,放置 5 个标签:

barplot(dt, names.arg=c(names(dt)[1:5], rep("", 2), names(dt)[8:13]))

编辑

如果你想按顺序放置标签(例如每5个),你可以这样做:

lab <- rep("", length(dt))实验室[seq(1, length(dt), by=5)] <-names(dt)[seq(1, length(dt), by=5)]条形图(DT,names.arg=lab)

This has been questioned many times like this one X axis in Barplot in R

however none helped me to solve my problem

dt<- structure(c(79L, 54L, 37L, 41L, 42L, 121L, 134L, 169L, 23L, 19L, 
22L, 19L, 25L), .Names = c("Experi_1", "Experi_2", "Experi_3", 
"Experi_4", "Experi_5", "Experi_6", "Experi_7", "Experi_8", "Experi_9", 
"Experi_10", "Experi_11", "Experi_12", "Experi_13"))

I plot my data like this

mydt<- barplot(dt)

if I do this , it overlay twice the x-axis

axis(side=1,at=mydt[1+c(1,13)],labels=c("Experi1","Experi13"))

It set few of the x-axis labels automatically. I want to do it manually, for example get only mention beginning of x-axis label and end . In this case Experi1 and Experi 13. I tried to understand ?axis and specially label but I could not set it to a costume as axis label as I want.

解决方案

Parameter names.arg of barplot function allows you to custom your labels the way you want. You just have to give the parameter a vector of name with the same length as the number of bars:

for example, putting only first and last:

barplot(dt, names.arg=c("Experi_1", rep("", 11), "Experi_13"))

another example, putting 5 labels:

barplot(dt, names.arg=c(names(dt)[1:5], rep("", 2), names(dt)[8:13]))

EDIT

if you want to put label by sequence (every 5 for the example), you can do:

lab <- rep("", length(dt))
lab[seq(1, length(dt), by=5)] <-names(dt)[seq(1, length(dt), by=5)]
barplot(dt, names.arg=lab)

这篇关于如何设置条形图的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆