I have written the following code to plot my x-y 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)

set.seed(1000)where1000can ofc be any number, the same random numbers will be chosen. It's somewhat likeRhas a list of 'random' numbers stored, and theset.seedfunction tells it at what point to start reading off the list. - Akhil Nair+ scale_size_continuous(range = c(0, max))a play. With regards to themaxpoint, you'll have to tune that geometrically to match your scaling, but this is the argument that will actually grow and shrink the points as you're expecting. Also, changing yourgeom_pointcall to+ geom_point(aes(size = pointSize))will be sufficient, rather than what you have (as you're not overriding the data, and the aesthetics can seesampleDataFrame's column names). - Akhil Nairgeom_point(data = sampleDataFrame, aes(size = pointSize)) + scale_size_continuous(range = c(0, 30)) + ...and I have altered max arbitrarily but it doesn't seem to be having any affect . - PhysWhiz