ggplot2 - 仅显示 y 轴的上下范围

发布于 01-09 10:22 字数 961 浏览 3 评论 0原文

我的 geom_dotplot 中的 y 轴范围从 0 到 1。点仅在上限和下限范围内。我想将没有点的中间范围 (0.05 - 0.95) 缩小到 0.05 区间,并与 0 - 0.05 之间的下限范围和 0.95-1 之间的上限范围一起显示。 有人可以帮忙吗? 我的代码是:

ggplot(
  identical, aes(x=SNV, y=RAF, fill=Mutual_zygosity_of_parents)) +
  geom_dotplot(
    binaxis = 'y', stackdir = 'center', stackratio = 0, dotsize = 0.3, show.legend = FALSE) +
  scale_fill_manual(values=c("cadetblue1")) + 
  theme(legend.key=element_blank()) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  theme(axis.text.y = element_text(face="bold",size=16), 
        axis.title.y = element_text(face="bold",size=16)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
  expand_limits(x= c(-1,+195))

plot

非常感谢 米洛斯岛

y-axis in my geom_dotplot ranges from 0 to 1. Dots are only in the upper and lower range. I want to shrink the intermediate range with no dots (0.05 - 0.95) into the interval 0.05 and display together with the lower range between 0 - 0.05 and the upper range 0.95-1.
Can anyone help, please?
my code is:

ggplot(
  identical, aes(x=SNV, y=RAF, fill=Mutual_zygosity_of_parents)) +
  geom_dotplot(
    binaxis = 'y', stackdir = 'center', stackratio = 0, dotsize = 0.3, show.legend = FALSE) +
  scale_fill_manual(values=c("cadetblue1")) + 
  theme(legend.key=element_blank()) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  theme(axis.text.y = element_text(face="bold",size=16), 
        axis.title.y = element_text(face="bold",size=16)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
  expand_limits(x= c(-1,+195))

plot

Thanks a lot
Milos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍2025-01-16 10:22:02

您的问题不可重现,因为我们没有您的数据,但我们至少可以构建具有相同名称的类似数据结构,以便我们可以使用您的绘图代码来获得类似的结果:

set.seed(1)

identical <- data.frame(SNV = factor(sample(1:200, 400, TRUE)),
                        RAF = c(runif(200, 0, 0.02), runif(200, 0.97, 1)),
                        Mutual_zygosity_of_parents = "Yes")

p <- ggplot(
  identical, aes(x=SNV, y=RAF, fill=Mutual_zygosity_of_parents)) +
  geom_dotplot(
    binaxis = 'y', stackdir = 'center', stackratio = 0, dotsize = 0.3, show.legend = FALSE) +
  scale_fill_manual(values=c("cadetblue1")) + 
  theme(legend.key=element_blank()) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  theme(axis.text.y = element_text(face="bold",size=16), 
        axis.title.y = element_text(face="bold",size=16)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
  expand_limits(x= c(-1,+195))

p

在此处输入图像描述

现在这里的问题是ggplot 无法绘制不连续的轴,但通过分面可以相当接近。我们需要做的就是将分面变量设置为 RAF RAF RAF RAF RAF RAF RAF < 0.5,这会将上点和下点分成各自的组。如果我们还使用 scales = "free_y",那么 y 轴将“放大”到上下点的范围:

p + facet_grid(RAF < 0.5~., scales = "free_y")
  theme(strip.background = element_blank(),
        strip.text = element_blank())

在此处输入图像描述

Your question is not reproducible since we don't have your data, but we can at least construct a similar data structure with the same names so we can use your plotting code to get a similar result:

set.seed(1)

identical <- data.frame(SNV = factor(sample(1:200, 400, TRUE)),
                        RAF = c(runif(200, 0, 0.02), runif(200, 0.97, 1)),
                        Mutual_zygosity_of_parents = "Yes")

p <- ggplot(
  identical, aes(x=SNV, y=RAF, fill=Mutual_zygosity_of_parents)) +
  geom_dotplot(
    binaxis = 'y', stackdir = 'center', stackratio = 0, dotsize = 0.3, show.legend = FALSE) +
  scale_fill_manual(values=c("cadetblue1")) + 
  theme(legend.key=element_blank()) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  theme(axis.text.y = element_text(face="bold",size=16), 
        axis.title.y = element_text(face="bold",size=16)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
  expand_limits(x= c(-1,+195))

p

enter image description here

Now the problem here is that ggplot cannot do discontinuous axes, but you can come fairly close by faceting. All we need to do is set the faceting variable as RAF < 0.5, which will split the upper and lower dots into their own groups. If we also use scales = "free_y", then the y axis will "zoom in" to the range of the upper and lower dots:

p + facet_grid(RAF < 0.5~., scales = "free_y")
  theme(strip.background = element_blank(),
        strip.text = element_blank())

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文