How do use different points sizes to represent the amount in the location of that point

ggplot2, r

Solution

Here's a guess at what you're after. In the `df` data frame below, `x` and `y` are your categorical variables. There are various ways to get the frequency counts. Here, the `ddply()` function from the `plyr` package is used. Followed by the plot. In the call to `ggplot`: the `size` aesthetic ensures that the point sizes represent the frequencies; and the `scale_size_discrete()` function controls the size of the points on the plot.

# Some toy data
df <- structure(list(x = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", "2", "3", "4", "5"
), class = "factor"), y = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", "2", "3", 
"4", "5"), class = "factor")), .Names = c("x", "y"), row.names = c(NA, 
79L), class = "data.frame")

# Required packages
library(plyr)
library(ggplot2)

# Get the frequency counts
dfc <- ddply(df, c("x", "y"), "nrow", .drop = FALSE)
#dfc

# The plot
ggplot(data = dfc, aes(x = x, y = y, size = factor(nrow))) + 
    geom_point() + 
    scale_size_discrete(range = c(1, 10))

Or the same plot using the `df` data frame - the unaggregated data.

ggplot(data = df, aes(x = x, y = y)) +
  stat_sum(aes(size = factor(..n..)), geom = "point") +
  scale_size_discrete(range = c(1, 10))

Problem

I'm working with categorical data and I'm trying to plot a scatterplot where the size of the points should represent the frequencies on the location of that point. I tried it first with jitter but I'm unhappy with that solution. I thought I could create a Frequencies column but didn't manage to create a code for that. ``` qplot(X, Y, data=datatable, geom=c("point")) ``` Has anyone an idea? thx

Original source