log2_breaks <- c(1, 2, 4, 8, 16, 32, 64, 128, 256)
p <- ggplot(plot_data, aes(Median, fill = Median < 0)) +
geom_histogram(bins = 9, color = "black") + # Adjust binwidth as needed
scale_fill_manual(values = c("skyblue", "red"),
name = "",
labels = c("X", "Y")) + # Custom labels for fill colors
labs(title = "", x = "X", y = "Y") +
theme_classic() +
theme(legend.text = element_text(size = 14),
axis.text = element_text(size = 14),
axis.title = element_text(size = 14)) +
scale_y_continuous(breaks = log2_breaks, labels = log2_breaks)
I want to make the y axis a log2 scale to better visualize the distribution of data, (1, 2, 4, 8 etc). I tried scale_y_continuous(breaks = log_breaks, labels = log_labels) specifying the breaks but this did not work, it only illustrated the breaks but not on a log scale.
Using
+ scale_y_continuous(transformation = 'log2')should work, but here the color aesthetic makes the results appear strange. Below is a workaround if you still want the color: it is to addbreaks = seq(-150, 150, by = 50)insidegeom_histogramto make a histogram break that falls exactly on the zero:Note that there is a warning because the bins height for some colors are zero, and the log transformation doesn't like that.
Without this trick, the plot looks like this:
Here, the 2 colors are stacked on the middle bin and I'm not sure that's what you want.