Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

only one margin of facet_grid

Tags:

r

ggplot2

I would like to use facet grid to display only the right margin not the bottom margin.

Example Code

library(ggplot2)
qplot(mpg, wt, data=mtcars) + facet_grid(cyl ~ vs, margins=TRUE) 

Currently Outcome enter image description here

Desired Outcome enter image description here

like image 895
Tyler Rinker Avatar asked Mar 13 '12 22:03

Tyler Rinker


People also ask

What is an advantage of using Facet_grid instead of Facet_wrap?

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.

What does the function Facet_grid () do?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap() .

How can I change my facet order?

You can use the following basic syntax to specify the order of facets in ggplot2: p + facet_grid(~factor(my_variable, levels=c('val1', 'val2', 'val3', ...))) The following example shows how to use this syntax in practice.

What is a facet grid?

A FacetGrid is a multi-axes grid with subplots visualizing the distribution of variables of a dataset and the relationship between multiple variables.


1 Answers

qplot(mpg, wt, data=mtcars) + facet_grid(cyl ~ vs, margins="vs") 

enter image description here


When this answer was written, this behavior was not documented. Now, it is in the facet_grid documentation

margins either a logical value or a character vector. Margins are additional facets which contain all the data for each of the possible values of the faceting variables. If FALSE, no additional facets are included (the default). If TRUE, margins are included for all faceting variables. If specified as a character vector, it is the names of variables for which margins are to be created.

(Thanks to @Harry for pointing this out.)

like image 62
Brian Diggs Avatar answered Sep 23 '22 00:09

Brian Diggs