控制ggplot2图中点的相对大小 [英] Controlling relative size of points in ggplot2 plots

查看:95
本文介绍了控制ggplot2图中点的相对大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制许多不同的图块,根据 数据.不幸的是,我无法提供图片说明,但基本上,该图是由图块组成的,这些图块上是否具有正方形和点.

I need to draw many different tile plots, which have squares and dots on top of the tiles according to data. Unfortunately I cannot include illustrating picture, but basically the plot consist of tiles which either have squares and dots on them or not.

这些图中的每一个在x方向和y方向上具有不同数量的图块.结果,我不能使用绝对单位(mm)来缩放点大小和框大小.有没有办法以相对的方式指定点的大小.例如,像geom_point(aes(x=x, y=y), size = 0.5 * tile_size()).

Each of those figures has different number of tiles on x-direction and y-direction. As a result, I cannot use absolute units (mm) to scale the dot sizes and box sizes. Is there any way to specify the size of the dot in relative terms. For instance, like geom_point(aes(x=x, y=y), size = 0.5 * tile_size()).

以下 buggy 代码段

df <- data.frame(x=factor(c(1:4), labels=c("a", "b", "c", "d")), y=factor(c(1:4), labels=c("f", "g", "h", "j")))
p <- ggplot(data = df) + geom_tile(aes(x=x, y=y), fill="green", color="black")
p <- p + opts(aspect.ratio=2)
p <- p + geom_point(aes(x=x, y=y), color="red", size = 4, shape=15, color="red")
p <- p + geom_point(aes(x=x, y=y), color="blue", size = 2)
p <- p + scale_x_discrete("Variables") + scale_y_discrete("Time")
show(p)

df <- data.frame(x=factor(c(1:4), labels=c("a", "b", "c", "d")), y=factor(c(1:4), labels=c("f", "g", "h", "j")))
p <- ggplot(data = df) + geom_tile(aes(x=x, y=y), fill="green", color="black")
p <- p + opts(aspect.ratio=2)
p <- p + geom_point(aes(x=x, y=y), color="red", size = 4, shape=15, color="red")
p <- p + geom_point(aes(x=x, y=y), color="blue", size = 2)
p <- p + scale_x_discrete("Variables") + scale_y_discrete("Time")
show(p)

几乎可以实现所需的功能,除非我调整图形窗口或pdf尺寸的大小,点和盒子相对于图块大小的相对大小都会改变.特别是,点和框可能会比其上绘制的图块大,这是绝对不可以的.

almost achieves the desired functionality, except if I resize the graphical window or the pdf dimensions, the relative size of points and boxes with respect to the tile sizes changes. In particular, the dots and boxes might be larger than the tile they are drawn on, which is absolute no-no.

如果不可能相对缩放点大小,那么我想知道是否可以使用ggsave()pdf()指定绘图区域的尺寸(没有轴标签,刻度等的绘图).然后我可以自己进行相对缩放.

If relative scaling of point size is impossible, then I would like to know is it possible to specify the dimensions of the plot area (plot without axes labels, ticks and so on) using ggsave() or pdf(). Then I could do relative scaling by myself.

推荐答案

我认为您想以数据单位而不是物理单位指定大小.这对于圆形和正方形来说有点棘手,因为它们实际上在数据空间中可能是椭圆形和矩形(除非您正在使用coord_equal).但是,如果要执行此操作,则可以自己生成字形并使用geom_polygon绘制它们.

I think you want to specify sizes in data units, rather than physical units. This is something that is a little tricky for circles and squares because they may actually be ovals and rectangles in data space (unless you are using coord_equal). But if you want to do this, you can generate the glyphs yourself and use geom_polygon to draw them.

这篇关于控制ggplot2图中点的相对大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆