Reputation: 2071
I'm trying to make the X and Y labels transparent in a certain degree. However, (1) I can't find something that works in the scale_x_continuous()
function as it doesn't have the alpha
function available. Is there any another way (without alpha
) to make them transparent in a certain degree?
This is the code:
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), limits = c(0, 0.5)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 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")
Upvotes: 0
Views: 1420
Reputation: 3532
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:
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.)Upvotes: 1