如何偏移径向图中的线?
How to offset lines in a radial plot?
我使用 plotrix
包中的 radial.plot
函数创建了一个图表,说明样本适合多个不同类别的程度。下面给出的示例的结果如下所示:
可以看出,我已经在使用透明度使重叠线可见,例如示例 1 和 2。我仍然认为这是一个有点丑陋的解决方法。我想要做的是将线条彼此稍微偏移,就像在这个模型中一样:
到目前为止我没有找到答案。有帮助吗?
library(RColorBrewer)
library(plotrix)
library(scales)
m <- matrix(0, 5, 5)
m[1,1] <- 10
m[2,1] <- 12
m[5,2] <- 15
m[2,3] <- 20
m[4,4] <- 12
m[3,5] <- 17
colors <- brewer.pal(nrow(m), "Dark2")
pdf(file = "overview-plot.pdf", width = 14, height = 10)
par(cex.axis=2)
radial.plot(m,
labels=c("A", "B", "C", "D", "E"),
radlab = FALSE,
mar = c(11,8,11,8),
cex = 2,
label.prop = 1.3,
rp.type="rs",
lty = 1,
lwd = 5,
start = 0.65,
show.grid.labels=0,
point.symbols=19,
point.col=alpha(colors, 0.8),
line.col=alpha(colors, 0.5),
boxed.radial = FALSE,
show.radial.grid = FALSE,
radial.lim=c(0,max(m)),
grid.bg = "#f0f0f0")
legend(
24,8,
c(1:5),
col=colors,
lty=1,
lwd=5,
bty = "n",
title = "Samples",
cex = 2)
dev.off()
以下代码应生成与您想要的类似的绘图。它将矩阵转换为长度向量并指定每个绘制长度的角度。角度根据为组 ("A"、"B"、...) 绘制的长度数进行调整。不绘制零长度。添加或修改代码的部分已标记。
图书馆(RColorBrewer)
图书馆(plotrix)
图书馆(天平)
m <- matrix(0, 5, 5)
m[1,1] <- 10
m[2,1] <- 12
m[5,2] <- 15
m[2,3] <- 20
m[4,4] <- 12
m[3,5] <- 17
#### added code ####
# vector of number of non-zero (plotted) lines for each group (label)
num_non_zero <- colSums(m > 0)
# angles of groups (in radians)
ang_gp <- 2*pi*seq(0,length(num_non_zero)-1,1)/length(num_non_zero)
# adjustment to angles for multiple entries in groups (in radians)
# increasing values will give more space
# no check for angle being too large and being plotted in another group
ang_adj <- 2*pi/50
# plotting angles used with one entry per line plotted (in radians)
ang_plot <- rep(0,sum(num_non_zero))
# values of lengths to plot
# which() returns values with numbering starting
# going down column 1
lens_plot <- m[which(m > 0)]
# counter used in loop
counter <- 1
# colors used in plotting as index of vectors
# modulo operator returns 0 when divisible by number of groups
# so assign this to the number of groups in this case
col_plot_inds <- which(m>0)%%nrow(m)
col_plot_inds[col_plot_inds == 0] <- nrow(m)
# calcuations of adjustment to angles for each line
# based on the number of groups
# loop over the number of groups
for(i in 1:length(num_non_zero)){
# set of angle shifts, but not demeaned
shifts_unscaled <- ang_adj*seq(0,num_non_zero[i]-1,1)
# shifts demeaned
shifts_scaled <- shifts_unscaled - mean(shifts_unscaled)
# loop over the number if elements in each group
for(j in 1:num_non_zero[i]){
# assigning angle as group angle plus shift
ang_plot[counter] <-ang_plot[counter]+ ang_gp[i] + shifts_scaled[j]
# incrementing counter
counter <- counter + 1
}
}
colors <- brewer.pal(nrow(m), "Dark2")
pdf(file = "overview-plot.pdf", width = 14, height = 10)
par(cex.axis=2)
radial.plot(lens_plot, # using vector not matrix of value
radial.pos=ang_plot, # calcuated in loop
labels=c("A", "B", "C", "D", "E"),
radlab = FALSE,
mar = c(11,8,11,8),
cex = 2,
label.prop = 1.3,
rp.type="rs",
lty = 1,
lwd = 5,
start = 0.65,
show.grid.labels=0,
point.symbols=19,
point.col=alpha(colors[col_plot_inds], 0.8), # calling group index for colors
line.col=alpha(colors[col_plot_inds], 0.5), # calling group index for colors
boxed.radial = FALSE,
show.radial.grid = FALSE,
radial.lim=c(0,max(m)),
grid.bg = "#f0f0f0",
)
#### end of added code ####
legend(
24,8,
c(1:5),
col=colors,
lty=1,
lwd=5,
bty = "n",
title = "Samples",
cex = 2)
dev.off()
我使用 plotrix
包中的 radial.plot
函数创建了一个图表,说明样本适合多个不同类别的程度。下面给出的示例的结果如下所示:
可以看出,我已经在使用透明度使重叠线可见,例如示例 1 和 2。我仍然认为这是一个有点丑陋的解决方法。我想要做的是将线条彼此稍微偏移,就像在这个模型中一样:
library(RColorBrewer)
library(plotrix)
library(scales)
m <- matrix(0, 5, 5)
m[1,1] <- 10
m[2,1] <- 12
m[5,2] <- 15
m[2,3] <- 20
m[4,4] <- 12
m[3,5] <- 17
colors <- brewer.pal(nrow(m), "Dark2")
pdf(file = "overview-plot.pdf", width = 14, height = 10)
par(cex.axis=2)
radial.plot(m,
labels=c("A", "B", "C", "D", "E"),
radlab = FALSE,
mar = c(11,8,11,8),
cex = 2,
label.prop = 1.3,
rp.type="rs",
lty = 1,
lwd = 5,
start = 0.65,
show.grid.labels=0,
point.symbols=19,
point.col=alpha(colors, 0.8),
line.col=alpha(colors, 0.5),
boxed.radial = FALSE,
show.radial.grid = FALSE,
radial.lim=c(0,max(m)),
grid.bg = "#f0f0f0")
legend(
24,8,
c(1:5),
col=colors,
lty=1,
lwd=5,
bty = "n",
title = "Samples",
cex = 2)
dev.off()
以下代码应生成与您想要的类似的绘图。它将矩阵转换为长度向量并指定每个绘制长度的角度。角度根据为组 ("A"、"B"、...) 绘制的长度数进行调整。不绘制零长度。添加或修改代码的部分已标记。
m <- matrix(0, 5, 5)
m[1,1] <- 10
m[2,1] <- 12
m[5,2] <- 15
m[2,3] <- 20
m[4,4] <- 12
m[3,5] <- 17
#### added code ####
# vector of number of non-zero (plotted) lines for each group (label)
num_non_zero <- colSums(m > 0)
# angles of groups (in radians)
ang_gp <- 2*pi*seq(0,length(num_non_zero)-1,1)/length(num_non_zero)
# adjustment to angles for multiple entries in groups (in radians)
# increasing values will give more space
# no check for angle being too large and being plotted in another group
ang_adj <- 2*pi/50
# plotting angles used with one entry per line plotted (in radians)
ang_plot <- rep(0,sum(num_non_zero))
# values of lengths to plot
# which() returns values with numbering starting
# going down column 1
lens_plot <- m[which(m > 0)]
# counter used in loop
counter <- 1
# colors used in plotting as index of vectors
# modulo operator returns 0 when divisible by number of groups
# so assign this to the number of groups in this case
col_plot_inds <- which(m>0)%%nrow(m)
col_plot_inds[col_plot_inds == 0] <- nrow(m)
# calcuations of adjustment to angles for each line
# based on the number of groups
# loop over the number of groups
for(i in 1:length(num_non_zero)){
# set of angle shifts, but not demeaned
shifts_unscaled <- ang_adj*seq(0,num_non_zero[i]-1,1)
# shifts demeaned
shifts_scaled <- shifts_unscaled - mean(shifts_unscaled)
# loop over the number if elements in each group
for(j in 1:num_non_zero[i]){
# assigning angle as group angle plus shift
ang_plot[counter] <-ang_plot[counter]+ ang_gp[i] + shifts_scaled[j]
# incrementing counter
counter <- counter + 1
}
}
colors <- brewer.pal(nrow(m), "Dark2")
pdf(file = "overview-plot.pdf", width = 14, height = 10)
par(cex.axis=2)
radial.plot(lens_plot, # using vector not matrix of value
radial.pos=ang_plot, # calcuated in loop
labels=c("A", "B", "C", "D", "E"),
radlab = FALSE,
mar = c(11,8,11,8),
cex = 2,
label.prop = 1.3,
rp.type="rs",
lty = 1,
lwd = 5,
start = 0.65,
show.grid.labels=0,
point.symbols=19,
point.col=alpha(colors[col_plot_inds], 0.8), # calling group index for colors
line.col=alpha(colors[col_plot_inds], 0.5), # calling group index for colors
boxed.radial = FALSE,
show.radial.grid = FALSE,
radial.lim=c(0,max(m)),
grid.bg = "#f0f0f0",
)
#### end of added code ####
legend(
24,8,
c(1:5),
col=colors,
lty=1,
lwd=5,
bty = "n",
title = "Samples",
cex = 2)
dev.off()