Home > database >  Add breaks to square root transformed x-axis ggplot2
Add breaks to square root transformed x-axis ggplot2

Time:07-25

I made a histogram with ggplot and used scale_x_sqrt to square root transform the x-axis as the data is heavily skewed. The data ranges from about 40 to 15000 and I would like to include breaks at 50, 100, 1000, 5000, 10000 and 15000. However, when I include these values as breaks, ggplot ignores everything under 1000. Since most of the data is between 50 - 100, these labels are the most interesting. How can I force ggplot to include these breaks anyway?

dput:

structure(list(data = structure(list(mean_dist = c(150.411215146272, 
175.1801917413, 129.198766726427, 494.517337587811, 4004.5314885339, 
121.252791598999, 553.145127185951, 147.713473938297, 245.020312907887, 
482.846777092631, 198.494360295811, 354.524572639398, 78.6053630858533, 
216.541809731071, 216.541809731071, 486.634147875947, 13335.9896040513, 
110.782379790446, 164.543403351254, 98.0983760251435, 208.771439268054, 
116.099043793849, 117.834466860662, 372.753602132152, 239.35765761374, 
114.398698636631, 138.457691738871, 239.35765761374, 999.095619603017, 
3563.66126100038, 316.548964886711, 122.52536628287, 68.3315401057944, 
119.782844028692, 316.548964886711, 221.543536168436, 45.2766082294286, 
2678.26403875858, 44.4877747339358, 4311.89215730919, 45.2766082294286, 
144.222645025699, 136.301013276485, 83.6557775669878, 38.0629739397109, 
186.563007050923, 198.584509982312, 147.437459127145, 251.314335958325, 
112.025548769548, 568.339911858175, 197.721036856221, 1063.92959986003, 
82.0580339180257, 277.27345010557)), class = "data.frame", row.names = c(NA, 
-55L)), layers = list(<environment>), scales = <environment>, 
    mapping = structure(list(x = ~mean_dist), class = "uneval"), 
    theme = structure(list(line = structure(list(colour = "black", 
        size = 0.5, linetype = 1, lineend = "butt", arrow = FALSE, 
        inherit.blank = TRUE), class = c("element_line", "element"
    )), rect = structure(list(fill = "white", colour = "black", 
        size = 0.5, linetype = 1, inherit.blank = TRUE), class = c("element_rect", 
    "element")), text = structure(list(family = "", face = "plain", 
        colour = "black", size = 11, hjust = 0.5, vjust = 0.5, 
        angle = 0, lineheight = 0.9, margin = structure(c(0, 
        0, 0, 0), unit = 8L, class = c("margin", "simpleUnit", 
        "unit", "unit_v2")), debug = FALSE, inherit.blank = TRUE), class = c("element_text", 
    "element")), title = NULL, aspect.ratio = NULL, axis.title = NULL, 
        axis.title.x = structure(list(family = NULL, face = NULL, 
            colour = NULL, size = NULL, hjust = NULL, vjust = 1, 
            angle = NULL, lineheight = NULL, margin = structure(c(2.75, 
            0, 0, 0), unit = 8L, class = c("margin", "simpleUnit", 
            "unit", "unit_v2")), debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.title.x.top = structure(list(family = NULL, 
            face = NULL, colour = NULL, size = NULL, hjust = NULL, 
            vjust = 0, angle = NULL, lineheight = NULL, margin = structure(c(0, 
            0, 2.75, 0), unit = 8L, class = c("margin", "simpleUnit", 
            "unit", "unit_v2")), debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.title.x.bottom = NULL, axis.title.y = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = NULL, vjust = 1, angle = 90, lineheight = NULL, 
            margin = structure(c(0, 2.75, 0, 0), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.title.y.left = NULL, axis.title.y.right = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = NULL, vjust = 0, angle = -90, lineheight = NULL, 
            margin = structure(c(0, 0, 0, 2.75), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.text = structure(list(family = NULL, 
            face = NULL, colour = "grey30", size = structure(0.8, class = "rel"), 
            hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL, 
            margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.text.x = structure(list(family = NULL, 
            face = NULL, colour = NULL, size = NULL, hjust = NULL, 
            vjust = 1, angle = NULL, lineheight = NULL, margin = structure(c(2.2, 
            0, 0, 0), unit = 8L, class = c("margin", "simpleUnit", 
            "unit", "unit_v2")), debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.text.x.top = structure(list(family = NULL, 
            face = NULL, colour = NULL, size = NULL, hjust = NULL, 
            vjust = 0, angle = NULL, lineheight = NULL, margin = structure(c(0, 
            0, 2.2, 0), unit = 8L, class = c("margin", "simpleUnit", 
            "unit", "unit_v2")), debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.text.x.bottom = NULL, axis.text.y = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = 1, vjust = NULL, angle = NULL, lineheight = NULL, 
            margin = structure(c(0, 2.2, 0, 0), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.text.y.left = NULL, axis.text.y.right = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = 0, vjust = NULL, angle = NULL, lineheight = NULL, 
            margin = structure(c(0, 0, 0, 2.2), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), axis.ticks = structure(list(colour = "grey20", 
            size = NULL, linetype = NULL, lineend = NULL, arrow = FALSE, 
            inherit.blank = TRUE), class = c("element_line", 
        "element")), axis.ticks.x = NULL, axis.ticks.x.top = NULL, 
        axis.ticks.x.bottom = NULL, axis.ticks.y = NULL, axis.ticks.y.left = NULL, 
        axis.ticks.y.right = NULL, axis.ticks.length = structure(2.75, unit = 8L, class = c("simpleUnit", 
        "unit", "unit_v2")), axis.ticks.length.x = NULL, axis.ticks.length.x.top = NULL, 
        axis.ticks.length.x.bottom = NULL, axis.ticks.length.y = NULL, 
        axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, 
        axis.line = structure(list(colour = "black", size = structure(1, class = "rel"), 
            linetype = NULL, lineend = NULL, arrow = FALSE, inherit.blank = TRUE), class = c("element_line", 
        "element")), axis.line.x = NULL, axis.line.x.top = NULL, 
        axis.line.x.bottom = NULL, axis.line.y = NULL, axis.line.y.left = NULL, 
        axis.line.y.right = NULL, legend.background = structure(list(
            fill = NULL, colour = NA, size = NULL, linetype = NULL, 
            inherit.blank = TRUE), class = c("element_rect", 
        "element")), legend.margin = structure(c(5.5, 5.5, 5.5, 
        5.5), unit = 8L, class = c("margin", "simpleUnit", "unit", 
        "unit_v2")), legend.spacing = structure(11, unit = 8L, class = c("simpleUnit", 
        "unit", "unit_v2")), legend.spacing.x = NULL, legend.spacing.y = NULL, 
        legend.key = structure(list(), class = c("element_blank", 
        "element")), legend.key.size = structure(1.2, unit = 3L, class = c("simpleUnit", 
        "unit", "unit_v2")), legend.key.height = NULL, legend.key.width = NULL, 
        legend.text = structure(list(family = NULL, face = NULL, 
            colour = NULL, size = structure(0.8, class = "rel"), 
            hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL, 
            margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), legend.text.align = NULL, legend.title = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = 0, vjust = NULL, angle = NULL, lineheight = NULL, 
            margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), legend.title.align = NULL, legend.position = "right", 
        legend.direction = NULL, legend.justification = "center", 
        legend.box = NULL, legend.box.just = NULL, legend.box.margin = structure(c(0, 
        0, 0, 0), unit = 1L, class = c("margin", "simpleUnit", 
        "unit", "unit_v2")), legend.box.background = structure(list(), class = c("element_blank", 
        "element")), legend.box.spacing = structure(11, unit = 8L, class = c("simpleUnit", 
        "unit", "unit_v2")), panel.background = structure(list(
            fill = "white", colour = NA, size = NULL, linetype = NULL, 
            inherit.blank = TRUE), class = c("element_rect", 
        "element")), panel.border = structure(list(), class = c("element_blank", 
        "element")), panel.spacing = structure(5.5, unit = 8L, class = c("simpleUnit", 
        "unit", "unit_v2")), panel.spacing.x = NULL, panel.spacing.y = NULL, 
        panel.grid = structure(list(colour = "grey92", size = NULL, 
            linetype = NULL, lineend = NULL, arrow = FALSE, inherit.blank = TRUE), class = c("element_line", 
        "element")), panel.grid.major = structure(list(), class = c("element_blank", 
        "element")), panel.grid.minor = structure(list(), class = c("element_blank", 
        "element")), panel.grid.major.x = NULL, panel.grid.major.y = NULL, 
        panel.grid.minor.x = NULL, panel.grid.minor.y = NULL, 
        panel.ontop = FALSE, plot.background = structure(list(
            fill = NULL, colour = "white", size = NULL, linetype = NULL, 
            inherit.blank = TRUE), class = c("element_rect", 
        "element")), plot.title = structure(list(family = NULL, 
            face = NULL, colour = NULL, size = structure(1.2, class = "rel"), 
            hjust = 0, vjust = 1, angle = NULL, lineheight = NULL, 
            margin = structure(c(0, 0, 5.5, 0), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), plot.title.position = "panel", plot.subtitle = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = 0, vjust = 1, angle = NULL, lineheight = NULL, 
            margin = structure(c(0, 0, 5.5, 0), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), plot.caption = structure(list(family = NULL, 
            face = NULL, colour = NULL, size = structure(0.8, class = "rel"), 
            hjust = 1, vjust = 1, angle = NULL, lineheight = NULL, 
            margin = structure(c(5.5, 0, 0, 0), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), plot.caption.position = "panel", plot.tag = structure(list(
            family = NULL, face = NULL, colour = NULL, size = structure(1.2, class = "rel"), 
            hjust = 0.5, vjust = 0.5, angle = NULL, lineheight = NULL, 
            margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), plot.tag.position = "topleft", plot.margin = structure(c(5.5, 
        5.5, 5.5, 5.5), unit = 8L, class = c("margin", "simpleUnit", 
        "unit", "unit_v2")), strip.background = structure(list(
            fill = "white", colour = "black", size = structure(2, class = "rel"), 
            linetype = NULL, inherit.blank = TRUE), class = c("element_rect", 
        "element")), strip.background.x = NULL, strip.background.y = NULL, 
        strip.placement = "inside", strip.text = structure(list(
            family = NULL, face = NULL, colour = "grey10", size = structure(0.8, class = "rel"), 
            hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL, 
            margin = structure(c(4.4, 4.4, 4.4, 4.4), unit = 8L, class = c("margin", 
            "simpleUnit", "unit", "unit_v2")), debug = NULL, 
            inherit.blank = TRUE), class = c("element_text", 
        "element")), strip.text.x = NULL, strip.text.y = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = NULL, vjust = NULL, angle = -90, lineheight = NULL, 
            margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element")), strip.switch.pad.grid = structure(2.75, unit = 8L, class = c("simpleUnit", 
        "unit", "unit_v2")), strip.switch.pad.wrap = structure(2.75, unit = 8L, class = c("simpleUnit", 
        "unit", "unit_v2")), strip.text.y.left = structure(list(
            family = NULL, face = NULL, colour = NULL, size = NULL, 
            hjust = NULL, vjust = NULL, angle = 90, lineheight = NULL, 
            margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text", 
        "element"))), class = c("theme", "gg"), complete = TRUE, validate = TRUE), 
    coordinates = <environment>, facet = <environment>, plot_env = <environment>, 
    labels = list(x = "mean_dist", y = structure("count", fallback = TRUE), 
        weight = structure("weight", fallback = TRUE))), class = c("gg", 
"ggplot"))

And my code with the data I used:

dat <- data.frame(x = c(150.411215146272,
         175.1801917413,
         129.198766726427,
         494.517337587811,
         4004.5314885339,
         121.252791598999,
         553.145127185951,
         147.713473938297,
         245.020312907887,
         482.846777092631,
         198.494360295811,
         354.524572639398,
         78.6053630858533,
         216.541809731071,
         216.541809731071,
         486.634147875947,
         13335.9896040513,
         110.782379790446,
         164.543403351254,
         98.0983760251435,
         208.771439268054,
         116.099043793849,
         117.834466860662,
         372.753602132152,
         239.35765761374,
         114.398698636631,
         138.457691738871,
         239.35765761374,
         999.095619603017,
         3563.66126100038,
         316.548964886711,
         122.52536628287,
         68.3315401057944,
         119.782844028692,
         316.548964886711,
         221.543536168436,
         45.2766082294286,
         2678.26403875858,
         44.4877747339358,
         4311.89215730919,
         45.2766082294286,
         144.222645025699,
         136.301013276485,
         83.6557775669878,
         38.0629739397109,
         186.563007050923,
         198.584509982312,
         147.437459127145,
         251.314335958325,
         112.025548769548,
         568.339911858175,
         197.721036856221,
         1063.92959986003,
         82.0580339180257,
         277.27345010557))

ggplot(dat, aes(x = x))   
  geom_histogram(position = "dodge",
                 bins = 7,
                 fill = NA,
                 col = "black",
                 size = 1)  
  scale_x_sqrt(breaks = c(50, 100, 1000, 5000, 10000, 15000))  
  theme_classic()

enter image description here

CodePudding user response:

You need to add limits to cover the values in the lower bins:

library(ggplot2)

ggplot(dat, aes(x = x))   
  geom_histogram(position = "dodge",
                 bins = 7,
                 fill = NA,
                 col = "black",
                 size = 1)  
  scale_x_sqrt(breaks = c(50, 100, 1000, 5000, 10000, 15000),
               limits = c(0, 20000))  
  theme_classic()

enter image description here

  • Related