【问题标题】:Using radar/polar graphs to depict individual datapoints使用雷达/极坐标图描绘单个数据点
【发布时间】:2018-05-22 03:21:10
【问题描述】:

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

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

我有很多文件,它们通常看起来像这样。它们的列数从 1 到 many 不等,分数始终采用降序格式。即 #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

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

  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)
  }
} }

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

“mutate_impl(.data, dots) 中的错误:评估错误:尝试 应用非函数。”

或:

“plot_clone(plot) 中的错误:尝试应用非函数”

# 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 传递,我应该能够做我需要的......

非常感谢任何想法。这让我很头疼。

【问题讨论】:

    标签: r plot ggplot2 visualization radar-chart


    【解决方案1】:

    我想出了以下想法。我使用 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 数据来提取你想要的数据。此方法允许您为所需的“形状”保存数据。我希望这就是你所追求的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多