In ggplot2, how can I add ticks on the top x-axis when I have levels in variables?
For example, take a look at this graphic:
library(ggplot2)
ToothGrowth$dose <- factor(ToothGrowth$dose, levels=unique(ToothGrowth$dose))
p <- ggplot(data=ToothGrowth, aes(x=supp, y=len, fill=dose)) +
geom_boxplot() +
theme_bw(base_size=20) +
scale_y_continuous(sec.axis=dup_axis(labels=NULL,name=NULL))
p
The above plot also has ticks in the right axis, which is half of what I need.
But when I try adding ticks to the top axis:
p +
scale_x_continuous(labels = levels(ToothGrowth$supp),
breaks = seq_along(levels(ToothGrowth$supp)),
sec.axis=dup_axis(labels=NULL,name=NULL))
I get the message:
Error: Discrete value supplied to continuous scale
which I think happens because I have levels in the dose
variable.
I tried to use scale_x_discrete
. However, it doesn't accept the argument sec.axis.
Is there any way make it work?