Well, it isn't pretty, but here's one way to get what you're looking for, based largely on this approach to annotating outside of the plot boundaries.
There isn't an alpha argument in scale_x_continuous()
for the label, nor is there one is element_text()
in the theme argument. You can specify alpha for text made with annotate()
and you can place these annotations outside the plot area, so this solution uses that approach.
df <- data.frame(X=runif(1), Y=rnorm(1))
ggplot(df, aes(x=X, y=Y)) +
geom_point(colour="red",size=3) +
# geom_text_repel(label=df$Label, family="sans", fontface="bold", size=3) +
scale_x_continuous(labels = scales::percent_format(accuracy = 1)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1), position = "right") +
annotate("rect", xmin = 0.25, xmax = Inf, ymin = 0.5, ymax = -Inf, fill= "brown2", alpha=0.3) +
theme_light() +
geom_hline(yintercept = 0.5,alpha=0.3, color="blue") + geom_vline(xintercept = 0.25,alpha=0.3,color="blue") +
theme(axis.title=element_blank(),
plot.margin = unit(c(5,10,10,5), "mm")) +
annotate("text", label="X", x=0.25, y=-0.15, alpha=0.5) +
annotate("text", label="Y", x=0.58, y=0.5, alpha=0.5) +
coord_cartesian(clip="off", ylim=c(0,1), xlim=c(0,0.5))
A couple notes:
- You need to use
coord_cartesian()
and specify clip="off"
to allow annotations outside the plot area. To keep the plot focused on the right area, you also need to specify xlim
and ylim
, which means you can't specify those parameters in scale_x/y_continuous()
. (I've removed these from your original code.)
- You also need to specify plot margins that leave you enough space for the label. It takes some trial and error to figure out what these could/should be.
- It also takes a some work to figure out where the transparent axis labels should be, so this isn't a great approach if you have more than a few plots.
