Colorbar width for facet plot in R

Viewed 121

I have this sample dataset for three labeled images:

set.seed(2021)
data_example <- lapply(c(1,2,3), function(i) {
  expand.grid(Row=1:4,Col=1:4) %>% 
    mutate(label = i)
}) 

data_example <- Reduce(rbind,data_example)
data_example$value <- sample(0:255,48)

And I created a ggplot for faceted images, but when trying to change the width of the colorbar, I can't:

ggplot(data_example, aes(Row,Col,fill=value)) +
  geom_raster()+
  scale_y_reverse()+
  scale_fill_gradient(low="white", high="blue") +
  facet_wrap(~label) +
  guides(color = guide_colorbar(barwidth = unit(.5,"lines")))

enter image description here

Any help will be greatly appreciated.

1 Answers

If you put the guide parameters inside the scale_fill_gradient arguments you get success:

  ...+
  scale_fill_gradient(low="white", high="blue", guide=guide_colorbar(barwidth=.5)) +
Related