以下是代码:
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()
这是情节:
如何找到与左下角相对应的X和Y值(我用绿点标记)?我猜它们差不多是(40,1.40),但是我能问R确切的值吗?
答案 0 :(得分:6)
是的,你可以!试试这个:
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
答案 1 :(得分:1)
你想要坐标还是想在那里画一些东西?要在那里绘制某些内容,您可以将-Inf
(或Inf
用于相对的网站)作为x和y坐标:
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)
注意左下角的蓝点。默认情况下会剪切它,但您可以关闭剪裁以显示整个点:
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")
由reprex package(v0.2.0)创建于2018-05-29。
答案 2 :(得分:0)
请注意,从ggplot2 2.2.1.9000开始,layout
中的参数已从panel_ranges
更改为panel_params
。
当前ggplot(稳定版)2.2.1版本使用layout$panel_ranges
Github上的{2.2}。上游版本现在使用layout$panel_params
下面的示例代码和控制台输出:
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
> 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