[英]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 回答。)
对我来说,您想让哪些数量/比较“更容易看到”并不明显,因为该短语有些主观和上下文相关。 我想到了几个想法:
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)
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.