简体   繁体   中英

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. 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. Once I rescale the axes with coord_cartesian I want the plotted points to increase/decrease relative to the axes accordingly.

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.

EDIT: min in pointSize should have been min = 0, not 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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