当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。