【问题标题】:Translate base R plot to ggplot. Gradient fill rectangles将基础 R 图转换为 ggplot。渐变填充矩形
【发布时间】:2021-11-09 11:51:07
【问题描述】:

有人可以帮我将以下基本 R 代码翻译成 ggplot2:

编辑 - 我的 x 值是观察值。例如:

x            <-runif(100,min=0,max=60)
lim_x        <-range(x)
lim_y        <-c(0.5,3.5)
probabilities<-cbind(seq(from=0,to=1,length.out=100),
                     c(seq(from=0,to=1,length.out=50),seq(from=1,to=0,length.out=50)),
                     seq(from=1,to=0,length.out=100))

plot(c(lim_x[1],lim_x[2]), c(lim_y[1], lim_y[2]), type = "n",ylab="",xlab="") 


for(i in 1:ncol(probabilities)){
  p <- probabilities[,i]
  gradient.rect(lim_x[1],i-0.5,lim_x[2],i+0.5,nslices=nrow(probabilities),
                reds=1,greens=1-p,blues=1-p)
}

产生这个情节:

【问题讨论】:

    标签: r ggplot2 plot graphics gradient


    【解决方案1】:

    这就是我将基本 R 代码转换为 ggplot2 的方式:

    library(ggplot2)
    
    lim_x        <-c(0,60)
    lim_y        <-c(0.5,3.5)
    probabilities<-cbind(seq(from=0,to=1,length.out=100),
                         c(seq(from=0,to=1,length.out=50),seq(from=1,to=0,length.out=50)),
                         seq(from=1,to=0,length.out=100))
    
    
    df <- reshape2::melt(probabilities)
    
    ggplot(df, aes(Var1, Var2, fill = value)) +
      geom_tile() +
      scale_fill_gradientn(colours = c("white", "red")) +
      annotate("rect", xmin = min(df$Var1) - 0.5, xmax = max(df$Var1) + 0.5,
               ymin = unique(df$Var2) - 0.5, ymax = unique(df$Var2) + 0.5,
               colour = "black", fill = NA)
    

    reprex package (v2.0.1) 于 2021-09-14 创建

    或者,如果您想要完全相同的颜色,您可以使用:

    ggplot(df, aes(Var1, Var2)) +
      geom_tile(aes(fill = I(rgb(1, 1 - value, 1 - value)))) +
      annotate("rect", xmin = min(df$Var1) - 0.5, xmax = max(df$Var1) + 0.5,
               ymin = unique(df$Var2) - 0.5, ymax = unique(df$Var2) + 0.5,
               colour = "black", fill = NA)
    

    【讨论】:

    • 谢谢你。如果我的 x 像原始帖子中的那样从 0 到 60 怎么办?所以 x 值的间隔不规则?
    • 您可以预转换df$Var1 &lt;- scales::rescale(df$Var1, lim_x),但这不会使它们变得不规则(仅使用非整数距离)。
    • 我已经包含了一个编辑,我该如何处理将 x 的已知值应用于您的答案? range(df$Var1) 必须是 range(x),而且 Var1 应该对应于 x 的实际值。我不知道在那种情况下如何融化 df
    • x 变量除了在您的示例中计算限制外,不会在任何地方使用。我不确定你要我翻译什么。
    【解决方案2】:

    你必须转换你的数据

    probabilities_long <- matrix(t(probabilities), ncol = 1)
    
    df <- data.frame(x1 = sort(rep(seq(0,60,length.out = 100),3)), 
                     x2 = sort(rep(seq(0.6, 60.6, length.out = 100),3)),
                     y1 = rep(c(0.5, 1.5, 2.5),100),
                     y2 = rep(c(1.5, 2.5, 3.5),100), 
                     prob = matrix(t(probabilities), ncol = 1))
    

    然后你可以使用geom_rect来绘制每个图块:

    ggplot(df)+
        geom_rect(aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill = prob))+
        scale_fill_gradient(low = "#ffffff",
                              high = "#ff0000")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      相关资源
      最近更新 更多