Closed bappa10085 closed 4 years ago
It does keep the x-axis free, see the corresponding from ggplot's, where I have removed the larges values for "ANN":
data_calibration <- subset(data_calibration, !(Method == "ANN" & Observed > 10000))
ggplot(data_calibration, aes(Observed,Predicted))+
geom_point(color="black",alpha = 1/3) +
facet_grid(Station ~ Method, scales="free") +
xlab("Measured") +
ylab("Predicted")+ theme_bw()+
geom_smooth(method="lm")
The x-axis is free to scale within each column. This behaviour is also replicated with lemon's facet_rep_grid
.
What you might be looking after is with facet_wrap
:
ggplot(data_calibration, aes(Observed,Predicted))+
geom_point(color="black",alpha = 1/3) +
facet_wrap(~ Station + Method, ncol=6, scales="free") +
xlab("Measured") +
ylab("Predicted")+ theme_bw()+
geom_smooth(method="lm")
Admittedly, it repeats the same row-strip text across all columns, which is not quite as pleasing as facet_grid
.
Is there a possibility to remove these repetitions in facet_wrap
?
I have solved this issue after taking help from this post.
First, create two plots using facet_grid
and facet_wrap
.
g1 = ggplot(data_calibration, aes(Observed,Predicted))+
geom_point(color="black",alpha = 1/3) +
facet_wrap(Station ~ Method, scales="free", ncol=6)+
xlab("Measured") +
ylab("Predicted")+ theme_bw()+
geom_smooth(method="lm")+
theme(strip.background = element_blank(),
strip.text = element_blank())
g2 = ggplot(data_calibration, aes(Observed,Predicted))+
geom_point(color="black",alpha = 1/3) +
facet_grid(Station ~ Method, scales="free")+
xlab("Measured") +
ylab("Predicted")+ theme_bw()+
geom_smooth(method="lm")
Now replace the top facet strips of g1 with those from g2
library(grid)
library(gtable)
gt1 = ggplot_gtable(ggplot_build(g1))
gt2 = ggplot_gtable(ggplot_build(g2))
gt1$grobs[grep('strip-t.+1$', gt1$layout$name)] = gt2$grobs[grep('strip-t', gt2$layout$name)]
grid.draw(gt1)
Add the right-hand panel strips
gt1 = gtable_add_cols(gt1, widths=gt1$widths[1], pos = -1)
panel_id <- gt1$layout[grep('panel-.+1$', gt1$layout$name),]
gt.side1 = gtable_filter(gt2, 'strip-r-1')
gt.side2 = gtable_filter(gt2, 'strip-r-2')
gt.side3 = gtable_filter(gt2, 'strip-r-3')
gt1 = gtable_add_grob(gt1, zeroGrob(), t = 1, l = ncol(gt1), b=nrow(gt1))
gt1 = gtable_add_grob(gt1, gt.side1, t = panel_id$t[1], l = ncol(gt1))
gt1 = gtable_add_grob(gt1, gt.side2, t = panel_id$t[2], l = ncol(gt1))
gt1 = gtable_add_grob(gt1, gt.side3, t = panel_id$t[3], l = ncol(gt1))
grid.newpage()
grid.draw(gt1)
It will be great if you can add this to your package after making it fairly general.
I would strongly advocate for incorporating this function in the package. The scales='free' is really useful but never implemented for facet_grid and is really causing a lot of headaches.
Still, this issue remains.
I have used
lemon
package withggplot2
for plotting multifaceted scatter plot with regression and confidence interval line using the following codeThis gives me following plot
You can see from the figure that y-axis is free but not the x-axis.
scales="free"
should make both x and y-axis free. How to solve this issue? Here is the dataset indput()
format.