Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:11:34+00:00 2026-05-29T15:11:34+00:00

When I make box plots, I like to also show the raw data in

  • 0

When I make box plots, I like to also show the raw data in the background, like this:

library(ggplot2)
library(RColorBrewer)

cols = brewer.pal(9, 'Set1')

n=10000
dat = data.frame(value=rnorm(n, 1:4), group=factor(1:4))

ggplot(dat, aes(x=group, y=value, color=group, group=group)) +
  geom_point(position=position_jitter(width=0.3), alpha=0.1) +
  scale_color_manual(values=cols) +
  geom_boxplot(fill=0, outlier.size=0)

enter image description here

However, I don’t like it how my box plots completely disappear when the points get too dense. I know I can adjust alpha, which is fine in some cases, but not when my groups have varying densities (For example when the lightest group would completely disappear if I were to decrease alpha enough so that the darkest group doesn’t obscure the box plot). What I’m trying to do is systematically shift the colors for the box plots – a bit darker, perhaps – so that they show up even when the background points max out the alpha. For example:

plot(1:9, rep(1, 9), pch=19, cex=2, col=cols)
cols_dk = rgb2hsv(col2rgb(brewer.pal(9, 'Set1'))) - c(0, 0, 0.2)
cols_dk = hsv(cols_dk[1,], cols_dk[2,], cols_dk[3,])
points(1:9, rep(1.2, 9), pch=19, cex=2, col=cols_dk)

enter image description here

So far I haven’t found a way to fake in a different scale_color for the geom_boxplot layer (which would seem the simplest route if there’s a way to do it). Nor have I been able to find a simple syntax to systematically adjust the colors the same way you can easily offset a continuous aesthetic like aes(x=x+1).

The closest thing I’ve been able to get is to completely duplicate the levels of the factor…

ggplot(dat, aes(x=group, y=value, color=group, group=group)) +
  geom_point(position=position_jitter(width=0.3), alpha=0.1) +
  scale_color_manual(values=c(cols[1:4], cols_dk[1:4])) +
  geom_boxplot(aes(color=factor(as.numeric(group)+4)), fill=0, outlier.size=0)

enter image description here

but then I have to deal with that ugly legend. Any better ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:11:35+00:00Added an answer on May 29, 2026 at 3:11 pm

    For now, you could define your own version of GeomBoxplot (calling it, say, GeomPlotDark), differing from the original only in that it first ‘darkens’ the colors before plotting them.

    With proto, you can do this by creating a proto object, GeomBoxplotDark, that inherits from GeomBoxplot, and differs only in its draw function. Most of the draw function’s definition is taken from the GeomBoxplot sources; I have annotated the lines I changed with comments like this # ** ... **:

    require(ggplot2)
    
    GeomBoxplotDark <- proto(ggplot2:::GeomBoxplot,
      draw <- function(., data, ..., outlier.colour = "black", outlier.shape = 16, outlier.size = 2) {
        defaults <- with(data, {                               # ** OPENING "{" ADDED **
        cols_dk <- rgb2hsv(col2rgb(colour)) - c(0, 0, 0.2)     # ** LINE ADDED        **
        cols_dk <- hsv(cols_dk[1,], cols_dk[2,], cols_dk[3,])  # ** LINE ADDED        **
        data.frame(x = x, xmin = xmin, xmax = xmax,
          colour = cols_dk,                                    # ** EDITED, PASSING IN cols_dk **
          size = size,
          linetype = 1, group = 1, alpha = 1,
          fill = alpha(fill, alpha),
          stringsAsFactors = FALSE
        )})                                                    # ** CLOSING "}" ADDED **
        defaults2 <- defaults[c(1,1), ]
    
        if (!is.null(data$outliers) && length(data$outliers[[1]] >= 1)) {
          outliers_grob <- with(data,
            GeomPoint$draw(data.frame(
              y = outliers[[1]], x = x[rep(1, length(outliers[[1]]))],
              colour=I(outlier.colour), shape = outlier.shape, alpha = 1,
              size = outlier.size, fill = NA), ...
            )
          )
        } else {
          outliers_grob <- NULL
        }
    
        with(data, ggname(.$my_name(), grobTree(
          outliers_grob,
          GeomPath$draw(data.frame(y=c(upper, ymax), defaults2), ...),
          GeomPath$draw(data.frame(y=c(lower, ymin), defaults2), ...),
          GeomRect$draw(data.frame(ymax = upper, ymin = lower, defaults), ...),
          GeomRect$draw(data.frame(ymax = middle, ymin = middle, defaults), ...)
        )))
      }
    )
    

    Then create a geom_boxplot_dark() to be called by the user, and which appropriately wraps the call to GeomBoxplotDark$new():

    geom_boxplot_dark <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", 
        outlier.colour = "black", outlier.shape = 16, outlier.size = 2, 
        ...) 
    GeomBoxplotDark$new(mapping = mapping, data = data, stat = stat, 
        position = position, outlier.colour = outlier.colour, outlier.shape = outlier.shape, 
        outlier.size = outlier.size, ...)
    

    Finally, try it out with code almost identical to your original call, just substituting a call to geom_boxplot_dark() for the call to geom_boxplot():

    library(ggplot2)
    library(RColorBrewer)
    
    cols = brewer.pal(9, 'Set1')
    
    n=10000
    dat = data.frame(value=rnorm(n, 1:4), group=factor(1:4))
    
    ggplot(dat, aes(x=group, y=value, color=group, group=group)) +
      geom_point(position=position_jitter(width=0.3), alpha=0.1) +
      scale_color_manual(values=cols) +
      geom_boxplot_dark(fill=0, outlier.size=0)
    

    I think the resulting plot looks pretty nifty. With a bit of tweaking, and viewed directly (not as an uploaded file), it’ll look awesome:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a combo box that behaves somewhat like the Firefox 3
i want to make a text box like stackoverflow have for the tag at
Are there any gems that would help me make a search box like the
How can I make alert box background color,button size message will be center align,alertbox
im trying to make this box stay one one width at 150px but how
I am wondering if there is some way to make an box have the
I am trying to make a list box where I have complete control over
What's the best way (if any) to make the inside box transparent so the
What will be the code when I try to make a combo box read
I am building a search box (input field) which should make a server call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.