使用 plotrix 包 radial.plot 为径向图中的各个点指定颜色
specifying color to individual points in a radial plot using plotrix package radial.plot
我有 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" 为元素着色。
总之,我的问题的答案需要 2 个步骤:
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 函数( polar/radial 坐标的代码似乎更简单,更容易使用)。
请帮忙,我是 R 的新手。谢谢。
这段代码应该足够灵活,可以生成您想要的绘图类型。它生成如下图所示。对于角度,我对它们进行了缩放,以便 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
)
我有 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" 为元素着色。 总之,我的问题的答案需要 2 个步骤: 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 函数( polar/radial 坐标的代码似乎更简单,更容易使用)。
请帮忙,我是 R 的新手。谢谢。
这段代码应该足够灵活,可以生成您想要的绘图类型。它生成如下图所示。对于角度,我对它们进行了缩放,以便 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
)