某些R函数(例如annotate())不能与coord_radar一起使用,是否有解决方法?

rn0zuynd  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(104)

我使用下面的脚本来创建大量的雷达图进行分析。我想添加一些大小箭头和一个圆圈在中间的雷达图。annotate()和geom_rect(),可能在其他函数中,不适用于coord_radar。
这个问题以前已经提出过,但解决方法(See here)不是,因为我需要对范围使用geom_ribbon()。

library(ggplot2)
library(stringr)

Plot = ggplot(df, 
       aes(x=Column,
           y=mean,
           group=Class)) +
  geom_ribbon(aes(ymin = value1, 
                  ymax = value2), 
              fill = "#1c4f96",
              alpha = 0.50) +
  geom_line() +
  labs(x=NULL,
       y=NULL) +
  coord_radar()

字符串
手动向图中添加圆/箭头/框的解决方法是什么?

sh7euo9m

sh7euo9m1#

我想你搞错了。annotate()在示例代码中运行良好。也许你用错了?
例如,请参阅下面的代码,我在图中添加了一个红色圆圈和一个绿色箭头。这个想法是,图V1V8上的标签对应于数字18。一个圆就是一条从x = 0x = 9的直线,y的值是恒定的。箭头是一条从起点到您希望它指向的位置的直线,其中添加了箭头参数。

coord_radar <- function (theta = "x", start = 0, direction = 1) 
{
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x") 
    "y"
  else "x"
  
  #dirty
  rename_data <- function(coord, data) {
    if (coord$theta == "y") {
      plyr::rename(data, c("y" = "theta", "x" = "r"), warn_missing = FALSE)
    } else {
      plyr::rename(data, c("y" = "r", "x" = "theta"), warn_missing = FALSE)
    }
  }
  theta_rescale <- function(coord, x, scale_details) {
    rotate <- function(x) (x + coord$start) %% (2 * pi) * coord$direction
    rotate(scales::rescale(x, c(0, 2 * pi), scale_details$theta.range))
  }
  
  r_rescale <- function(coord, x, scale_details) {
    scales::rescale(x, c(0, 0.4), scale_details$r.range)
  }
  
  ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
          direction = sign(direction),
          is_linear = function(coord) TRUE,
          render_bg = function(self, scale_details, theme) {
            scale_details <- rename_data(self, scale_details)
            
            theta <- if (length(scale_details$theta.major) > 0)
              theta_rescale(self, scale_details$theta.major, scale_details)
            thetamin <- if (length(scale_details$theta.minor) > 0)
              theta_rescale(self, scale_details$theta.minor, scale_details)
            thetafine <- seq(0, 2 * pi, length.out = 100)
            
            rfine <- c(r_rescale(self, scale_details$r.major, scale_details))
            
            # This gets the proper theme element for theta and r grid lines:
            #   panel.grid.major.x or .y
            majortheta <- paste("panel.grid.major.", self$theta, sep = "")
            minortheta <- paste("panel.grid.minor.", self$theta, sep = "")
            majorr     <- paste("panel.grid.major.", self$r,     sep = "")
            
            ggplot2:::ggname("grill", grid::grobTree(
              ggplot2:::element_render(theme, "panel.background"),
              if (length(theta) > 0) ggplot2:::element_render(
                theme, majortheta, name = "angle",
                x = c(rbind(0, 0.45 * sin(theta))) + 0.5,
                y = c(rbind(0, 0.45 * cos(theta))) + 0.5,
                id.lengths = rep(2, length(theta)),
                default.units = "native"
              ),
              if (length(thetamin) > 0) ggplot2:::element_render(
                theme, minortheta, name = "angle",
                x = c(rbind(0, 0.45 * sin(thetamin))) + 0.5,
                y = c(rbind(0, 0.45 * cos(thetamin))) + 0.5,
                id.lengths = rep(2, length(thetamin)),
                default.units = "native"
              ),
              
              ggplot2:::element_render(
                theme, majorr, name = "radius",
                x = rep(rfine, each = length(thetafine)) * sin(thetafine) + 0.5,
                y = rep(rfine, each = length(thetafine)) * cos(thetafine) + 0.5,
                id.lengths = rep(length(thetafine), length(rfine)),
                default.units = "native"
              )
            ))
          })
}

PlotDataframe =  structure(list(Column = c("V1", "V2", "V3", "V4", "V5", "V6", 
                                           "V7", "V8"), Median = c(0, 18.74554, 18.56488, 0, 12.02048, 11.13803, 
                                                                   0, 11.48958), Quantile1 = c(0, 16.811494, 15.871041, 0, 10.220009, 
                                                                                               2.981794, 0, 10.054176), Quantile3 = c(0, 22.32638, 22.65206, 
                                                                                                                                      0, 15.54919, 29.36191, 0, 13.05328), Class = c(3, 3, 3, 3, 3, 
                                                                                                                                                                                     3, 3, 3)), class = "data.frame", row.names = c(NA, -8L))

library(ggplot2)
library(stringr)

FingerprintTheme = theme(panel.background=element_blank(),
                         axis.text.y = element_blank(),
                         axis.text.x = element_text(size=6),
                         plot.title=element_text(size=8),
                         axis.ticks.y = element_blank(),
                         panel.grid.major.x = element_blank(),
                         panel.grid.major=element_line(colour="grey70"))

Plot = ggplot(PlotDataframe, 
              aes(x=Column,
                  y=Median,
                  group=Class)) +
  geom_ribbon(aes(ymin = Quantile1, 
                  ymax = Quantile3), 
              fill = "#1c4f96",
              alpha = 0.50) +
  geom_line(linewidth=1,
            show.legend=NA,
            colour = "#1c4f96") +
  labs(x=NULL,
       y=NULL) +
  scale_y_continuous(limits = c(-10, 110)) +
  coord_radar() +
  FingerprintTheme

Plot + 
  annotate("line", x = seq(0, 9, len=100), y = -5, colour = "red") +
  annotate("line", 
           arrow = arrow(length = unit(0.2, "cm"), ends = "last", type = "open"), 
           colour = "green", x = 1, y = c(20, 0))

字符串
x1c 0d1x的数据
创建于2023-07-10带有reprex v2.0.2

相关问题