"axis" 不会将 x 轴添加到箱线图
"axis" won't add x-axis to boxplot
我正在尝试制作带有自定义轴标签的箱线图,但我似乎无法向图中添加 x 轴。
例如:
test <- data.frame(year=as.integer(rep(1963:2014, each=10)),
response=rnorm(520))
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="",
range=0, xaxt="n", yaxt="n")
responselabs <- as.numeric(c(-3:3, by=1))
yearlabs <- as.integer(seq(1965,2015, by=5))
axis(2, at=responselabs, tck=0.03, las=1)
axis(1, at=yearlabs, tck=0.03)
returns 箱线图,但没有 x 轴标签:
尝试通过先制作一个空图来破解它,我可以获得坐标轴,但它不会添加箱线图:
plot(NA, ylim=c(-3, 3), xlim=c(1962, 2015), xaxt="n", yaxt="n", ylab="", xlab="")
axis(2, at=responselabs, tck=0.03, las=1)
axis(1, at=yearlabs, tck=0.03)
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="",
range=0, xaxt="n", yaxt="n", add=T)
这是怎么回事?
为什么不用ggplot2?
library(ggplot2)
p<-ggplot(test,aes(x=year,y=response,group=year))+
geom_boxplot()+
scale_x_continuous(breaks=round(seq(min(test$year),max(test$year),by=5),1))
如果您想四舍五入到最接近的 5,那么在 scale_x_continuous()
参数中调整代码相当容易。
p<-ggplot(test,aes(x=year,y=response,group=year))+
geom_boxplot()+
scale_x_continuous(breaks = round(seq(round(min(test$year)/5,0)*5,round(max(test$year)/5,0)*5, by = 5),1))
我认为发生的事情是 boxplot
将 year
转换为 factor
。我们可以通过在 axis
:
中使用 labels
参数来解决这个问题
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="",
range=0, xaxt="n", yaxt="n")
responselabs <- as.numeric(c(-3:3, by=1))
yearlabs <- as.integer(seq(1965,2015, by=5))
axis(2, at=responselabs, tck=0.03, las=1)
axis(1, at = yearlabs - 1962, labels = yearlabs)
正如其他人所说,您可能混淆了 x-axis 的基础值。玩弄 abline()
会发现 abline(v=2010)
不会出现,但 abline(v=50)
之类的东西会出现。
我相信这是你想要的情节(有一些额外的修改):
year_vals <- as.integer(rep(1963:2014, each=10)) # pulling out of next line for easy reference
test <- data.frame(year=year_vals,response=rnorm(520))
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="", range=0, xaxt="n", yaxt="n")
responselabs <- as.numeric(c(-3:3, by=1))
yearlabs <- year_vals[year_vals%%5==0] # alternate, more general definition; note that 2015 wasn't in the original 'test' data.frame #as.integer(seq(1965,2015, by=5))
axis(2, at=responselabs, tck=0.03, las=1)
# axis(1, at=1:length(yearlabs), tck=0.03)
axis(1, at=which(unique(year_vals)%in%yearlabs), labels=unique(yearlabs), tck=0.03)
我正在尝试制作带有自定义轴标签的箱线图,但我似乎无法向图中添加 x 轴。
例如:
test <- data.frame(year=as.integer(rep(1963:2014, each=10)),
response=rnorm(520))
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="",
range=0, xaxt="n", yaxt="n")
responselabs <- as.numeric(c(-3:3, by=1))
yearlabs <- as.integer(seq(1965,2015, by=5))
axis(2, at=responselabs, tck=0.03, las=1)
axis(1, at=yearlabs, tck=0.03)
returns 箱线图,但没有 x 轴标签:
尝试通过先制作一个空图来破解它,我可以获得坐标轴,但它不会添加箱线图:
plot(NA, ylim=c(-3, 3), xlim=c(1962, 2015), xaxt="n", yaxt="n", ylab="", xlab="")
axis(2, at=responselabs, tck=0.03, las=1)
axis(1, at=yearlabs, tck=0.03)
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="",
range=0, xaxt="n", yaxt="n", add=T)
这是怎么回事?
为什么不用ggplot2?
library(ggplot2)
p<-ggplot(test,aes(x=year,y=response,group=year))+
geom_boxplot()+
scale_x_continuous(breaks=round(seq(min(test$year),max(test$year),by=5),1))
如果您想四舍五入到最接近的 5,那么在 scale_x_continuous()
参数中调整代码相当容易。
p<-ggplot(test,aes(x=year,y=response,group=year))+
geom_boxplot()+
scale_x_continuous(breaks = round(seq(round(min(test$year)/5,0)*5,round(max(test$year)/5,0)*5, by = 5),1))
我认为发生的事情是 boxplot
将 year
转换为 factor
。我们可以通过在 axis
:
labels
参数来解决这个问题
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="",
range=0, xaxt="n", yaxt="n")
responselabs <- as.numeric(c(-3:3, by=1))
yearlabs <- as.integer(seq(1965,2015, by=5))
axis(2, at=responselabs, tck=0.03, las=1)
axis(1, at = yearlabs - 1962, labels = yearlabs)
正如其他人所说,您可能混淆了 x-axis 的基础值。玩弄 abline()
会发现 abline(v=2010)
不会出现,但 abline(v=50)
之类的东西会出现。
我相信这是你想要的情节(有一些额外的修改):
year_vals <- as.integer(rep(1963:2014, each=10)) # pulling out of next line for easy reference
test <- data.frame(year=year_vals,response=rnorm(520))
boxplot(response~year, data=test, ylim=c(-3,3), xlab="", ylab="", range=0, xaxt="n", yaxt="n")
responselabs <- as.numeric(c(-3:3, by=1))
yearlabs <- year_vals[year_vals%%5==0] # alternate, more general definition; note that 2015 wasn't in the original 'test' data.frame #as.integer(seq(1965,2015, by=5))
axis(2, at=responselabs, tck=0.03, las=1)
# axis(1, at=1:length(yearlabs), tck=0.03)
axis(1, at=which(unique(year_vals)%in%yearlabs), labels=unique(yearlabs), tck=0.03)