Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add x and y axis to all facet_wrap

It is often desirable to minimize ink in a plot. I have a faceted plot (facet_wrap) and would like to remove as much ink as possible yet maintain readability. I have set it up as I'd like except the x and y axis is not present for the facets (subplots) unless on the far left or bottom. With so much ink removed I believe the eye needs these cues and am asking how to put the x and y axis in all plots within a facet_wrap. Below is my code thus far, the current output and the deired output (red lines are the desired add in):

library(ggplot); library(grid)  ggplot(mtcars, aes(mpg, hp)) +      geom_point() +      facet_wrap(~carb) +     theme(panel.grid = element_blank(),         panel.background = element_rect(fill = "white", colour = "black"),          panel.border = element_rect(fill = NA, colour = "white"),          axis.line = element_line(),         strip.background = element_blank(),         panel.margin = unit(2, "lines")) 

Current Plot enter image description here

Desired Plot enter image description here

like image 279
Tyler Rinker Avatar asked Mar 01 '14 15:03

Tyler Rinker


People also ask

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

How do I change the label on a facet grid?

Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

How do I change X label in ggplot2?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .


2 Answers

This should simplify things considerably:

library('ggthemes') ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') +      theme_tufte() + theme(axis.line=element_line()) +      scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400)) 

enter image description here

like image 166
Thomas Avatar answered Oct 15 '22 00:10

Thomas


easiest way would be to add segments in each plot panel,

ggplot(mtcars, aes(mpg, hp)) +    geom_point() +    facet_wrap(~carb) +   theme_minimal() +   annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf)+   annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf) 

example

like image 27
baptiste Avatar answered Oct 15 '22 00:10

baptiste