當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R ggplot2 coord_trans 變換後的笛卡爾坐標係


coord_trans() 與比例變換不同,它發生在統計變換之後,並且會影響幾何圖形的視覺外觀 - 不能保證直線將繼續保持直線。

用法

coord_trans(
  x = "identity",
  y = "identity",
  xlim = NULL,
  ylim = NULL,
  limx = deprecated(),
  limy = deprecated(),
  clip = "on",
  expand = TRUE
)

參數

x, y

x 軸和 y 軸的轉換器或其名稱。

xlim, ylim

x 軸和 y 軸的限製。

limx, limy

[Deprecated]采用xlimylim反而。

clip

是否應該將繪圖裁剪到繪圖麵板的範圍內?設置"on"(默認)表示是,設置"off"表示否。在大多數情況下,不應更改 "on" 的默認值,因為設置 clip = "off" 可能會導致意外結果。它允許在繪圖上的任何位置繪製數據點,包括繪圖邊。如果通過 xlimylim 設置限製,並且某些數據點超出這些限製,則這些數據點可能會顯示在軸、圖例、繪圖標題或繪圖邊距等位置。

expand

如果 TRUE (默認值)會在限製中添加一個小的擴展因子,以確保數據和軸不重疊。如果 FALSE ,則完全從數據或 xlim /ylim 中獲取限製。

細節

轉換僅適用於連續值:有關轉換列表以及如何創建自己的轉換的說明,請參閱scales::trans_new()

例子

# \donttest{
# See ?geom_boxplot for other examples

# Three ways of doing transformation in ggplot:
#  * by transforming the data
ggplot(diamonds, aes(log10(carat), log10(price))) +
  geom_point()

#  * by transforming the scales
ggplot(diamonds, aes(carat, price)) +
  geom_point() +
  scale_x_log10() +
  scale_y_log10()

#  * by transforming the coordinate system:
ggplot(diamonds, aes(carat, price)) +
  geom_point() +
  coord_trans(x = "log10", y = "log10")


# The difference between transforming the scales and
# transforming the coordinate system is that scale
# transformation occurs BEFORE statistics, and coordinate
# transformation afterwards.  Coordinate transformation also
# changes the shape of geoms:

d <- subset(diamonds, carat > 0.5)

ggplot(d, aes(carat, price)) +
  geom_point() +
  geom_smooth(method = "lm") +
  scale_x_log10() +
  scale_y_log10()
#> `geom_smooth()` using formula = 'y ~ x'


ggplot(d, aes(carat, price)) +
  geom_point() +
  geom_smooth(method = "lm") +
  coord_trans(x = "log10", y = "log10")
#> `geom_smooth()` using formula = 'y ~ x'


# Here I used a subset of diamonds so that the smoothed line didn't
# drop below zero, which obviously causes problems on the log-transformed
# scale

# With a combination of scale and coordinate transformation, it's
# possible to do back-transformations:
ggplot(diamonds, aes(carat, price)) +
  geom_point() +
  geom_smooth(method = "lm") +
  scale_x_log10() +
  scale_y_log10() +
  coord_trans(x = scales::exp_trans(10), y = scales::exp_trans(10))
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: NaNs produced
#> Warning: NaNs produced
#> Warning: NaNs produced
#> Warning: NaNs produced
#> Warning: NaNs produced
#> Warning: NaNs produced
#> Warning: NaNs produced
#> Warning: NaNs produced


# cf.
ggplot(diamonds, aes(carat, price)) +
  geom_point() +
  geom_smooth(method = "lm")
#> `geom_smooth()` using formula = 'y ~ x'


# Also works with discrete scales
set.seed(1)
df <- data.frame(a = abs(rnorm(26)),letters)
plot <- ggplot(df,aes(a,letters)) + geom_point()

plot + coord_trans(x = "log10")

plot + coord_trans(x = "sqrt")

# }

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Transformed Cartesian coordinate system。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。