【问题标题】:Fill a polygon with gradient scale in R在R中用渐变比例填充多边形
【发布时间】:2018-04-26 11:47:49
【问题描述】:

我想创建一个量表来可视化一个变量对另一个变量的影响或多或少。

我构建了一个函数来绘制仪表,并用 3 种不同的颜色填充它。

gg.gauge <- function(pos,breaks=c(0,33,66,100),determinent) {

  require(ggplot2)

  get.poly <- function(a,b,r1=0.5,r2=1.0) {
    th.start <- pi*(1-a/100)
    th.end   <- pi*(1-b/100)
    th       <- seq(th.start,th.end,length=100)
    x        <- c(r1*cos(th),rev(r2*cos(th)))
    y        <- c(r1*sin(th),rev(r2*sin(th)))
    return(data.frame(x,y))
  }
  
  ggplot()+ 
    geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y), fill = "red")+
    geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y), fill = "gold")+
    geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y), fill = "green")+
    geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y))+
    geom_text(data=as.data.frame(breaks), size=5, fontface="bold", vjust=0,
              aes(x=0.8*cos(pi*(1-    breaks/100)),y=-0.1),label=c('Less','','',"More"))+
        annotate("text",x=0,y=0,label=determinent,vjust=0,size=8,fontface="bold")+
    coord_fixed()+
    theme_bw()+
    theme(axis.text=element_blank(),
          axis.title=element_blank(),
          axis.ticks=element_blank(),
          panel.grid=element_blank(),
          panel.border=element_blank(),
          legend.position = "none") 
}

输出:

我想用渐变色填充,这样它就可以产生从红色(低)到绿色(高)的渐变效果,而没有非常明显的切割。

我尝试使用scale_fill_grdientn,但没有得到肯定的结果。

谢谢

【问题讨论】:

    标签: r ggplot2 gradient


    【解决方案1】:

    实现这一点的最简单方法是使用单独的线段而不是多边形。以下面的修改示例为例,我只更改了get_poly的定义,并使用geom_segment而不是geom_polygon

    gg.gauge <- function(pos, breaks = c(0, 33, 66, 100), determinent) {
      require(ggplot2)
      get.poly <- function(a, b, r1 = 0.5, r2 = 1.0) {
        th.start <- pi * (1 - a / 100)
        th.end   <- pi * (1 - b / 100)
        th       <- seq(th.start, th.end, length = 1000)
        x        <- r1 * cos(th)
        xend     <- r2 * cos(th)
        y        <- r1 * sin(th)
        yend     <- r2 * sin(th)
        data.frame(x, y, xend, yend)
      }
    
      ggplot() + 
        geom_segment(data = get.poly(breaks[1],breaks[4]), 
                     aes(x = x, y = y, xend = xend, yend = yend, color = xend)) +
        scale_color_gradientn(colors = c("red", "gold", "green")) +
        geom_segment(data = get.poly(pos - 1, pos + 1, 0.2), aes(x = x, y  =y, xend = xend, yend = yend)) +
        geom_text(data=as.data.frame(breaks), size = 5, fontface = "bold", vjust = 0,
                  aes(x = 0.8 * cos(pi * (1 - breaks / 100)),  y = -0.1), label = c('Less', '', '', "More")) +
        annotate("text", x  = 0, y = 0,label=determinent,vjust=0,size=8,fontface="bold")+
        coord_fixed()+
        theme_bw()+
        theme(axis.text=element_blank(),
              axis.title=element_blank(),
              axis.ticks=element_blank(),
              panel.grid=element_blank(),
              panel.border=element_blank(),
              legend.position = "none")
    }
    gg.gauge(pos = 10, determinent = "Test")
    

    【讨论】:

    • 太棒了!谢谢你。快速附加问题:您如何获得平滑的图像?你只是增加 th 的长度吗?我得到的输出似乎有“缺失”数据(空白点)
    • 正确。 th 的长度是绘制的段数。我用了1000,这已经足够了。如果需要,请尝试更多。
    • 此外,您可以通过添加例如增加段的厚度。 lwd=3geom_segment。找到适合您需求的两种选项的完美组合。
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2021-10-24
    相关资源
    最近更新 更多