1

I've created a grouped boxplot and added three specific geom_hlines to the plot. However, I want to set the hline colors to fill=factor(Training.Location), rather than trying to match the colors manually with a color palette. Is there a way to do this?

ggplot(aes(x=factor(CumDes),y=Mn_Handle), data=NH_C) +
    geom_boxplot( aes(fill=factor(Training.Location))) +
    geom_point( aes(color=factor(Training.Location)), 
    position=position_dodge(width=0.75) ) + 
    theme(axis.ticks = element_blank(), axis.text.x = element_blank()) + 
    coord_cartesian(ylim = c(0, 2000)) + 
    geom_hline(yintercept=432, linetype="dashed", lwd=1.2) + 
    geom_hline(yintercept=583, linetype="dashed", lwd=1.2) + 
    geom_hline(yintercept=439, linetype="dashed", lwd=1.2)
tonytonov
  • 25,060
  • 16
  • 82
  • 98
user3594490
  • 1,949
  • 2
  • 20
  • 26
  • if you name your plot h and run in R g <- ggplot_build(h) d <- unique(g$data[[1]]["fill"]) you will get the default color. I guest you could try to extract these colors and set in geom_hline() – MLavoie Jan 05 '16 at 21:29
  • You can use `geom_segment` instead of `geom_hline` and add in `aes(colour = factor(Training.Location)[1])`. Not sure why adding that `aes` to `geom_hline` doesn't work though. – Mist Jan 05 '16 at 21:45
  • @Richo64 Thanks for suggestion, when I tried I got the following Error: geom_segment requires the following missing aesthetics: xend, yend. – user3594490 Jan 05 '16 at 22:13
  • If I use geom_hline and aes(color = factor(Training.Location =="site1")) and vary the sitename for each of three hlines, I get the same color for all three hlines and "TRUE"/"FALSE" additions to the legend. I think the true false argument is overriding the color for all three hlines for some reason. – user3594490 Jan 05 '16 at 22:20
  • Yeah, a segment requires a start and end point. Use max and min to set these values. e.g. `geom_segment(y=432, yend = 432, x = min(factor(CumDes)), xend = max(factor(CumDes)))` – Mist Jan 05 '16 at 22:21
  • Sorry, I left the aes out. The whole thing should look like this: `geom_segment(y=432, yend = 432, x = min(factor(CumDes)), xend = max(factor(CumDes)), aes(colour = factor(Training.Location)[1]))` – Mist Jan 05 '16 at 22:57
  • 1
    It would be great if you could supply a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to answer your question. That way others can also befit form your question, and the accompanying answer, in the future. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Jan 05 '16 at 23:09
  • @Eric Fail, wondering if the name is a joke. Apologies if it's not. If I saw the coding and understood what the objective was I wouldn't need anything more to benefit from some potential solutions. I find similar postings here pretty frequently and still find them useful. I've never been able to figure out how to upload an image here as you have below or how to ammend with a datafile, additional coding without gettig flagged for altering the original question. Thanks for your input and assistance though.I'll keep trying to improve. – user3594490 Jan 06 '16 at 00:24
  • @richo64 I've tried adding your ammended code but received Error in factor(CumDes) : object 'CumDes' not found. Maybe I need to restate the dataframe somewhere? – user3594490 Jan 06 '16 at 00:29
  • That's what happens when you write code and don't test it :) Replace `CumDes` with `NH_C$CumDes`. But Eric's solution looks fine to me, I'd go with that. – Mist Jan 06 '16 at 00:38

2 Answers2

7

This is the sort of thing that seems easiest with a new dataset. I'm not sure how you are calculating the values you are using for the horizontal lines, but often times I want to calculate these from the original dataset and use some sort of aggregation function/package for that.

Here is a modified example from the help page for geom_hline.

Make the dataset to give to geom_hline, including the values for the horizontal lines as well as the grouping variable.

mean_wt = data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))

Then just plot with the new dataset for that layer, using whatever aesthetic you wish with the grouping variable.

ggplot(mtcars, aes(x = factor(vs), wt) ) +
    geom_boxplot(aes(fill = factor(cyl))) +
    geom_point(aes(color = factor(cyl)), position = position_dodge(.75)) +
    geom_hline(data = mean_wt, aes(yintercept = wt, color = factor(cyl)) )

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • There are two separate datasets, one to obtain the boxplots and another for a control group to obtain the values for the hlines. What you are showing in the image is exactly what I was trying to obtain. I wish I would've posted some data but couldn't find a way to make it parsimonious enough. Using aes(color = factor(my fill variable)) kept resulting in an error. I finally had to subset the dataset and manually set the color value. I will save your code and modify with the manually entered values for the hlines to see if I missed something before. – user3594490 Jan 07 '16 at 04:19
  • @user3594490 As you know, it's hard without an example but the key to having this work is to make sure that your grouping variable `Training.Location` is in the control group dataset that you give to `geom_hline along with the values for where the lines should be drawn. – aosmith Jan 07 '16 at 15:41
0

Here's a somewhat hackish solution (I had to improvise on the data, feel free to improve)

hhh

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
col <- c("#CC6666", "#9999CC", "#66CC99")

 ggplot(mtcars, aes(x = factor(cyl), y=mpg)) + 
geom_boxplot(aes(fill=gear)) +
geom_point( aes(color=factor(gear)), 
position=position_dodge(width=0.75) ) + 
scale_colour_manual(values= col) + 
theme(axis.ticks = element_blank(), axis.text.x = element_blank()) + coord_cartesian(ylim = c(8, 35)) + 
geom_hline(yintercept=12, linetype="dashed", lwd=1.2, color=col[1]) + 
geom_hline(yintercept=18, linetype="dashed", lwd=1.2, color=col[2]) + 
geom_hline(yintercept=28, linetype="dashed", lwd=1.2, color=col[3])
Eric Fail
  • 8,191
  • 8
  • 72
  • 128