rggplot2plotggboxplot

How can I change the layout color of a ggplot with scale_y_break?


ggboxplot with axis break

Hi, I plan to plot a black background boxplot with the ggplot function via the configuration of plot. background. The scale_y_break() has been adopted to segment the y-axis. However, the final plot remained a huge white broader, and nothing can be removed.

It would be great if somebody could provide a hint with me. The following was my coding in R.

thickbox <- ggboxplot(df_data,x="Yasix",y="Thickness", fill = "Status.y", shape= "Status.y", width = 0.9,  color = "white")+
  scale_y_break(c(620,3700),scales = 1)+
  theme(axis.line = element_line(size = 2,colour = "white"),
        axis.ticks = element_line(size = 2,colour ="white"),
        axis.ticks.length=unit(0.4, "cm"),
        prism.ticks.length.y = unit(0.12, "cm"),
        axis.text = element_text(size = 20, color = "white", face = "bold"),
        axis.title = element_text(size = 20, face = "bold",colour = "black"),
        legend.position = "none",
        plot.subtitle = element_text(size = 20, face = "bold",hjust = 0.5,colour = "white"),
        plot.title = element_text(size=28, face = "bold",colour = "black"),
        axis.text.y.right = element_blank(),
        axis.line.y.right = element_blank(),
        axis.ticks.y.right = element_blank(),
        text = element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = 'black'),
        plot.background=element_rect(fill = "black"),
        legend.background = element_rect(fill = 'black'),
        legend.text = element_text(colour = 'white'),
        legend.title = element_text(colour = "white"))

thickbox

enter image description here


Solution

  • Looking into the mechanics of ggbreak, it produces a special class of object that is only converted into a ggplot when you print it. The layers in the ggplot it produces are not true geom layers, but actually grob layers (i.e. pictures) of two ggplots stitched together. The theme specification you pass applies to the two inset ggplots, but not to the resulting ggplot object. The resulting ggplot object has its own theme specification, which only inherits some of the theme elements of the subplots.

    The bottom line is that we need to do something like:

    print(thickbox) + 
      theme(plot.background = element_rect(fill = 'black', color = 'black'),
            panel.background = element_rect(fill = 'black', color = 'black'),
            panel.border = element_rect(color = 'black', linewidth = 5, 
                                        fill = NA),
            axis.line = element_line(color = 'black', linewidth = 2))
    

    enter image description here


    Data used

    There was no data in the question, so I had to create my own with similar values:

    set.seed(1)
    
    df_data <- data.frame(Yasix = c(rep(c(-20, 30, 80, 110, 150, 200, 250), 
                                        times = c(10, 10, 10, 10, 10, 1, 5))),
                          Thickness = rnorm(56, mean = rep(c(560, 3750, 560, 3750, 
                                            560, 560, 3750),
                                            times = c(10, 10, 10, 10, 10, 1, 5)), 
                                            sd = 20),
                          Status.y = rep(c('A', 'B', 'A', 'B', 'A', 'A', 'B'),
                                         times = c(10, 10, 10, 10, 10, 1, 5)))