How do I show all boxplot labels

对着背影说爱祢 提交于 2021-02-16 04:20:26

问题


I've created a box plot, the data on the left is the continous variable and the data on the right has about 10 unique options. When I create the boxplot I cannot see the labels. How do I make it show all the labels, possibly vertically?

boxplot(data$Rate ~ as.factor(data$Purpose))

I've looked around and cannot work out what im trying to follow.


回答1:


You can add argument las=2 to function boxplot() to make all labels perpendicular to axis.

df<-data.frame(Rate=rnorm(100),Purpose=rep(letters[1:10],each=10))
boxplot(df$Rate~df$Purpose,las=2)

If your label names are long then you should adjust also plot margins.

par(mar=c(7,5,1,1))
boxplot(df$Rate~df$Purpose,las=2)



回答2:


When you are wanting to precisely specify your axes labels, here's the strategy I use:

##Generate a boxplot without axes
boxplot(count ~ spray, data = InsectSprays, axes=FALSE)

##Add in a y-axis
axis(2, seq(0,25, 5), seq(0, 25, 5))

##Add in an x-axis
##las=2 changes the orientation
axis(1, 1:6, paste("Big Label", 1:6), las=2)



回答3:


There is an entry in the R FAQ on how to rotate axis labels in base graphics :

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f

But I personaly would prefer to use ggplot2, which makes it easier :

data <- data.frame(Rate=rnorm(1:1000),Purpose=sample(c("foo","bar","baz"),1000,replace=TRUE))
ggplot(data, aes(x=factor(Purpose), y=Rate)) + geom_boxplot() + theme(axis.text.x  = element_text(angle=90, vjust=0.5))

enter image description here



来源:https://stackoverflow.com/questions/14872783/how-do-i-show-all-boxplot-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!