rggplot2formattinggeom-col

geom_col remove borders between columns


I have the following plot where I wish to remove the black lines between columns but not the outline for a whole block e.g. wild and landrace. Is there a way to do so based on the code I'm using? Thanks in advance!

enter image description here

Example code:

library(ggplot2)
library(forcats)

ggplot(tbl2, aes(factor(sampleID), value, fill=comp)) +
  geom_col(color="black", size=0.1, width=1) +
  facet_grid(~fct_inorder(variety), switch="x", scales="free", space="free") +
  theme_minimal() + labs(x="Individuals", title="K=3", y="components") +
  scale_y_continuous(expand=c(0, 0)) +
  scale_x_discrete(expand=expand_scale(add=1)) +
  theme(
    panel.spacing.x=unit(0.1, "lines"),
    axis.text.x=element_blank(),
    panel.grid=element_blank()
  ) +
  scale_fill_brewer(palette="Paired", guide='none')

-- EDIT post-comment -- changed tbl with tbl2 and a dput(tbl2)

structure(list(sampleID = c("INLUP00165", "INLUP00165", "INLUP00165", 
"INLUP00169", "INLUP00169", "INLUP00169", "INLUP00208", "INLUP00208", 
"INLUP00208", "INLUP00214", "INLUP00214", "INLUP00214", "INLUP00228", 
"INLUP00228", "INLUP00228", "INLUP00233", "INLUP00233", "INLUP00233", 
"INLUP00245", "INLUP00245", "INLUP00245", "INLUP00325", "INLUP00325", 
"INLUP00325", "INLUP00332", "INLUP00332", "INLUP00332", "INLUP00393", 
"INLUP00393", "INLUP00393", "INLUP00418", "INLUP00418", "INLUP00418", 
"INLUP00496", "INLUP00496", "INLUP00496"), variety = c("wild", 
"wild", "wild", "landrace", "landrace", "landrace", "wild", "wild", 
"wild", "wild", "wild", "wild", "landrace", "landrace", "landrace", 
"wild", "wild", "wild", "landrace", "landrace", "landrace", "landrace", 
"landrace", "landrace", "landrace", "landrace", "landrace", "landrace", 
"landrace", "landrace", "landrace", "landrace", "landrace", "landrace", 
"landrace", "landrace"), comp = c("comp1", "comp2", "comp3", 
"comp1", "comp2", "comp3", "comp1", "comp2", "comp3", "comp1", 
"comp2", "comp3", "comp1", "comp2", "comp3", "comp1", "comp2", 
"comp3", "comp1", "comp2", "comp3", "comp1", "comp2", "comp3", 
"comp1", "comp2", "comp3", "comp1", "comp2", "comp3", "comp1", 
"comp2", "comp3", "comp1", "comp2", "comp3"), value = c(0.000304534914903343, 
0.999695539474487, 5.66402567378596e-16, 7.25639850429616e-08, 
0.99999988079071, 7.41350128733039e-16, 2.27430740759918e-42, 
1, 1.40129846432482e-45, 0, 1, 1.26116861789234e-44, 1, 3.6168054052767e-30, 
6.73797980640088e-16, 7.37646416837379e-07, 0.999999284744263, 
1.86443519971077e-23, 1, 2.82813122498233e-29, 4.54367988546311e-14, 
6.76272702548886e-06, 1.06498683288686e-43, 0.999993205070496, 
0, 1, 0, 3.04290442727506e-05, 4.66462843997956e-16, 0.999969601631165, 
4.25612745047488e-09, 1, 5.97792615408821e-09, 1.18218178221686e-16, 
1, 3.16024730889457e-37)), class = "data.frame", row.names = c(NA, 
-36L))

Solution

  • As recommended in comments, remove color = "black" from the geom_col layer, remove the padding from the x-scale, and use panel.border to draw the rectangles around each facet:

    ggplot(tbl2, aes(factor(sampleID), value, fill=comp)) +
      geom_col(width=1) +
      facet_grid(~fct_inorder(variety), switch="x", scales="free", space="free") +
      theme_minimal() + labs(x="Individuals", title="K=3", y="components") +
      scale_y_continuous(expand=c(0, 0)) +
      scale_x_discrete(expand=c(0, 0)) +
      theme(
        panel.spacing.x=unit(0.1, "lines"),
        axis.text.x=element_blank(),
        panel.grid=element_blank(),
        panel.border = element_rect(color = "black", size = 3, fill = NA)
      ) +
      scale_fill_brewer(palette="Paired", guide='none')
    

    enter image description here

    size = 3 is used to make it nice and clear, you can of course tone it down.