4

Here is the code:

xbreaks <- c(100, 200, 300)
ybreaks <- c(2, 3, 4, 5)
ggplot(mtcars, aes(hp, wt)) +
  scale_x_continuous(breaks=xbreaks) +
  scale_y_continuous(breaks=ybreaks) +
  geom_point()

Here is the plot:

enter image description here

How can I find the X and Y values corresponding to the bottom left-hand corner (which I've marked with a green dot)? I guess that they are approximately (40, 1.40), but can I ask R for the exact values?

Nick Brown
  • 81
  • 5
  • Also see https://stackoverflow.com/questions/7705345/how-can-i-extract-plot-axes-ranges-for-a-ggplot2-object/35372274#answer-40304848 – lukeA May 29 '18 at 23:07

3 Answers3

6

Yes you can! Try this:

g <- ggplot(mtcars, aes(hp, wt)) +
  scale_x_continuous(breaks=xbreaks) +
  scale_y_continuous(breaks=ybreaks) +
  geom_point()

b <- ggplot_build(g)
b$layout$panel_ranges[[1]]$x.range

[1]  37.85 349.15
thc
  • 9,527
  • 1
  • 24
  • 39
1

Do you want the coordinates or do you want to plot something there? To plot something there, you can use -Inf (or Inf for the opposite site) as x and y coordinates:

library(ggplot2)
xbreaks <- c(100, 200, 300)
ybreaks <- c(2, 3, 4, 5)
ggplot(mtcars, aes(hp, wt)) +
  scale_x_continuous(breaks=xbreaks) +
  scale_y_continuous(breaks=ybreaks) +
  geom_point() +
  geom_point(aes(x = -Inf, y = -Inf), color = "blue", inherit.aes = FALSE)

Notice the blue dot in the lower left corner. It is clipped by default, but you can switch off clipping to show the entire dot:

ggplot(mtcars, aes(hp, wt)) +
  scale_x_continuous(breaks=xbreaks) +
  scale_y_continuous(breaks=ybreaks) +
  geom_point() +
  geom_point(aes(x = -Inf, y = -Inf), color = "blue", inherit.aes = FALSE) +
  coord_cartesian(clip = "off")

Created on 2018-05-29 by the reprex package (v0.2.0).

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
1

Note that as of ggplot2 2.2.1.9000, the parameter within layout is changed from panel_ranges but panel_params.

The current ggplot (stable) 2.2.1 version uses layout$panel_ranges the upstream version 2.2.1.9000 on Github now uses layout$panel_params

Example Code and console output below:

Updated ggplot2

require("devtools")
devtools::install_git("git@github.com:tidyverse/ggplot2.git")

data(mtcars)
xbreaks <- c(100, 200, 300)
ybreaks <- c(2, 3, 4, 5)
g <- ggplot(mtcars, aes(hp, wt)) +
  scale_x_continuous(breaks=xbreaks) +
  scale_y_continuous(breaks=ybreaks) +
  geom_point()

b <- ggplot_build(g)
b$layout$panel_ranges[[1]]$x.range


g <- ggplot(mtcars, aes(hp, wt)) +
  scale_x_continuous(breaks=xbreaks) +
  scale_y_continuous(breaks=ybreaks) +
  geom_point()

b <- ggplot_build(g)
b$layout$panel_params[[1]]$x.range

Console Output:

> require("devtools")
Loading required package: devtools
> devtools::install_git("git@github.com:tidyverse/ggplot2.git")
Downloading git repo git@github.com:tidyverse/ggplot2.git
Installing ggplot2
'/usr/local/Cellar/r/3.5.0_1/lib/R/bin/R' --no-site-file --no-environ --no-save  \
  --no-restore --quiet CMD INSTALL  \
  '/private/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gp/T/RtmpwOWMNj/filee8386283a116'  \
  --library='/usr/local/lib/R/3.5/site-library' --install-tests 

* installing *source* package ‘ggplot2’ ...
** R
** data
*** moving datasets to lazyload DB
** inst
** tests
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (ggplot2)
Reloading installed ggplot2
> data(mtcars)
> xbreaks <- c(100, 200, 300)
> ybreaks <- c(2, 3, 4, 5)
> g <- ggplot(mtcars, aes(hp, wt)) +
+   scale_x_continuous(breaks=xbreaks) +
+   scale_y_continuous(breaks=ybreaks) +
+   geom_point()
> b <- ggplot_build(g)
> b$layout$panel_ranges[[1]]$x.range
NULL
> g <- ggplot(mtcars, aes(hp, wt)) +
+   scale_x_continuous(breaks=xbreaks) +
+   scale_y_continuous(breaks=ybreaks) +
+   geom_point()
> b <- ggplot_build(g)
> b$layout$panel_params[[1]]$x.range
[1]  37.85 349.15
Technophobe01
  • 8,212
  • 3
  • 32
  • 59