Skip to content

Generalize position_jitterdodge to all possible dodge positions #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

bleutner
Copy link

So far, position_jitterdodge() is limited to boxplots dodged by the fill aesthetic.
This pull request generalizes the function to work properly on all posible dodge aesthetics:

  • fill
  • colour
  • linetype
  • alpha
  • size
  • shape

not that all of these readily make sense to me, but as long as it is supported by boxplot etc. I think it would be good to have this functionality. I for one was trying based on colour dodging, when I came upon this.

library(ggplot2)
# Example data
data(diamonds)
dsub <- subset(diamonds, clarity %in% levels(clarity)[1:3] & color %in% levels(color)[1:2])   
dsub$test <- sample(c("a","b"), nrow(dsub), replace = TRUE) 

# Set-up ggplot
p <- ggplot(dsub) +
        geom_boxplot(outlier.colour = NA) +   
        geom_point(position = position_jitterdodge())

# So far we are limited to dodge by 'fill'
p %+% aes(x = cut, y = carat, fill = clarity) 

# Dodge by single aesthetic #####################
# Dodge by: colour
p %+% aes(x = cut, y = carat, colour = clarity)

# Dodge by: linetype          
p %+%  aes(x = cut, y = carat, linetype = clarity)

# Dodge by: alpha          
p %+%  aes(x = cut, y = carat, alpha = clarity)

# Dodge by: size          
p %+%  aes(x = cut, y = carat, size = clarity)

# Dodge by: shape          
p %+%  aes(x = cut, y = carat, shape = clarity)

# Dodge interactions ############################
# Dodge by: fill & colour , case fill == color    
p %+%  aes(x = cut, y = carat, fill = clarity, colour = clarity)

# Dodge by: fill & colour , case fill != color        
p %+% aes(x = cut, y = carat, fill = clarity, colour = color)

# Dodge by: fill & colour & linetype
p %+% aes(x = cut, y = carat, fill = clarity, linetype = color, color = test)

# Adjust the x transformation based on the number of 'fill' variables
nfill <- length(levels(data$fill))

width <- self$jitter.width %||% resolution(data$x, zero = FALSE) * 0.4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix the indenting?

@hadley
Copy link
Member

hadley commented Jan 25, 2016

Looks good apart from a few style problems.

@bleutner
Copy link
Author

I fixed all comments.


# Adjust the x transformation based on the number of 'dodge' variables
dodgecols <- intersect(c("fill", "colour", "linetype", "shape", "size", "alpha"), colnames(data))
if (length(dodgecols) == 0) stop("`position_jitterdodge()` requires ",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more natural to break the line just before stop(), and I prefer to always put if statements in {}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Like that?

@hadley
Copy link
Member

hadley commented Jan 25, 2016

Perfect - thanks!

@hadley hadley closed this in ba04941 Jan 25, 2016
@hadley
Copy link
Member

hadley commented Jan 25, 2016

Thanks!

@lock
Copy link

lock bot commented Jan 18, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants