skip to Main Content

Please, find a reprex below.

I have produced this plot

enter image description here

As you can see, the grid continuous from 0 to -5. I wish leave that area completely blank, as in this plot (produced in photoshop):

enter image description here

I have tried geom_rect() but I cannot make it work:

enter image description here

1) Why is the rectangle pink when specified fill=”white”? I need it to be white.

2) How can I close the gap from the bottom of geom_rect() down to the x-axis? I need that space to be white too (ie, no sign of the grid).

SOLUTION

1) geom_rect(data=df, mapping=aes(xmin=0, xmax=3, ymin=-6,
ymax=0), fill=”white”, alpha=1)

2) I changed to scale_y_continuous(name="X", breaks=seq(0,35,5)) +
coord_cartesian(ylim=c(-5,35))
instead of scale_y_continuous(name="X",
breaks=seq(0,35,5), limits=c(-5,35))

#My script
df <- data.frame(x = as.factor(w3$uicc),
                  y = w3$lnd,
                  f = rep(c(""), c(nrow(w3))))

df <- df[!is.na(df$x),]

  ggplot(df) +
    geom_boxplot(aes(x, y, fill = f, colour = f), outlier.alpha = 0, position = position_dodge(width = 0.78)) +
    scale_x_discrete(name = "", label=c("Stage IIInn=115","Stage IVnn=302")) + theme +
    scale_y_continuous(name="X", breaks=seq(0,35,5), limits=c(-5,35)) +
    stat_boxplot(aes(x, y, colour = f), geom = "errorbar", width = 0.3,position = position_dodge(0.7753), size=1) +
    geom_point(aes(x, y, fill = f, colour = f), size = 3, shape = 21, position = position_jitterdodge(), alpha=0.7) +
    scale_fill_manual(values = c("#edf1f9", "#fcebeb"), name = "X",
                      labels = c("X", "X")) +
    scale_colour_manual(values = c("#1C73C2", "red"), name = "X",
                        labels = c("X", "X")) +
    geom_rect(data=df, mapping=aes(xmin=0, xmax=3, ymin=-5, ymax=0, fill="white"), alpha=1)



#My data

w3 <- structure(list(uicc = c(4L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 
4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 
4L, 3L, 3L, 3L, 3L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
3L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
3L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L), lnd = c(22.2222222, 
9.0909091, 25, 12.1212121, 6.6666667, 4.3478261, 33.3333333, 
10, 33.3333333, 60, 71.4285714, 25, 16.6666667, 11.1111111, 42.8571429, 
16.6666667, 10, 62.5, 25, 11.1111111, 50, 33.3333333, 7.6923077, 
7.6923077, 7.1428571, 16.6666667, 20, 14.2857143, 11.7647059, 
12.5, 71.4285714, 17.6470588, 33.3333333, 2.8571429, 7.1428571, 
5.8823529, 6.25, 4.7619048, 5, 14.2857143, 3.3333333, 20, 28.5714286, 
100, 7.1428571, 9.0909091, 2.173913, 18.1818182, 7.6923077, 9.0909091, 
14.2857143, 4.5454545, 8.6956522, 3.8461538, 11.5384615, 11.1111111, 
66.6666667, 4, 4.5454545, 14.2857143, 25, 14.2857143, 14.2857143, 
25, 7.6923077, 9.0909091, 5.8823529, 4.5454545, 16.6666667, 12.5, 
4.7619048, 30, 8.3333333, 10.5263158, 8.1081081, 8.3333333, 6.6666667, 
18.1818182, 5, 9.0909091, 6.6666667, 63.6363636, 26.6666667, 
12.5, 14.2857143, 25, 6.0606061, 25.9259259, 25, 10.5263158, 
40, 9.0909091, 6.6666667, 16.6666667, 20, 22.2222222, 14.2857143, 
15.3846154, 12.1212121, 2.0408163)), row.names = c(3L, 4L, 10L, 
15L, 19L, 20L, 35L, 37L, 40L, 47L, 48L, 50L, 54L, 57L, 59L, 62L, 
63L, 66L, 67L, 68L, 74L, 80L, 81L, 83L, 84L, 94L, 99L, 100L, 
117L, 120L, 122L, 123L, 129L, 130L, 131L, 137L, 140L, 141L, 142L, 
144L, 165L, 171L, 175L, 182L, 192L, 195L, 198L, 199L, 204L, 208L, 
209L, 210L, 211L, 212L, 215L, 218L, 221L, 222L, 234L, 236L, 239L, 
241L, 242L, 243L, 246L, 251L, 257L, 260L, 261L, 268L, 275L, 276L, 
277L, 280L, 284L, 294L, 297L, 298L, 303L, 306L, 309L, 316L, 317L, 
319L, 320L, 322L, 324L, 326L, 328L, 329L, 330L, 333L, 334L, 336L, 
337L, 339L, 340L, 341L, 346L, 349L), class = "data.frame") 

3

Answers


  1. Chosen as BEST ANSWER

    I have updated the question with solutions to both questions


  2. For the geom_rect to be white, the fill argument must be set outside of the mapping, like this:

    geom_rect(data=df, mapping=aes(xmin=0, xmax=3, ymin=-5, ymax=0), fill = "white", alpha=1)
    
    Login or Signup to reply.
  3. When you specify

    geom_rect(mapping=aes([...], fill="white"), alpha=1)
    

    you are asking ggplot to map the variable “white” to a colour, which is does using its default colour palette. If you want to specify the colour for a single geom, the easiest way is to specify it outside of aes

    geom_rect(mapping=aes([...]), fill="white", alpha=1)
    

    Similarly, if you were to specify

    geom_rect(mapping=aes(alpha=1))
    

    Then ggplot2 would try to map this continuous value to a continuous alpha scale (scale_alpha_continuous)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search