I am trying to create a plot that has an axis on the top and the right side of the plot. I added minor ticks to the x and y-axis but I noticed that when I try and add a second x and y-axis, the minor ticks are removed. I was also hoping to remove the second axis labels but that doesn't seem to work. I am wondering if there is an issue with how I added the minor ticks?
Do my main questions are, how can I add minor ticks to the second axes and how can I remove the labels.
data.bw <- structure(list(num = c(88L, 58L, 15L, 11L, 14L, 29L, 34L, 40L,
24L, 20L, 3L, 1L, 1L), bar = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5), group = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor")), class = "data.frame", row.names = c(NA,
-13L))
library(ggplot2)
library(ggh4x)
ggplot(data.bw, aes(bar,num, fill = group))
geom_bar(stat = 'identity', width = 1)
scale_fill_manual(values = c('orange', 'khaki'))
scale_y_continuous(
minor_breaks = seq(0, 90, by = 2),
breaks = seq(0, 90, by = 10), limits = c(0, 90),
expand = expansion(mult = c(0, 0)),
sec.axis = dup_axis(name = NULL),
guide = "axis_minor"
)
scale_x_continuous(
minor_breaks = seq(0, 14, by = 0.5),
breaks = seq(0, 14, by = 2), limits = c(0, 14),
expand = expansion(mult = c(0, 0)),
sec.axis = dup_axis(name = NULL),
guide = "axis_minor"
)
scale_fill_manual(values = alpha(c("red"), .3))
theme_bw()
theme(
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
CodePudding user response:
As far as I know, secondary axes in ggplot2 don't get any minor break information to pass on to the guides (or get bungled up). See also related issue. However, since you're using dup_axis()
, I'm presuming you want to duplicate your primary axes, which you can also do with guides(x.sec = "axis_minor", y.sec = "axis_minor")
, which take their order directly from the scale instead of a secondary scale.
Removing the labels of the secondary axes is as simple as setting the appropriate theme elements to element_blank()
. Had you meant the axis titles instead of text, these are off by default but you can pass them as guides(x.sec = guide_axis_minor(title = "My title"))
had you wanted them.
data.bw <- structure(list(num = c(88L, 58L, 15L, 11L, 14L, 29L, 34L, 40L,
24L, 20L, 3L, 1L, 1L), bar = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5), group = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor")), class = "data.frame", row.names = c(NA,
-13L))
library(ggplot2)
library(ggh4x)
ggplot(data.bw, aes(bar,num, fill = group))
geom_bar(stat = 'identity', width = 1)
scale_fill_manual(values = c('orange', 'khaki'))
scale_y_continuous(
minor_breaks = seq(0, 90, by = 2),
breaks = seq(0, 90, by = 10), limits = c(0, 90),
expand = expansion(mult = c(0, 0)),
guide = "axis_minor"
)
scale_x_continuous(
minor_breaks = seq(0, 14, by = 0.5),
breaks = seq(0, 14, by = 2), limits = c(0, 14),
expand = expansion(mult = c(0, 0)),
guide = "axis_minor"
)
guides(x.sec = "axis_minor", y.sec = "axis_minor")
theme_bw()
theme(
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x.top = element_blank(),
axis.text.y.right = element_blank()
)
Created on 2021-09-13 by the reprex package (v2.0.1)