Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution that avoids editing your data:</p> <p>Say your plot is facetted by the <code>group</code> part of your dataframe, which has levels <code>control, test1, test2</code>, then create a list named by those values:</p> <pre><code>hospital_names &lt;- list( 'Hospital#1'="Some Hospital", 'Hospital#2'="Another Hospital", 'Hospital#3'="Hospital Number 3", 'Hospital#4'="The Other Hospital" ) </code></pre> <p>Then create a 'labeller' function, and push it into your facet_grid call:</p> <pre><code>hospital_labeller &lt;- function(variable,value){ return(hospital_names[value]) } ggplot(survey,aes(x=age)) + stat_bin(aes(n=nrow(h3),y=..count../n), binwidth=10) + facet_grid(hospital ~ ., labeller=hospital_labeller) ... </code></pre> <p>This uses the levels of the data frame to index the hospital_names list, returning the list values (the correct names).</p> <hr> <p>Please note that this only works if you only have one faceting variable. If you have two facets, then your labeller function needs to return a different name vector for each facet. You can do this with something like :</p> <pre><code>plot_labeller &lt;- function(variable,value){ if (variable=='facet1') { return(facet1_names[value]) } else { return(facet2_names[value]) } } </code></pre> <p>Where <code>facet1_names</code> and <code>facet2_names</code> are pre-defined lists of names indexed by the facet index names ('Hostpital#1', etc.).</p> <hr> <p><strong>Edit:</strong> The above method fails if you pass a variable/value combination that the labeller doesn't know. You can add a fail-safe for unknown variables like this:</p> <pre><code>plot_labeller &lt;- function(variable,value){ if (variable=='facet1') { return(facet1_names[value]) } else if (variable=='facet2') { return(facet2_names[value]) } else { return(as.character(value)) } } </code></pre> <hr> <p>Answer adapted from <a href="https://stackoverflow.com/questions/2893005/how-to-change-strip-text-labels-in-ggplot-with-facet-and-margin-true">how to change strip.text labels in ggplot with facet and margin=TRUE</a></p> <hr> <p><del>edit: <strong>WARNING</strong>: if you're using this method to facet by a <em>character</em> column, you may be getting incorrect labels. See <a href="https://github.com/hadley/ggplot2/issues/1070" rel="noreferrer">this bug report</a>.</del> fixed in recent versions of ggplot2.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload