使用雷达/极坐标图来描绘各个数据点

这是我之前的帖子:
How to measure the area of a polygon in ggplot2?

我现在要做的是将得到的雷达图表用作完全独立的散点图中的数据点,或者可以保存对象并稍后在图形上使用它来描绘实际数据的形状.

我有很多文件,它们通常看起来像这样.它们的编号范围从1到多,并且分数始终为降序格式.即#1总是在任何给定文件中贡献最多.

myfile
01 0.31707317
02 0.12195122
03 0.09756098
04 0.07317073
05 0.07317073
06 0.07317073
07 0.07317073
08 0.07317073
09 0.04878049
10 0.04878049

在这里,我想分别为每个文件绘制一个雷达图,只是得到没有网格的形状. ggradar是我找到的最接近和最好的.

ggradar(as.matrix(t(radar)), group.point.size = 2, grid.line.width = 0, grid.max = 1.0, grid.mid = 0, 
    grid.min = 0, axis.line.colour = "white", axis.label.size = 0, grid.label.size = 0, 
    centre.y = 0, background.circle.colour = "white", group.colours = "black", group.line.width = 1) 

《使用雷达/极坐标图来描绘各个数据点》

现在,当我想将这些形状用作数据点时,问题就出现了.

@brian善意地评论并暗示如何处理这个问题.

R::ggplot2::geom_points: how to swap points with pie charts?

Small ggplot2 plots placed on coordinates on a ggmap

受这些评论的启发,我现在可以将图表与文件名一起保存为一列,并使用相同的管道继续添加新文件.

  for (file in file_list){
#eliminate the empty files (they contain only the header)
if (file.size(file) > 420){
  # if the merged dataset does exist, append to it
  if (exists("dfradar")){
    radarfile <-read.table(file, header=TRUE, sep="\t")
    radarfile1 <- as.data.frame(as.numeric(radarfile[,3]))
    rownames(radarfile1) <- c(1:nrow(radarfile))
    dfradar1 <- ggradar(t(radarfile1), group.point.size = 1, grid.line.width = 0, grid.max = 1, grid.mid = 0, 
                        grid.min = 0, axis.line.colour = "white", axis.label.size = 0, grid.label.size = 0, 
                        centre.y = 0, background.circle.colour = "white", group.colours = "black", group.line.width = 0.5) + 
                    theme(legend.position = "none")      
    dfradar1 <- cbind(substring(file,11), dfradar1)
    dfradar <- rbind(dfradar, dfradar1)
  }  
  # if the merged dataset doesn't exist, create it
  if (!exists("dfradar")){
    radarfile <- read.table(file, header=TRUE, sep="\t")
    radarfile1 <- as.data.frame(as.numeric(radarfile[,3]))
    rownames(radarfile1) <- c(1:nrow(radarfile))
    dfradar <- ggradar(t(radarfile1), group.point.size = 1, grid.line.width = 0, grid.max = 1, grid.mid = 0, 
                        grid.min = 0, axis.line.colour = "white", axis.label.size = 0, grid.label.size = 0, 
                        centre.y = 0, background.circle.colour = "white", group.colours = "black", group.line.width = 0.5) + 
                theme(legend.position = "none")
    dfradar <- cbind(substring(file,11), dfradar)
    rm(radarfile)
    rm(radarfile1)
  }
} }

现在,当我想要保存凹凸时,我不能;我明白了:

“Error in mutate_impl(.data, dots) : Evaluation error: attempt to
apply non-function.”

要么:

“Error in plot_clone(plot) : attempt to apply non-function”

# merge the df with another df containing all other 
# variables that I wanna use in my scatterplot
dfradar_merge <- merge(dfradar, Cases, all=FALSE)
dfradar_merge <-  dfradar_merge %>% mutate(radargrobs = list(annotation_custom(ggplotGrob(radarplots)), 
xmin = as.numeric(Gender), xmax = as.numeric(Gender)*1.2, 
ymin = as.numeric(Age) , ymax = as.numeric(Age)*1.2)))

我想如果我能直接得到这个部分并将变量作为xmin和ymin传递,我应该能够做我需要的……

任何想法都非常感谢.这对我来说很头疼.

最佳答案 我提出了以下想法.我使用ggradar包来创建图形.然后,我用ggplot_build()查看了留在图形后面的数据框.似乎图形背后有13个列表.我需要调查哪个列表是包含多边形数据的正确列表.我发现第6个列表是您要提取并保存以供将来使用的列表.让我告诉你我做了什么.

library(dplyr)
library(ggradar)
library(scales)

# I modified the code from https://github.com/ricardo-bion/ggradar to get a graphic.

mtcars %>%
mutate_all(rescale) %>%
mutate(group = rownames(mtcars)) %>%
slice(5:9) %>%
select(1:4) -> mtcars_radar

g <- ggradar(mtcars_radar)

上面的代码生成以下图形.我的电脑或其他东西有问题,我看不到完整的图形.但这不是这个问题的问题.

《使用雷达/极坐标图来描绘各个数据点》

现在,我们希望将数据保留在图形之后并寻找正确的数据.

foo <- ggplot_build(g)$data

# This is the 6th list in foo.

 $:'data.frame':   20 obs. of  8 variables:
  ..$colour  : chr [1:20] "#FF5A5F" "#FF5A5F" "#FF5A5F" "#FF5A5F" ...
  ..$x       : num [1:20] 0 0.72 -0.687 0 0 ...
  ..$y       : num [1:20] 1.111 -0.416 -0.397 1.111 0.611 ...
  ..$group   : atomic [1:20] 1 1 1 1 2 2 2 2 3 3 ...
  .. ..- attr(*, "n")= int 5
  ..$PANEL   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
  ..$size    : num [1:20] 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 ...
  ..$linetype: num [1:20] 1 1 1 1 1 1 1 1 1 1 ...
  ..$alpha   : logi [1:20] NA NA NA NA NA NA ...

您想要将此列表提取为数据框.我能够发现这个列表的原因是我正在关注颜色信息(例如,FF5A5F).

mydf <- foo[[6]]

    colour          x          y group PANEL size linetype alpha
1  #FF5A5F  0.0000000  1.1111111     1     1  1.5        1    NA
2  #FF5A5F  0.7203077 -0.4158698     1     1  1.5        1    NA
3  #FF5A5F -0.6868360 -0.3965450     1     1  1.5        1    NA
4  #FF5A5F  0.0000000  1.1111111     1     1  1.5        1    NA
5  #FFB400  0.0000000  0.6111111     2     1  1.5        1    NA
6  #FFB400  0.4286803 -0.2474987     2     1  1.5        1    NA
7  #FFB400 -0.2584135 -0.1491951     2     1  1.5        1    NA
8  #FFB400  0.0000000  0.6111111     2     1  1.5        1    NA
9  #007A87  0.0000000  1.1111111     3     1  1.5        1    NA
10 #007A87  0.7203077 -0.4158698     3     1  1.5        1    NA
11 #007A87 -0.4726248 -0.2728700     3     1  1.5        1    NA
12 #007A87  0.0000000  1.1111111     3     1  1.5        1    NA
13 #8CE071  0.0000000  0.1111111     4     1  1.5        1    NA
14 #8CE071  0.2467912 -0.1424850     4     1  1.5        1    NA
15 #8CE071 -0.2278119 -0.1315273     4     1  1.5        1    NA
16 #8CE071  0.0000000  0.1111111     4     1  1.5        1    NA
17 #7B0051  0.0000000  0.1111111     5     1  1.5        1    NA
18 #7B0051  0.2595364 -0.1498434     5     1  1.5        1    NA
19 #7B0051 -0.1268266 -0.0732234     5     1  1.5        1    NA
20 #7B0051  0.0000000  0.1111111     5     1  1.5        1    NA

让我们确认这个数据是否再现了ggradar图形中的五个三角形.

gg <- ggplot(data = mydf, aes(x = x, y = y, group = group, color = factor(group))) +
      geom_path(show.legend = FALSE) +
      theme_bw()

《使用雷达/极坐标图来描绘各个数据点》

总之,您希望破解ggplot数据以提取所需的数据.此方法允许您保存所需“形状”的数据.我希望这就是你所追求的.

点赞