r - 如何使用ggplot2中的ggproto函数修改绘图的图例?

标签 r plot ggplot2 legend ggproto

在主成分分析中,我在 prcomp() 中提取成分结果作为散点图。我想添加组名称的标签,然后使用 MASS::cov.trob() 在每个组中计算每个组的中心。我使用 ggplot2::ggproto() 创建新的统计数据并重建新的几何图形,以显示每个组的标签。然而,新图的图例不合理,因为它应该是点图例而不是字符图例。我尝试过多种变体,但似乎都不起作用。有任何想法吗?这是我的代码:

# data
data(Cars93, package = "MASS")
car_df <- Cars93[, c(3, 5, 13:15, 17, 19:25)]
car_df <- subset(car_df, Type == "Large" | Type == "Midsize" | Type == "Small")
x1 <- mean(car_df$Price) + 2 * sd(car_df$Price)
x2 <- mean(car_df$Price) - 2 * sd(car_df$Price)
car_df <- subset(car_df, Price > x2 | Price < x1)
car_df <- na.omit(car_df)

# Principal Component Analysis
car.pca <- prcomp(car_df[, -1], scale = T)
car.pca_pre <- cbind(as.data.frame(predict(car.pca)[, 1:2]), car_df[, 1])
colnames(car.pca_pre) <- c("PC1", "PC2", "Type")
head(car.pca_pre)

# create a new stat
library(ggplot2)
StatLabel <- ggproto("StatLabel" ,Stat,
               compute_group = function(data, scales) {
                library(MASS)
                df <- data.frame(data$x,data$y)
                center <- cov.trob(df)$center
                names(center)<- NULL 
                center <- t(as.data.frame(center))
                center <- as.data.frame(cbind(center))
                colnames(center) <- c("x","y")
                rownames(center) <- NULL
                return(center)
                },
                required_aes = c("x", "y")
)

stat_label <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", 
    ..., parse = FALSE, nudge_x = 0, nudge_y = 0, label.padding = unit(0.15, 
        "lines"), label.r = unit(0.15, "lines"), label.size = 0.1, 
    na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) 
{
    if (!missing(nudge_x) || !missing(nudge_y)) {
        if (!missing(position)) {
            stop("Specify either `position` or `nudge_x`/`nudge_y`", 
                call. = FALSE)
        }
        position <- position_nudge(nudge_x, nudge_y)
    }
    layer(data = data, mapping = mapping, stat = StatLabel, geom = GeomLabel, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(parse = parse, label.padding = label.padding, 
            label.r = label.r, label.size = label.size, na.rm = na.rm, 
            ...))
}

# plot
ggplot(car.pca_pre, aes(PC1, PC2, color = Type)) + geom_point() + 
stat_label(aes(label = Type))

enter image description here

最佳答案

我认为让新的统计数据在图例中显示点不是很自然,因为它没有绘制任何点。按照目前的情况,当点和文本都有组合图例时,ggplot 似乎优先于文本图例。最简单的解决方案是默认情况下不为标签统计提供图例。

您可以将函数更改为默认值 show.legend = FALSE,然后您的绘图将显示点图例。

stat_label <- function (mapping = NULL, 
                        data = NULL, 
                        stat = "identity", 
                        position = "identity", 
                        ..., 
                        parse = FALSE, 
                        nudge_x = 0, nudge_y = 0, 
                        label.padding = unit(0.15, "lines"), 
                        label.r = unit(0.15, "lines"), 
                        label.size = 0.1, 
                        na.rm = FALSE, 
                        show.legend = FALSE,       ## <--- change
                        inherit.aes = TRUE) 
{
  if (!missing(nudge_x) || !missing(nudge_y)) {
    if (!missing(position)) {
      stop("Specify either `position` or `nudge_x`/`nudge_y`", 
           call. = FALSE)
    }
    position <- position_nudge(nudge_x, nudge_y)
  }
  layer(data = data, mapping = mapping, stat = StatLabel, geom = GeomLabel, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(parse = parse, label.padding = label.padding, 
                      label.r = label.r, label.size = label.size, na.rm = na.rm, 
                      ...))
}

# plot
ggplot(car.pca_pre, aes(PC1, PC2, color = Type)) + geom_point() + 
  stat_label(aes(label = Type))

enter image description here

关于r - 如何使用ggplot2中的ggproto函数修改绘图的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42550039/

相关文章:

r - 在 R 中进行 knn 分类后,如何获得每个测试用例的预测列表?

读取 R 的电子邮件附件

matlab - 如何在 Matlab 中绘制 int64 值?

r - 如何使直方图条在 R 中的 Plotly 中具有不同的颜色

R - stat_compare_means 从 Kruskal-Wallis 测试返回不同的值

r - 计算MACD和错误处理时,数字超出有效范围

r - 在 Ubuntu 上使用 R 获取抗锯齿绘图

python - 在实时绘图期间在 matplotlib 中移动 x Axis (python)

r - 面网格导致 R 中不同的行高

r - 添加(或覆盖)填充美学到 ggplot2 自动绘图函数