Question

I've made series of GIFs of population distribution plots (using ggplot2 and Image Magick), where each sample has the number of individuals, and a latitude and longitude value. However, the range of values differs from plot to plot, so the size between plots is constant. I'm trying to keep the size of the values constant between graphs (ie. the point below which has a value of 30, would be larger in size that the plot before which has a value of 10).

Here is a reproducible example:

    #simulating latitude and longitude point creation
    a <- c("1","1")
    b <- c("2","2")
    c <- c("3","1")

    x <- rbind(a,b,c)
    x <- as.data.frame(x)

    #simulating the total individual count in my data
    mag1 <- as.numeric(c("2", "6", "10"))
    mag2 <- as.numeric(c("2", "6", "30"))

    d1 <- cbind(x, mag1)
    d2 <- cbind(x, mag2)

    #Plotting the two scatter plots
    g1 <- ggplot(data=d1, aes(x=V1, y=V2)) + geom_point(aes(size=mag1))
    g1
    g2 <- ggplot(data=d2, aes(x=V1, y=V2)) + geom_point(aes(size=mag2))
    g2

Any help is greatly appreciated! (apologies in advance if the code is messy, or my question wasn't communicated 100% clearly, I'm new to R!)

Was it helpful?

Solution

Use scale_size_continuous() and set the same limits= for all plots. With argument breaks= you can set the values (breaks) you will get in legend. If the maximal size of point you get on plot is too small then you can also change argument range= which default values are range=c(1,6).

g1 + scale_size_continuous(limits=c(1,30),breaks=c(5,10,20,30))

g2 + scale_size_continuous(limits=c(1,30),breaks=c(5,10,20,30))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top