从ggplot2中的x轴删除空白行 [英] Remove blank lines from x axis in ggplot2

查看:240
本文介绍了从ggplot2中的x轴删除空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在删除x轴值之间的间隔时遇到了一些麻烦. 我想删除每个facet_wrap

I have a some trouble about removing the space in between x axis values. I want to remove the gap between two values in each facet_wrap

我检查了此帖子 /ggplot-geom-tile-spacing-with-facets 和这一个 remove-blank-lines-from-plot-geom-tile -ggplot ,但scales=free_x根本没有帮助.

I checked this post /ggplot-geom-tile-spacing-with-facets and this one remove-blank-lines-from-plot-geom-tile-ggplot but scales=free_x didnt helped at all.

这是我的可复制示例

set.seed(123)
library(ggplot2)

x <-  rep(c(seq(2,40,length.out=8),seq(-2,-40,length.out=8)),times=1);
yy <- replicate(1,c(sort(10^runif(8,-9,1),decreasing=TRUE), sort(10^runif(8,-6,1),decreasing=TRUE),sort(10^runif(8,-3,0),decreasing=TRUE)))
direc <- rep(rep(c("A","B"),each=8),times=6)
add <-rep(seq(1:4),each=12)
df <- data.frame(x,yy,direc,add)

ggplot(data = df, aes(x = x, y = yy, colour=direc)) +
  geom_point(size=5)+
  geom_line(size=1.3)+
  scale_y_log10(limits = c(1e-7,1),breaks = c(3e-7,1e-3,1e-1,1))+
  scale_x_continuous(expand = c(0, 0),breaks=c(seq(-40,40,10)))+
  #scale_x_discrete(expand=c(0,0),drop=F)+
  #facet_grid(FIELD.Oe. ~ WER)+
  facet_wrap(~add)

我想消除AB因素之间的差距.我需要从x轴上删除未使用的值.

I want to remove the gap between A and B factors. I need is to remove the unused values from x axis.

推荐答案

如果将direc转换为水平c('B', 'A')的因数(以左侧的B排列构面),

If you turn direc into a factor with levels c('B', 'A') (to arrange facets with B on the left),

df$direc <- factor(direc, levels = c("B", "A"))

仅用facet_grid替换facet_wrap即可达到目的.

just replacing facet_wrap with a facet_grid does the trick.

ggplot(data = df, aes(x = x, y = yy, colour = direc)) +
  geom_point(size=5)+
  geom_line(size=1.3)+
  scale_y_log10(limits = c(1e-7,1),breaks = c(3e-7,1e-3,1e-1,1))+
  facet_grid(add ~ direc, scales = 'free')

好吧, a 技巧;希望这就是您想要的:

Well, a trick; hopefully it's what you want:

colour自变量在技术上也不需要这种安排,尽管它不会引起任何问题.我也删除了scale_x_continuous,因为它被scales = 'free'覆盖.

The colour argument is also technically unnecessary with this arrangement, though it doesn't cause any issues. I took out scale_x_continuous, as well, because it gets overridden by scales = 'free'.

这篇关于从ggplot2中的x轴删除空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆