0

I'm using ggplot to create a scatterplot of a dataframe. The x and y axis are two columns in the frame, and the following code gives me a scatter plot:

ggplot(df,aes(x=Season,y=type))+
  geom_point(fill="blue")

But the points are all the same size. I want each point to depend on the count of how many rows matched for the combination of x and y. Anyone know how?

Johnny
  • 59
  • 5

1 Answers1

5

You haven't provided any sample data, so I'm generating some:

library('tidyverse')

df <- data.frame(Season = sample(c('W', 'S'), size=20, replace=T),
                 type=sample(c('A', 'B'), size=20, replace=T))

df %>%
  group_by(Season, type) %>%
  summarise(count = n()) %>%
  ggplot(aes(x=Season, y=type, size=count)) +
     geom_point(col="blue")

The idea is to count all the occurences of your Season–type data, and then use that new count field to adjust the size in your ggplot.

enter image description here

Stewart Macdonald
  • 2,062
  • 24
  • 27
  • You could use `count(Season, type)` rather than `group_by(Season, type) %>% summarise(count = n())`. – zephryl Dec 16 '22 at 03:51