Resize Points in Existing Plot Fails

78 views
Skip to first unread message

Dario Strbenac

unread,
Mar 20, 2015, 2:00:11 AM3/20/15
to ggp...@googlegroups.com
If I have a function which does lots of processing and returns a ggplot object, I don't want to rerun the function and do all of the time-consuming calculations again, but it seems impossible to change the point size of plot points. The object is also huge when saved to disk.

> class(myPlot)
[1] "gg"     "ggplot"

myPlot <- myPlot + geom_point(size = 1) # Doesn't change appearance.
myPlot
myPlot <- myPlot + geom_point(size = 10) # Doesn't change appearance.
myPlot

> format(object.size(myPlot), units = "KB")
[1] "26 Kb"

> save(myPlot, file = "myPlot.obj")     # Why 198 MB on disk ?

#############
## $ ls -l myPlot.obj
## -rw-r----- 1 dario stgrad 208140875 Mar 20 16:52 myPlot.obj
#############

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] ggplot2_1.0.0       ClassifyR_1.1.32    BiocParallel_1.0.3  Biobase_2.26.0      BiocGenerics_0.12.1 limma_3.22.4

Doug Mitarotonda

unread,
Mar 20, 2015, 11:22:41 AM3/20/15
to Dario Strbenac, ggp...@googlegroups.com
Well, because you didn’t provide a reproducible example, it is hard to help you. When I make a small example on my own what you suggest works fine. 

> library(ggplot2)
> dft <- data.frame(A=1:3, B=4:6)
> ggplot(dft, aes(A,B)) + geom_point()
> p <- ggplot(dft, aes(A,B)) + geom_point()
> p + geom_point(size=1) # small points
> p + geom_point(size=10) # large points


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dario Strbenac

unread,
Mar 21, 2015, 12:00:10 AM3/21/15
to ggp...@googlegroups.com
Hello,

That simple example works for me, too. I have a reproducible example which does not work. You many copy and paste it directly into R. It fails if you resize the points more than once.

library(ggplot2)

aData <- data.frame(A = factor(sample(c('A', 'B'), 50, replace = TRUE)),
                    B = factor(sample(c('C', 'D'), 50, replace = TRUE)),
                    C = factor(rep(seq(10, 100, 10), 5)),
                    D = rnorm(50, 10, 10))


overlapPlot <- ggplot(aData, aes(x = C, y = D, colour = A, shape = B)) + geom_line(size = 2) + geom_point(size = 3)

overlapPlot <- overlapPlot + geom_point(size = 10)
overlapPlot     # This works.
overlapPlot <- overlapPlot + geom_point(size = 1)
overlapPlot     # This fails.


> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] ggplot2_1.0.0

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4 digest_0.6.8     grid_3.1.2       gtable_0.1.2     labeling_0.3     MASS_7.3-39      munsell_0.4.2  
 [8] plyr_1.8.1       proto_0.3-10     Rcpp_0.11.4      reshape2_1.4.1   scales_0.2.4     stringr_0.6.2

Dennis Murphy

unread,
Mar 21, 2015, 12:41:13 AM3/21/15
to Dario Strbenac, ggplot2
The plot does not "fail" - the way you've constructed it makes it
impossible to see the result of the last call because the fill color
of points is the same in all three calls. Here's a modified version of
your code; the line is removed to better see what is going on. The
point shape is also changed to unfilled to show that the last call
indeed works.

ggplot(aData, aes(x = C, y = D, colour = A)) +
geom_point(size = 4, shape = 21)

last_plot() + geom_point(size = 10, shape = 21)
last_plot() + geom_point(size = 2, shape = 21)


As for your original plot, one suggestion is to change to an unfilled
point shape (21-25) as follows:

overlapPlot <- ggplot(aData, aes(x = C, y = D, colour = A, shape = B)) +
geom_line(size = 2) + geom_point(size = 3) +
scale_shape_manual(values = c(21, 22))

last_plot() + geom_point(size = 10)
last_plot() + geom_point(size = 1)


ggplot2 is based on grid graphics, and grid operates on the "painter's
model" - to quote Paul Murrell in Chapter 5 of R Graphics, 2e:

"Like the traditional system, all grid output occurs on the current
device, and later output obscures any earlier output that it overlaps
(i.e.,output follows the “painters model”)."

Dennis

Dario Strbenac

unread,
Mar 21, 2015, 3:00:15 AM3/21/15
to ggp...@googlegroups.com
If it's like a painter's canvas, why does using plot.new() before the last plot not work as desired ?

Dennis Murphy

unread,
Mar 21, 2015, 5:51:19 PM3/21/15
to Dario Strbenac, ggplot2
Because plot.new() is a base graphics function, not grid. You start a
new graphics page in grid with grid.newpage(). Calling grid.newpage()
wipes out the previous contents; that's its purpose. Since I have no
idea what "work as desired" means, I can't attempt to help further.

Dennis

On Sat, Mar 21, 2015 at 12:00 AM, Dario Strbenac <dario....@gmail.com> wrote:
> If it's like a painter's canvas, why does using plot.new() before the last
> plot not work as desired ?
>

Dario Strbenac

unread,
Mar 22, 2015, 6:00:10 PM3/22/15
to ggp...@googlegroups.com
What I mean is that I would expect the following code to clear the canvas, enabling the size 1 points to be visible.

overlapPlot <- overlapPlot + geom_point(size = 10)
overlapPlot
grid.newpage()
Reply all
Reply to author
Forward
0 new messages