代码之家  ›  专栏  ›  技术社区  ›  Chris

X和Y标签中的透明度-ggplot2,R

  •  0
  • Chris  · 技术社区  · 6 年前

    我试图使X和Y标签在一定程度上透明。然而,(1)我找不到在这个领域有效的东西 scale_x_continuous() 功能,因为它没有 alpha 功能可用。还有别的办法吗 阿尔法

    代码如下:

    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")
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   phalteman Andryas Waurzenczak    6 年前

    好吧,这并不漂亮,但这里有一种方法可以让你得到你想要的,主要是基于 this approach 在打印边界之外进行注释。

    中没有alpha参数 scale_x_continuous() 对于标签,也没有一个是 element_text() 可以 annotate()

    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))
    

    几点注意:

    • coord_cartesian() clip="off" 允许在打印区域之外添加注释。要使绘图集中在正确的区域,还需要指定 xlim ylim ,这意味着您不能在中指定这些参数 scale_x/y_continuous() . (我已经从您的原始代码中删除了这些。)
    • 还需要指定打印边距,以便为标签留出足够的空间。要想弄清楚这些可能/应该是什么,需要反复试验。

    enter image description here