0

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")
Chris
  • 2,019
  • 5
  • 22
  • 67
  • Do you really need them to be transparent, or just lighter, to match the aesthetic of the rest of your plot? – phalteman Jan 17 '19 at 19:25
  • Both of them. Because it doesn't has to match with just the aesthetic of the plot, but also with the whole format of the power point presentation. In some of them I need it transparent, on other just lighter (that's why I'm traying to find an "alpha"). – Chris Jan 17 '19 at 20:15

1 Answers1

1

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.

enter image description here

phalteman
  • 3,442
  • 1
  • 29
  • 46