![](/img/trans.png)
[英]Using coord_flip() with facet_wrap(scales = “free_y”) in ggplot2 seems to give unexpected facet axis tick marks and tick labels
[英]facet_wrap Title wrapping & Decimal places on free_y axis (ggplot2)
我有一组使用facet_wrap
生成多个图的代码:
ggplot(summ,aes(x=depth,y=expr,colour=bank,group=bank)) +
geom_errorbar(aes(ymin=expr-se,ymax=expr+se),lwd=0.4,width=0.3,position=pd) +
geom_line(aes(group=bank,linetype=bank),position=pd) +
geom_point(aes(group=bank,pch=bank),position=pd,size=2.5) +
scale_colour_manual(values=c("coral","cyan3", "blue")) +
facet_wrap(~gene,scales="free_y") +
theme_bw()
使用参考数据集,此代码生成如下图:
我试图在这里实现两个目标:
expr
值的新列,但它会导致误差线无法正确排列。facet_wrap
语句中的代码来包装文本?可能不能作为明确的答案,但这里有一些关于您的问题的提示:
首先,让我们尝试使用format
函数的直接解决方案。 在这里,我们将所有 y 轴刻度标签格式化为具有 1 个十进制值,然后使用round
对其进行round
。
formatter <- function(...){
function(x) format(round(x, 1), ...)
}
mtcars2 <- mtcars
sp <- ggplot(mtcars2, aes(x = mpg, y = qsec)) + geom_point() + facet_wrap(~cyl, scales = "free_y")
sp <- sp + scale_y_continuous(labels = formatter(nsmall = 1))
问题是,有时这种方法并不实用。 例如,从您的图中获取最左边的图。 使用相同的格式,所有 y 轴刻度标签将四舍五入为-0.3
,这不是可取的。
另一种解决方案是将每个图的中断点修改为一组四舍五入的值。 但同样,以图形最左边的图为例,它最终只有一个标签点, -0.3
另一种解决方案是将标签格式化为科学形式。 为简单起见,您可以修改formatter
函数如下:
formatter <- function(...){
function(x) format(x, ..., scientific = T, digit = 2)
}
现在您可以为所有绘图的 y 轴使用统一格式。 不过,我的建议是在四舍五入后将标签设置为小数点后两位。
这是可以做到用labeller
的参数facet_wrap
。
# Modify cyl into factors
mtcars2$cyl <- c("Four Cylinder", "Six Cylinder", "Eight Cylinder")[match(mtcars2$cyl, c(4,6,8))]
# Redraw the graph
sp <- ggplot(mtcars2, aes(x = mpg, y = qsec)) + geom_point() +
facet_wrap(~cyl, scales = "free_y", labeller = labeller(cyl = label_wrap_gen(width = 10)))
sp <- sp + scale_y_continuous(labels = formatter(nsmall = 2))
必须注意, wrap 函数检测空间以将标签分隔成行。 因此,在您的情况下,您可能需要修改变量。
这仅解决了问题的第一部分。 您可以创建一个函数来格式化轴并使用scale_y_continous
进行调整。
df <- data.frame(x=rnorm(11), y1=seq(2, 3, 0.1) + 10, y2=rnorm(11))
library(ggplot2)
library(reshape2)
df <- melt(df, 'x')
# Before
ggplot(df, aes(x=x, y=value)) + geom_point() +
facet_wrap(~ variable, scale="free")
# label function
f <- function(x){
format(round(x, 1), nsmall=1)
}
# After
ggplot(df, aes(x=x, y=value)) + geom_point() +
facet_wrap(~ variable, scale="free") +
scale_y_continuous(labels=f)
scale_*_continuous(..., labels = function(x) sprintf("%0.0f", x))
在我的情况下有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.