8642

ggplot Integer Breaks on Facets

<h3>Question</h3>
library(tidyverse) df <- tibble(col1 = rep(c("A", "B"), 2), col2 = c(0.4, 0.7, 3, 9), col3 = c("I", "I", "II", "II")) #> # A tibble: 4 x 3 #> col1 col2 col3 #> <chr> <dbl> <chr> #> 1 A 0.4 I #> 2 B 0.7 I #> 3 A 3 II #> 4 B 9 II ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free")

I want to create integer breaks for the ggplot above such that:

<ol><li>There is an integer lower break at or below the lowest value value for each facet.</li> <li>There is an integer upper break at or above the highest value value for each facet.</li> </ol>

For my first facet I the integer values for the axis would include 0 and 1. For the second facet II the integer values should include at the min 0 and the max integer would have to be at least 9, maybe 10 would look better, depending on the routine used for creating the breaks.

These attempts from this older stackoverflow question don't quite work.

# Attempt 1 ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free") + scale_y_continuous( breaks = function(x) unique(floor(pretty(seq(0, (max(x) + 1) * 1.1))))) # Attempt 2 ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free") + scale_y_continuous(breaks = scales::pretty_breaks(2)) # Attempt 3 ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free") + scale_y_continuous(breaks = c(0, 1)) # Attempt 4 ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free") + scale_y_continuous( breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1)) # Attempt 5 ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free") + scale_y_continuous( breaks = function(x, n = 5) pretty(x, n)[round(pretty(x, n),1) %% 1 == 0])

Most attempts produce something like the following. Notice the missing 1 break on the first facet. And I'd want the second facet to have a break at 10. I don't want to manually set limits or breaks because in reality I have hundreds of facets. Hopefully one of the functions above can be modified to fit my requirements.


<h3>Answer1:</h3>

To achieve the desired result you also have to adjust the limits of the y-axis. Try this:

<pre class="lang-r prettyprint-override">library(tidyverse) df <- tibble(col1 = rep(c("A", "B"), 2), col2 = c(0.4, 0.7, 3, 9), col3 = c("I", "I", "II", "II")) my_ceil <- function(x) { ceil <- ceiling(max(x)) ifelse(ceil > 1 & ceil %% 2 == 1, ceil + 1, ceil) } my_breaks <- function(x) { ceil <- my_ceil(max(x)) unique(ceiling(pretty(seq(0, ceil)))) } my_limits <- function(x) { ceil <- my_ceil(x[2]) c(x[1], ceil) } ggplot(df, aes(col1, col2)) + geom_col() + facet_wrap(vars(col3), scales = "free") + scale_y_continuous(breaks = my_breaks, limits = my_limits)

<sup>Created on 2020-05-21 by the reprex package (v0.3.0)</sup>

来源:https://stackoverflow.com/questions/61915427/ggplot-integer-breaks-on-facets

Recommend

  • How to annotate Python function using return type of another function?
  • How to encode Java files in UTF-8 using Apache Ant?
  • RxJS Subscriber unsubscribe vs. complete
  • JAXB: Empty string does not produce empty element
  • Is there a particular naming convention for Java methods that throw exceptions?
  • Bcrypt vs Hash in laravel
  • Open StreetMap Reverse Geocoding
  • Mac app icon not displayed in itunes connect
  • Why does as.character() return an integer on a list of dates?
  • Angular 4 adding an image before each new line using renderer
  • changing URL path to local xml path in android xml parser
  • PHP for ; foreach variable scope
  • Updating TextView with clock
  • Ruby on Rails 4: Display Search Results on Search Results Page
  • Do geospatial queries work on arrays? ( $geoWithin, $geoIntersects )
  • Ruby json gem is encoding html entities
  • Is there a way to limit the duration of an audio capture in Codename One?
  • Plot KMeans clusters and classification for 1-dimensional data
  • jre_home environment variable is not defined correctly while starting tomcat
  • exception thrown while building the java application using netbeans
  • Dynamic Bootstrap Tabs using PHP/MySQL
  • Problem with installing Charm-Crypto for Python3
  • iterating through image folder using javascript and adding the result in HTML
  • Open a cmd program with full functionality (i/o)
  • Office.js mail object properties missing in TypeScript
  • Special characters (ë) in JSON-response
  • How to smoothly connect two signals in matlab [closed]
  • C# ICMPv6 checksum calculation
  • Querying Elasticsearch Address Based Index
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • Shiny - change the size (padding?) of dropdown menu (select tags) smaller
  • Controller or RestController
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • How to call jQuery function in HTML returned by AJAX
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • VSTS work items list through REST API
  • How to warp text around image in iOS?
  • multiple button click in asp.net MVC 3
  • Sql - ON DUPLICATE KEY UPDATE