R 使用geom_箱线图的系数时更改x轴限制
当使用因子作为x轴比例时,是否可以将x轴限制两侧增加1。例如,在下图中,是否可以将xlimits更改为3和9?i、 e.R 使用geom_箱线图的系数时更改x轴限制,r,ggplot2,R,Ggplot2,当使用因子作为x轴比例时,是否可以将x轴限制两侧增加1。例如,在下图中,是否可以将xlimits更改为3和9?i、 e.xlim(3,9) 当我尝试scale\x\u continuous(limits=c(3,9))时,我得到: Error in if (zero_range(from) || zero_range(to)) return(rep(mean(to), : missing value where TRUE/FALSE needed In addition: Warning
xlim(3,9)
当我尝试scale\x\u continuous(limits=c(3,9))
时,我得到:
Error in if (zero_range(from) || zero_range(to)) return(rep(mean(to), :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
no non-missing arguments to min; returning Inf
2: In loop_apply(n, do.ply) :
no non-missing arguments to max; returning -Inf
3: In loop_apply(n, do.ply) :
position_dodge requires constant width: output may be incorrect
您需要
缩放x\u离散
:
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() + scale_x_discrete(limits = c("4","6"))
编辑:要求包括更广泛的范围。要包含其他值,您需要在因子列表上使用breaks函数:
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() +
scale_x_discrete(breaks = factor(3:9), limits = c("3", "4", "5", "6", "7", "8", "9"))
或在打印前将其过滤掉:
mtcars<-mtcars[mtcars$cyl!=8,]
mtcar是否可以使限值低于4(3)并高于8(9),而不会出现错误?这样做的原因是将其与条形图对齐。
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() + xlim("4", "6")
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() +
scale_x_discrete(breaks = factor(3:9), limits = c("3", "4", "5", "6", "7", "8", "9"))
p + geom_boxplot() +
scale_x_discrete(breaks=factor(c("3","4","5","6","7", "eight", "9")),
limits = c("3", "4", "5", "6", "7", "eight", "9")
mtcars<-mtcars[mtcars$cyl!=8,]