2017-03-02 84 views
1

在主成分分析中,我提取出散點圖的prcomp()的分量結果。我想添加組名稱的標籤,然後使用MASS::cov.trob()在每個組中計算每個組的中心。我使用ggplot2::ggproto()創建新的統計數據並重新構建新的幾何圖形,以顯示每個組的標籤。然而,新圖形具有不合理的傳說,因爲它應該是一個點傳說而不是人物傳說。我已經嘗試了多種變化,但它們都沒有效果。有任何想法嗎?這裏是我的代碼:如何使用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

回答

1

我不認爲這會很自然地有新的統計顯示分的傳奇,因爲它沒有任何積點。正如所看到的,當點和文本都具有組合圖例時,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

+0

謝謝!我完成了我在圖表中熟悉的新geom,顯示在'adegraphics :: s.class' [link](http://enterotype.embl.de/images/between.png) –