繁体   English   中英

如何将 plot 个垂直段与 x 重叠?

[英]How to plot vertical segments with overlapping x?

f=read.table(pipe('curl -s https://i.stack.imgur.com/vZwiL.gif | tail -c +43 | zcat'), header=T)
f$x=asinh(f$x)
f$y1=asinh(f$y1)
f$y2=asinh(f$y2)
png('out.png')
with(f, plot(c(x, x), c(y1, y2), type='n'))
with(f, points(x, y1, pch=20))
with(f, segments(x, y1, x, y2))
dev.off()

我想 plot 段 plot 使用上面的数据。 目前的结果是这样的。

它不是很明显。 x 列是整数,其间隔相对不均匀。 我认为有人可能会做类似的事情 1) 将它们分组到垃圾箱中,2) plot asinh 比例中的垃圾箱,3) 在每个垃圾箱中的 x 中应用 gitter 来解决重叠问题。

3 可以通过根据 y1 对段进行排序来更改为 3',以便 y1 在每个 bin 中从低到高(从左到右)排序。 但是,这可能会引入更多代码。

我怎样才能 plot 这个数据比当前的解决方案更容易查看(至少如上面两种方法所解释的,例如,gitter vs sort,但也欢迎其他更有创意的方法)而不必编写太多代码? (欢迎简单的 R 和 ggplot2 回答。)

在此处输入图像描述

对我来说,您想让哪些数量/比较“更容易看到”并不明显,因为该短语有些主观和上下文相关。 我想到了几个想法:

  • 对比例进行转换,以便数据可以以原始单位显示(如果这对您的情况很重要),并添加一些 alpha 以使点密度较高的区域更清晰,并使用不同的 colors 将点与上面的范围区分开来.

library(ggplot2)
asinh_trans <- scales::trans_new(name = "asinh",
                                 transform = base::asinh,
                                 inverse = base::sinh)

ggplot(f, aes(x, y1, xend = x, yend = y2)) +
  geom_segment(alpha = 0.1, color = "blue") +
  geom_point(alpha = 0.5, size = 0.3) +
  scale_x_continuous(trans = asinh_trans) +
  scale_y_continuous(trans = asinh_trans)

在此处输入图像描述

  • 使用点样本来给出更多“指示性”的典型意义:

ggplot(dplyr::slice_sample(f, n= 200), aes(x, y1, xend = x, yend = y2)) +
  geom_segment(alpha = 0.1, color = "blue") +
  geom_point(alpha = 0.5, size = 0.3) +
  scale_x_continuous(trans = asinh_trans) +
  scale_y_continuous(trans = asinh_trans)

在此处输入图像描述

  • 关注 x 值的分布,它在 21、22、30 等处有一些条带。

ggplot(f, aes(x, y1, xend = x, yend = y2)) +
  # geom_segment(alpha = 0.1, color = "blue") +
  geom_point(alpha = 0.2, size = 0.3) +
  scale_x_continuous(trans = asinh_trans, breaks = 2^c(0:12)) +
  scale_y_continuous(trans = asinh_trans) +
  coord_cartesian(ylim = c(0,5))

在此处输入图像描述


编辑——为每个观察分配自己的 x 值的附加方法,因此没有数据重叠,同时在缩放比例上将 x 值保持在 1 以内。 单独查看每个点需要非常广泛的数字,但它应该是可能的。

asinh_trans <- scales::trans_new(name = "asinh",
                                 transform = base::asinh,
                                 inverse = base::sinh)
f %>%
  arrange(x, y1) %>%
  group_by(x) %>%
  mutate(x_adj = x + row_number()/n()) %>%

  ggplot(aes(x_adj, y1, xend = x_adj, yend = y2)) +
  geom_segment(alpha = 0.1, color = "blue") +
  geom_point(alpha = 0.5, size = 0.3) +
  scale_x_continuous(trans = asinh_trans, 
                     breaks = c(1*10^(0:5),
                                2*10^(0:5), 
                                5*10^(0:5))) +
  scale_y_continuous(trans = asinh_trans)

在此处输入图像描述

暂无
暂无

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

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