【问题标题】:Closing the lines in a ggplot2 radar / spider chart关闭 ggplot2 雷达/蜘蛛图中的线条
【发布时间】:2015-03-06 11:47:54
【问题描述】:

我需要一种灵活的方式在 ggplot2 中制作雷达/蜘蛛图。从我在 github 和 ggplot2 小组中找到的解决方案中,我已经走到了这一步:

library(ggplot2) 

# Define a new coordinate system 
coord_radar <- function(...) { 
  structure(coord_polar(...), class = c("radar", "polar", "coord")) 
} 
is.linear.radar <- function(coord) TRUE 

# rescale all variables to lie between 0 and 1 
scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))

scaled$model <- rownames(mtcars)    # add model names as a variable 

as.data.frame(melt(scaled,id.vars="model")) -> mtcarsm

ggplot(mtcarsm, aes(x = variable, y = value)) + 
    geom_path(aes(group = model)) +
    coord_radar() + facet_wrap(~ model,ncol=4) + 
    theme(strip.text.x = element_text(size = rel(0.8)), 
          axis.text.x = element_text(size = rel(0.8)))

这行得通,除了行没有关闭。 我认为我能够做到这一点:

mtcarsm <- rbind(mtcarsm,subset(mtcarsm,variable == names(scaled)[1]))
ggplot(mtcarsm, aes(x = variable, y = value)) + 
    geom_path(aes(group = model)) +
    coord_radar() + facet_wrap(~ model,ncol=4) + 
    theme(strip.text.x = element_text(size = rel(0.8)), 
          axis.text.x = element_text(size = rel(0.8)))

为了加入行,但这不起作用。这也不是:

closes <- subset(mtcarsm,variable == names(scaled)[c(1,11)])
ggplot(mtcarsm, aes(x = variable, y = value)) + 
    geom_path(aes(group = model)) +
    coord_radar() + facet_wrap(~ model,ncol=4) + 
    theme(strip.text.x = element_text(size = rel(0.8)), 
          axis.text.x = element_text(size = rel(0.8))) + geom_path(data=closes)

解决不了问题,还产生很多

"geom_path: 每组只包含一个观察值。你需要 调整群体审美?”

消息。 Som,我该如何关闭线路?

/弗雷德里克

【问题讨论】:

  • ggplot(mtcarsm[mtcarsm$model == "Maserati Bora", ], aes(x = variable, y = value)) + geom_path(aes(group = model)) + coord_radar() 关闭此处的行。
  • 现在我很困惑。运行您发布的代码清楚地显示了mpgcarb 之间的差距。这是怎么回事?
  • 忘了说:关闭rbinded 数据框的行。

标签: r ggplot2 radar-chart aesthetics


【解决方案1】:

使用 ggplot2 2.0.0 中新的ggproto 机制,coord_radar 可以定义为:

coord_radar <- function (theta = "x", start = 0, direction = 1) 
{
 theta <- match.arg(theta, c("x", "y"))
 r <- if (theta == "x") 
        "y"
      else "x"
 ggproto("CoordRadar", CoordPolar, theta = theta, r = r, start = start, 
      direction = sign(direction),
      is_linear = function(coord) TRUE)
}

不确定语法是否完美,但它正在工作......

【讨论】:

  • 一段时间后问题再次出现,此解决方案不适用于 ggplot2 v. 3.3.0,,,
【解决方案2】:

这里的代码对于 ggplot2: 2.0.0 来说似乎已经过时了

试试我的包 zmisc:devtools:install_github("jerryzhujian9/ezmisc")

安装后就可以运行了:

df = mtcars
df$model = rownames(mtcars)

ez.radarmap(df, "model", stats="mean", lwd=1, angle=0, fontsize=0.6, facet=T, facetfontsize=1, color=id, linetype=NULL)
ez.radarmap(df, "model", stats="none", lwd=1, angle=0, fontsize=1.5, facet=F, facetfontsize=1, color=id, linetype=NULL)

如果您对里面的内容感到好奇,请查看我的代码github

主要代码改编自http://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.html

【讨论】:

  • 您的软件包是否在您链接到的代码所提供的功能之上添加了功能?还是它只是提供了同一种情节的捷径?
  • 我有一个不同的重新缩放算法,并且在内部还将数据格式转换为 ggplot2 所需的长格式。
【解决方案3】:
  • 解决方案关键因素
    1. melt 之后添加重复的mpgrbind
    2. ggproto 上继承CoordPolar
    3. ggproto 上设置is_linear = function() TRUE

尤其是is_linear = function() TRUE很重要,
因为如果没有,你会得到这样的情节......

使用is_linear = function() TRUE 设置即可获得,

library(dplyr)
library(data.table)
library(ggplot2)

rm(list=ls())

scale_zero_to_one <- 
  function(x) {
    r <- range(x, na.rm = TRUE)
    min <- r[1]
    max <- r[2]
    (x - min) / (max - min)
  }

scaled.data <-
  mtcars %>%
  lapply(scale_zero_to_one) %>%
  as.data.frame %>%
  mutate(car.name=rownames(mtcars)) 

plot.data <-
  scaled.data %>%
  melt(id.vars='car.name') %>%
  rbind(subset(., variable == names(scaled.data)[1]))

# create new coord : inherit coord_polar
coord_radar <- 
  function(theta='x', start=0, direction=1){
    # input parameter sanity check
    match.arg(theta, c('x','y'))

    ggproto(
      NULL, CoordPolar, 
      theta=theta, r=ifelse(theta=='x','y','x'),
      start=start, direction=sign(direction),
      is_linear=function() TRUE)
  }

plot.data %>%
  ggplot(aes(x=variable, y=value, group=car.name, colour=car.name)) + 
  geom_path() +
  geom_point(size=rel(0.9)) +
  coord_radar() + 
  facet_wrap(~ car.name, nrow=4) + 
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.x = element_blank(),
    legend.position = 'none') +
  labs(title = "Cars' Status")
  • 最终结果

【讨论】:

    【解决方案4】:

    对不起,我太笨了。这似乎有效:

    library(ggplot2) 
    
    # Define a new coordinate system 
    coord_radar <- function(...) { 
      structure(coord_polar(...), class = c("radar", "polar", "coord")) 
    } 
    is.linear.radar <- function(coord) TRUE 
    
    # rescale all variables to lie between 0 and 1 
    scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))
    
    scaled$model <- rownames(mtcars)    # add model names as a variable 
    
    as.data.frame(melt(scaled,id.vars="model")) -> mtcarsm
    
    
    mtcarsm <- rbind(mtcarsm,subset(mtcarsm,variable == names(scaled)[1]))
    ggplot(mtcarsm, aes(x = variable, y = value)) + 
        geom_path(aes(group = model)) +
        coord_radar() + facet_wrap(~ model,ncol=4) + 
        theme(strip.text.x = element_text(size = rel(0.8)), 
              axis.text.x = element_text(size = rel(0.8))) 
    

    【讨论】:

      【解决方案5】:

      事实证明,geom_polygom 仍然会在极坐标中生成一个多边形,因此

      # rescale all variables to lie between 0 and 1
      scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))
      scaled$model <- rownames(mtcars)    # add model names as a variable
      # melt the dataframe
      mtcarsm <- reshape2::melt(scaled)
      # plot it as using the polygon geometry in the polar coordinates
      ggplot(mtcarsm, aes(x = variable, y = value)) +
      geom_polygon(aes(group = model), color = "black", fill = NA, size = 1) +
      coord_polar() + facet_wrap( ~ model) +
      theme(strip.text.x = element_text(size = rel(0.8)),
          axis.text.x = element_text(size = rel(0.8)),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank()) +
      xlab("") + ylab("")
      

      完美运行...

      【讨论】:

      • 是的,但我认为大多数人都希望在绘图中的点之间看到直线(不是气泡)。我更喜欢下面的 coord_radar 解决方案的输出。
      【解决方案6】:

      感谢你们的帮助,但它并不能满足我的所有需求。我使用了两个系列的数据进行比较,所以我为马自达取了 mtcars 的子集:

      1. 没有人提到 x 变量的顺序,而 ggplot2 为绘图对这个变量进行排序,但没有对数据进行排序,这使我的图表在第一次尝试时出错。为我应用排序功能是 dplyr::arrange(plot.data, x.variable.name)

      2. 我需要用值注释图表,ggplot2::annotate() 工作正常,但它没有包含在最近的答案中

      3. 在添加 ggplot2::geom_line 之前,上述代码对我的数据无法正常工作

      最后这个代码块做了我的图表:

      scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))
      scaled$model <- rownames(mtcars)
      mtcarsm <- scaled %>%
        filter(grepl('Mazda', model)) %>% 
        gather(variable, value, mpg:carb) %>% 
        arrange(variable)
      
      
      ggplot(mtcarsm, aes(x = variable, y = value)) + 
        geom_polygon(aes(group = model, color = model), fill = NA, size = 1) +
        geom_line(aes(group = model, color = model), size = 1) + 
        annotate("text", x = mtcarsm$variable, y = (mtcarsm$value + 0.05), label = round(mtcarsm$value, 2), size = 3) +
        theme(strip.text.x = element_text(size = rel(0.8)),
              axis.text.x = element_text(size = rel(1.2)),
              axis.ticks.y = element_blank(),
              axis.text.y = element_blank()) +
        xlab("") + ylab("") +
        guides(color = guide_legend()) +
        coord_radar()
      

      希望对某人有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        相关资源
        最近更新 更多