简体   繁体   English

使用plotrix包radial.plot为径向图中的各个点指定颜色

[英]specifying color to individual points in a radial plot using plotrix package radial.plot

I have 2 lists of data. 我有2个数据列表。

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

I would like to plot a graph using polar (radial) coordinates using radial plot. 我想使用径向图使用极坐标(径向)绘制图形。 I want r=theta, so the values for the coordinates for r and theta are in the list "a". 我想要r = theta,所以r和theta的坐标值在列表“ a”中。

For the color of the points I want to use list "b" to specify the colors of each point using conditions. 对于点的颜色,我想使用列表“ b”使用条件指定每个点的颜色。 So for example: If the value of the elemnt in b is less than 1, the color of that element is black. 因此,例如:如果b中的elemnt的值小于1,则该元素的颜色为黑色。 If the value of the element in b is between 1 and 2, the color is green. 如果b中元素的值在1到2之间,则颜色为绿色。 If the value of the element in b is greater than 3, the color is red. 如果b中元素的值大于3,则颜色为红色。

So the resulting list would be: 因此,结果列表将是:

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

The last step would be to use the list "c" to color the elements. 最后一步是使用列表“ c”为元素着色。 In summary the answer to my question require 2 steps: 1) creating a new list "c" with a list of color names based on values of b 2) using list c in the radial plot to color each symbol in the plot (I don't know if this is possible) 总而言之,我的问题的答案需要2个步骤:1)使用b的值创建一个带有颜色名称列表的新列表“ c” 2)在径向图中使用列表c为图中的每个符号着色(我不不知道这是否可能)

Here is what I have so far. 这是我到目前为止所拥有的。

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

The actual data is much larger, but I am providing only a small example code to focus on the 2 problems that I need to solve. 实际数据要大得多,但是我仅提供一个小示例代码来关注我需要解决的2个问题。 I was able to generate this plot using ggplot2. 我能够使用ggplot2生成此图。 However, I would prefer to use the radial.plot function in the plotrix package to this because of other reasons (the code for polar/radial coordinates seems to be much simpler and easier to use). 但是,由于其他原因,我宁愿在plotrix包中使用radial.plot函数(极坐标/径向坐标的代码似乎更容易使用)。

Please help, I'm new to R. Thank you. 请帮助,我是R的新手。谢谢。

This code should be flexible enough to generate the type of plot you want. 此代码应足够灵活以生成所需的绘图类型。 It generates the figure shown below. 生成如下图。 For the angles I scaled them so that the maximum value of a would map to 2*pi. 对于角度,我对其进行了缩放,以使a的最大值映射为2 * pi。 This will give a spiral look to the plot. 这将使图呈螺旋状。 Not doing that means that the angle will be the value in a modulo 2*pi, and with large numbers this appears random. 如果不这样做,这意味着,该角度将在值a模2 * PI,和具有大量这种随机出现。

The second part about assigning colors can be done through assigning colors based on logical indexing. 关于分配颜色的第二部分可以通过基于逻辑索引分配颜色来完成。 More conditions can be added as needed. 可以根据需要添加更多条件。 Without a specific pattern it isn't simple to give a more general result for coloring. 没有特定的图案,要给出更一般的着色结果并不容易。

在此处输入图片说明

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
                  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM