【问题标题】:specifying color to individual points in a radial plot using plotrix package radial.plot使用 plotrix 包radial.plot 为径向图中的各个点指定颜色
【发布时间】:2017-08-23 23:13:02
【问题描述】:

我有 2 个数据列表。

a = c(0, 14400, 15000, 1600)
b = c(0, 1.1, 2.3, 4.5)

我想使用径向图使用极坐标(径向)坐标绘制图形。 我想要 r=theta,所以 r 和 theta 的坐标值在列表“a”中。

对于点的颜色,我想使用列表“b”来使用条件指定每个点的颜色。 例如: 如果 b 中元素的值小于 1,则该元素的颜色为黑色。 如果 b 中元素的值介于 1 和 2 之间,则颜色为绿色。 如果b中元素的值大于3,则颜色为红色。

所以结果列表将是:

c = c("black", "green", "red", "red")

最后一步是使用列表“c”为元素着色。 总之,我的问题的答案需要两个步骤: 1) 创建一个新列表“c”,其中包含基于 b 值的颜色名称列表 2)在径向图中使用列表c为图中的每个符号着色(我不知道这是否可能)

这是我目前所拥有的。

pp <- radial.plot(a, a, start = -pi/3, rp.type = "s", clockwise = TRUE, point.symbols=19)
pp

实际数据要大得多,但我只提供一个小示例代码来专注于我需要解决的 2 个问题。 我能够使用 ggplot2 生成这个图。但是,由于其他原因,我更愿意使用 plotrix 包中的radial.plot 函数(极坐标/径向坐标的代码似乎更简单易用)。

请帮忙,我是 R 新手。谢谢。

【问题讨论】:

    标签: r list polar-coordinates plotrix


    【解决方案1】:

    此代码应该足够灵活以生成您想要的绘图类型。它生成如下图。对于角度,我对它们进行了缩放,以便a 的最大值映射到 2*pi。这将使情节呈现螺旋状。不这样做意味着角度将是 a 模 2*pi 中的值,并且对于大数字,这看起来是随机的。

    关于分配颜色的第二部分可以通过基于逻辑索引分配颜色来完成。可以根据需要添加更多条件。如果没有特定的模式,要给出更一般的着色结果并不容易。

    library(plotrix)
    
    # given example data
    a = c(0, 14400, 15000, 1600)
    b = c(0, 1.1, 2.3, 4.5)
    
    
    # initializing vector of colors as NA
    colors_plot <- rep(NA,length(b))
    
    # set of conditions listed in the plot
    colors_plot[b < 1] <- "black"
    colors_plot[intersect(1 < b,b < 2)] <- "green" # need intersect because of double condition
    colors_plot[b > 2] <- "red"
    # add more conditions as needed
    
    pp <- radial.plot(a, # radial distance
                      a*2*pi/max(a), # scaling so max(a) maps to 2*pi, this avoids the cooridnates wrapping
                      start = -pi/3, # same as code
                      rp.type = "s",
                      clockwise = TRUE,
                      point.symbols=19,
                      point.col=colors_plot, # colors calcualted above
                      radial.labels=NA # not plotting radial axis labels
                      )
    

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 2016-01-22
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多