gpt4 book ai didi

r - ggplot2 分面边距

转载 作者:行者123 更新时间:2023-12-04 01:29:50 25 4
gpt4 key购买 nike

我用 facet_wrap绘制一些数据。下面是一个例子:

library (ggplot2)
library (reshape)

# generate some dummy data
x = seq(0,1,0.05)
precision = sqrt(x)
recall = 1 - precision
fmeasure = 2 * (precision * recall) / (precision + recall)

# prepare it for plotting
df = data.frame(x=x, precision=precision, recall=recall, fmeasure=fmeasure)
df = melt(df, id.vars=c(x))

# plot it
p = ggplot(df, aes(x=x, y=value, group=variable))
p = p + geom_line() + facet_wrap(~variable, ncol=3)
p = p + coord_cartesian(xlim=c(0,1), ylim=c(0,1)) # second plot is without this line
print (p)

图 1:绘制上述代码。
Plot with xlim and ylim

但是,您在图 1 中看到的是后续方面的第一个和最后一个标签重叠。这可以通过增加刻面之间的空间来解决。其他选项是删除 xlimylim范围如图 2 所示,但这会在刻面本身中保留不必要的空间。

图 2:绘制线 p = p + coord_cartesian(xlim=c(0,1), ylim=c(0,1))移除。
Plot without xlim and ylim

我试图增加刻面之间的空间,但到目前为止我一直无法做到。你有什么建议吗?

我使用 ggplot2 版本 0.9.1 。

最佳答案

对于 0.9.1 使用:p + opts(panel.margin = unit(2, "lines"))但是你有很多额外的空白,IMO 失去了一些刻面的效果(注意 0.9.2 现在使用 theme 而不是 opts )

多年来,ggplot2 API 发生了变化,截至 2018-02-01,这是更新的解决方案:

p + theme(panel.spacing = unit(2, "lines"))

关于r - ggplot2 分面边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com