问题
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
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.
Here is a reproducible example of mine
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)
I want to remove the gap between A
and B
factors. I need is to remove the unused values from x axis.
回答1:
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"))
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')
Well, a trick; hopefully it's what you want:
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'
.
回答2:
I had a similar thought to @alistaire but couldn't get ggplot
to relinquish enough control, so I went with cowplot::plot_grid()
.
I rearranged the factors by creating the order manually, with B
appearing before A
.
df$facet_order <- factor(paste0(df$direc,df$add), levels=c("B1", "A1", "B2", "A2", "B3", "A3", "B4", "A4"))
Creating the plots for A
and B
individually, then combining them using plot_grid
;
g1 <- ggplot(data=df[df$direc=="B",], aes(x=x, y=yy)) +
geom_point(size=5, col="red") +
geom_line(size=1.3, col="red") +
scale_y_log10(limits=c(1e-8,10), breaks = c(3e-7,1e-3,1e-1,1)) +
scale_x_continuous(expand = c(0, 0),breaks=c(seq(-40,40,10))) +
facet_wrap(~facet_order, ncol=1) +
coord_cartesian(xlim=c(-50,0)) + theme(axis.line.x=element_line(colour="red", size=1.5),
legend.position="none")
g2 <- ggplot(data=df[df$direc=="A",], aes(x=x, y=yy)) +
geom_point(size=5, col="blue") +
geom_line(size=1.3, col="blue") +
scale_y_log10(limits=c(1e-8,10), breaks = c(3e-7,1e-3,1e-1,1)) +
scale_x_continuous(expand = c(0, 0),breaks=c(seq(-40,40,10))) +
facet_wrap(~facet_order, ncol=1) +
coord_cartesian(xlim=c(0,50)) + theme(axis.text.y=element_blank(),
axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title.y=element_blank(),
axis.line.x=element_line(colour="blue", size=1.5),
legend.position="none")
library(cowplot)
g <- plot_grid(g1,g2,scale=1.05)
g
Depending on the output size you're after, you can mess with the size
argument to plot_grid
and have the x=0
label exactly overlap so that the data line up perfectly.
My suggestion (in the absence of simply not doing this) would be to do this for all the values of add
so that you can manually move the relative x axes so that there is no gap for any of them.
来源:https://stackoverflow.com/questions/34890341/remove-blank-lines-from-x-axis-in-ggplot2