简体   繁体   English

R:使用轴缩放geom_point(ggplot2)

[英]R: Scale geom_point with axes (ggplot2)

I have written the following code to plot my xy data on a set of re-scaleable axes, the values contained in pointSize are the correctly scaled vertical/horizontal diameters of the point I want at each plotted coordinate. 我编写了以下代码,以将我的xy数据绘制在一组可重新缩放的轴上,pointSize中包含的值是在每个绘制坐标处要正确缩放的点的垂直/水平直径。 How do I go about getting this to work? 我该如何开始工作呢? Right now I am just plotting points with whatever scaling is used by default in geom_point(aes(size)) and the points don't scale with the axes. 现在,我只是使用geom_point(aes(size))中默认使用的任何缩放比例绘制点,并且这些点不随轴缩放。 Once I rescale the axes with coord_cartesian I want the plotted points to increase/decrease relative to the axes accordingly. 使用coord_cartesian重新缩放轴后,我希望绘制的点相对于轴相应地增加/减少。

For example, if the point size is say 5, that means I want the horizontal and vertical diameter of the point to be 5 relative to the axes regardless of specified xyScaling. 例如,如果点的大小为5,则意味着我希望点的水平和垂直直径相对于轴为5,无论指定的xyScaling如何。

EDIT: min in pointSize should have been min = 0, not min = -10 编辑:pointSize中的min应该是min = 0,而不是min = -10

Minimal reproducible code: 最小的可复制代码:

# Sample size & x-y axes plot boundaries
sampleSize <- 100

# Set scale factor of x-y axes
xyScaling <- 1

# Set to false once sampled to rescale axis with same distributions
resample <- TRUE

if (resample == TRUE){
  xSample <- replicate(sampleSize, runif(1, min = -sampleSize/2, max = sampleSize/2))

  ySample <- replicate(sampleSize, runif(1, min = -sampleSize/2, max = sampleSize/2))

  pointSize <- replicate(sampleSize, runif(1, min = 0, max = 10))
}

sampleDataFrame <- data.frame(xSample, ySample, pointSize)
samplePlot <- ggplot(sampleDataFrame, aes(xSample, ySample))
samplePlot +
geom_point(data = sampleDataFrame, aes(size = sampleDataFrame$pointSize[])) + 
coord_cartesian(xlim = c((xyScaling*(-sampleSize/2)),(xyScaling*(sampleSize/2))),
                ylim = c((xyScaling*(-sampleSize/2)),(xyScaling*(sampleSize/2)))) +
xlab("x") +
ylab("y") +
scale_size_identity(guide=FALSE)

EDIT: So I almost managed to solve the problem by using geom_rect, the following code does what I want with the caveat that the points are rectangles as opposed to ellipses/circles, I couldn't get this to work with ellipses, if anyone could guide me to the right function I would be very grateful. 编辑:所以我几乎可以通过使用geom_rect来解决问题,下面的代码做了我想要的告诫,即点是矩形而不是椭圆/圆形,如果有人可以,我无法将其用于椭圆指导我正确的工作,我将不胜感激。

sampleDataFrame <- data.frame(xSample, ySample, pointSize)

samplePlot <- ggplot(sampleDataFrame)
samplePlot +
  geom_point(aes(xSample, ySample, size = 0)) + 
  geom_rect(aes(xmin = xSample-(pointSize/2), xmax = xSample+(pointSize/2), ymin = ySample-(pointSize/2), ymax = ySample+(pointSize/2))) +
  coord_cartesian(xlim = c((xyScaling*(-sampleSize/2)),(xyScaling*(sampleSize/2))),
                  ylim = c((xyScaling*(-sampleSize/2)),(xyScaling*(sampleSize/2)))) +
  xlab("x") +
  ylab("y") +
  scale_size_identity(guide=FALSE)

this has been suggested in the past , but I don't think it got implemented. 过去曾有人建议这样做,但我认为它没有实现。 One problem is that circles are only circular in the special case of cartesian coordinates with unit aspect ratio. 一个问题是,在具有单位长宽比的笛卡尔坐标的特殊情况下,圆仅是圆形的。 The easiest workaround is probably to create a data.frame with xy positions describing circles (ellipses) and draw these as polygons. 最简单的解决方法可能是创建一个具有xy位置(描述圆(椭圆))的data.frame并将其绘制为多边形。

library(gridExtra)
library(ggplot2)

circle <- polygon_regular(50)

pointy_points <- function(x, y, size){
  do.call(rbind, mapply(function(x,y,size,id) 
    data.frame(x=size*circle[,1]+x, y=size*circle[,2]+y, id=id),
         x=x,y=y, size=size, id=seq_along(x), SIMPLIFY=FALSE))

}

test <- pointy_points(1:10, 1:10, size=seq(0.2, 1, length.out=10))

ggplot(test, aes(x,y,group=id, fill=id)) + geom_polygon()

在此处输入图片说明

You could try to edit the points at the lowest-level, but it's quite fiddly, 您可以尝试在最低级别上编辑点,但这很奇怪,

library(ggplot2); library(grid)
p <- qplot(1:10, 1:10, size=I(10))
g <- ggplotGrob(p)

points <- g$grobs[[4]][["children"]][[2]]
g$grobs[[4]][["children"]][[2]] <- 
  editGrob(points, size = convertUnit(points$size, unitTo = "npc"))

grid.newpage()
grid.draw(g)

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

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