deadlypaw
deadlypaw

Reputation: 11

geom_jitter smaller increasing point size

I'm trying to plot some People per year as a point in my Boxplot, with the size of the point as the age. When I don't write size=Pnr$Alterpunkt (that's the column in which I have saved the size of the points based on the age. It's like 0.1 for somebody who's 15, 0.2 for somebody who's 16 and so on.) in the aes it looks perfekt, but then I don't have a legend and I would like to have one. If I write it into the aes the points become much bigger, and that doesn't look quiet good, as I have a lot of points.

Then it looks like this:

enter image description here

I've already tried to set the shape to "." or stroke=0. I also tried to work with the guide_legend, but nothing seems to change anything

That's the code for the plot:

q <- ggplot(Pnr, aes(group=year, x=year, y=Ges))
q <- q + geom_boxplot(outlier.shape = NA)
q <- q + scale_x_continuous(breaks = c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,
                                       2015,2016,2017,2018,2019,2020))
q <- q + scale_y_continuous(limits = c(-1000,10000))
q <- q + theme(legend.position = c(0.3, 0.3),
               legend.direction = "horizontal", 
               legend.background = element_rect(fill = "white", colour = NA), 
               legend.title=element_blank(), 
               panel.background = element_rect(fill="white", colour="darkgrey", linetype="solid"),
               panel.grid.major = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               panel.grid.minor = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               strip.background = element_rect(fill="lightgrey", colour = "darkgrey"))
q <- q + geom_jitter(aes(size=Pnr$Alterpunkt, shape="."), show.legend = T)

I'd like to have it to look like this, but with a legend

enter image description here

I hope somebody can help me.

Upvotes: 1

Views: 8846

Answers (1)

Jon Spring
Jon Spring

Reputation: 66570

It's hard to know how to help specifically without an example of your data. Here are some suggestions, hope something helps:

  • geom_jitter(aes(size=Alterpunkt)... instead of geom_jitter(aes(size=Pnr$Alterpunkt, shape="."), since using $ inside ggplot calls can lead to errors, and that shape call should be outside of the aes(), if there at all.

  • Use scale_size_area(max_size =...) or scale_size_continuous(range = c(...)) to define the size of your dots. Depending on your aesthetic goals, you could apply a transformation to adjust how size varies as the underlying variable changes. In the case below, I liked the look of using trans = scales::exp_trans(base = 1.1) -- this made the big points significantly bigger, but not dramatically bigger, than the medium sized points. Adjust to taste.

Here's an example: enter image description here

library(gapminder)
library(tidyverse)

fake_df <- gapminder %>%
  mutate(age = (lifeExp - 23.5)*1.7,
         Ges = (gdpPercap/1000)^0.5) %>%
  select(continent, year, age, Ges)

q <- ggplot(fake_df, aes(group=year, x=year, y=Ges))
q <- q + geom_boxplot(outlier.shape = NA)
q <- q + scale_x_continuous(breaks = seq(1950, 2020, 10))
# q <- q + scale_y_continuous(limits = c(-1000,10000))
q <- q + theme(legend.position = c(0.3, 0.6),
               legend.direction = "horizontal", 
               legend.background = element_rect(fill = "white", colour = NA), 
               legend.title=element_blank(), 
               panel.background = element_rect(fill="white", colour="darkgrey", linetype="solid"),
               panel.grid.major = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               panel.grid.minor = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               strip.background = element_rect(fill="lightgrey", colour = "darkgrey"))
q <- q + geom_jitter(aes(size=age), show.legend = T) +
  scale_size_continuous(range = c(0.01, 1.5), 
                        trans = scales::exp_trans(base = 1.2),
                        breaks = c(1, 80, 100))
q

Upvotes: 1

Related Questions