简体   繁体   English

如何才能使我的缩放转换点不会被ggplot2剪切?

[英]How can I make it so my scale-transformed points don't get clipped in ggplot2?

I'm making a dotchart where I want to log transform the x scale. 我正在制作一个圆点图,我想记录转换x刻度。 I have some points at 0, which ggplot handles nicely in that it doesn't eliminate them, however it does clip them. 我有一些点在0,ggplot处理得很好,因为它不会消除它们,但它会剪切它们。

What can I do so the points at 0 don't get clipped? 我能做什么,0点的分数不会被削减? It seems xlim() and the scale transformation don't play together, only the last one called takes effect. 似乎xlim()和缩放变换不能一起播放,只有最后一个调用生效。

An example: 一个例子:

myData <- data.frame(x = c(rexp(5), 0), y = "category")
myBreaks <- c(.1, 1, 5)
ggplot(myData, aes(x = x, y = y)) +
scale_x_continuous(trans = "log",
                   breaks = myBreaks,
                   labels = myBreaks) +
geom_point(size = 5, legend = F)

修剪

Since log(0) is -Inf , I suspect that your 0 point will always be clipped if you keep it zero. 由于log(0)-Inf ,我怀疑如果保持零,你的0点将永远被剪裁。 I tried to fiddle with expand=... , coord_trans and everything else I could think of. 我试图摆弄expand=...coord_trans以及我能想到的其他一切。

Here is a workaround: 这是一个解决方法:

  • set your zero values to an arbitrary small value (say 1e-6) 将零值设置为任意小值(比如1e-6)
  • include a break at that value 包括该价值的突破
  • optionally label that break 0 可选地标记为中断0

The code: 代码:

myData <- data.frame(x = c(rexp(5), 0), y = "category")

myData <- within(myData, x[x==0] <- 1e-6)
myBreaks <- c(1e-6, 0.1, 1, 5)
myLabels <- c(0, myBreaks[-1])
ggplot(myData, aes(x = x, y = y)) +
    geom_point(size = 5, legend = F) +
    scale_x_continuous(
        trans = "log",
        breaks = myBreaks,
        labels = myLabels
    ) 

在此输入图像描述

With the release of ggplot2 version 3.0.0, you can use coord_cartesian(clip = 'off') . 随着ggplot2版本3.0.0的发布,你可以使用coord_cartesian(clip = 'off')

library(ggplot2)

myData <- data.frame(x = c(rexp(5), 0), y = "category")
myBreaks <- c(.1, 1, 5)
ggplot(myData, aes(x = x, y = y)) +
  scale_x_continuous(trans = "log",
                     breaks = myBreaks,
                     labels = myBreaks) +
  geom_point(size = 5, legend = F) +
  coord_cartesian(clip = 'off') +
  labs(title = "coord_cartesian(clip = 'off')")

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM