2017-05-19 5 views
1

2つの異なるデータセットのデータがあります。最初のdat1には複数のポイントが含まれています。 2番目のdat2には、それぞれのSeason-Speciesグループの最大値のみがdat1に含まれています。 dat1をプロットしようとしていて、各シーズン種グループの最大値を強調表示する大きな図形をプロットしたい、つまりdat2です。ポイントを変数に要約し、ポイントを同じ変数で代入します。

データ:私は、彼らがそれぞれのグループの最大のポイントを概説するように黒い三角形が正しく避けしたいの下にプロットで

library(ggplot2) 
library(dplyr) 

dat1 <- structure(list(Season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("Summer", "Winter"), class = "factor"), 
     Species = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
     2L, 2L, 2L), .Label = c("BHS", "MTG"), class = "factor"), 
     CovGrain = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
     1L, 2L, 3L), .Label = c("CanCov_30", "CanCov_500", "CanCov_1000", 
     "NDVI_30", "NDVI_500", "NDVI_1000", "Slope_30", "Slope_500", 
     "Slope_1000", "SlopeVar_30", "SlopeVar_500", "SlopeVar_1000" 
     ), class = "factor"), Count = c(4L, 19L, 4L, 5L, 3L, 14L, 
     14L, 9L, 9L, 4L, 10L, 9L)), .Names = c("Season", "Species", 
    "CovGrain", "Count"), class = "data.frame", row.names = c(1L, 
    2L, 3L, 14L, 15L, 16L, 30L, 31L, 32L, 45L, 46L, 47L)) 

dat2 <- dat1 %>% group_by(Season, Species) %>% 
    filter(Count == max(Count)) %>% as.data.frame() 


ggplot(dat1, aes(x = CovGrain, y = Count)) + 
    geom_point(aes(fill = Species, color = Species), 
      alpha = 0.5, stroke = 3, size = 3, position=position_dodge(0.5)) + 
    facet_wrap(~Season, scales = "free_x") + 
    scale_shape_manual(values = c(21,22,24)) + 
    scale_fill_manual(values=c("blue", "red")) + 
    geom_point(data = dat2, aes(x = CovGrain, y = Count), shape = 23, 
      stroke = 2, size = 6, position=position_dodge(0.5)) + 
    theme_bw() 

enter image description here

任意の提案が高く評価されています!

答えて

1

"Countは「シーズン」と「種」でグループ化され、最大であるかどうかを示す元のデータにブール変数を作成します。 をFALSE0に設定するにはscale_alpha_manualを使用してください(つまり、「カウント」はmaxにはありません)。 group = Speciesを使用して「種別」で避けてください。

dat1 <- dat1 %>% group_by(Season, Species) %>% 
    mutate(max_count = Count == max(Count)) 

pos <- position_dodge(0.5) 

ggplot(dat1, aes(x = CovGrain, y = Count)) + 
    geom_point(aes(color = Species), position = pos) + 
    geom_point(aes(alpha = max_count, group = Species), shape = 23, size = 6, position = pos) + 
    facet_wrap(~ Season) + 
    scale_alpha_manual(values = c(0, 1), guide = "none") + 
    theme_bw() 

enter image description here

+0

スリック!非常に単純で、より大きなデータに移行することができます。 –

1

これはちょっとハッキーですが、おそらくもっと良い解決策がありますが、1つの方法は、独自のランダム性で新しいx変数を作成することです。ハッキー部分は最初にgeom_point(size = -1)を実行してからx軸を維持するようになっています。だから、どんな意味でもエレガントではありませんが、あなたが望むものを手に入れることができます。

dat1$id <- 1:nrow(dat1) 

dat2 <- dat1 %>% 
    group_by(Season, Species, Cov) %>% 
    filter(Count == max(Count)) %>% 
    as.data.frame() 

randomness <- rnorm(nrow(dat1), 0, 0.5) 
dat1$new_x <- as.integer(dat1$CovGrain) + randomness 
dat2$new_x <- as.integer(dat2$CovGrain) + randomness[dat2$id] 

ggplot(dat1, aes(x = CovGrain, y = Count)) + 
    geom_point(size = -1) + 
    geom_point(aes(x = new_x, fill = Species, color = Species), 
      alpha = 0.5, stroke = 3, size = 3) + 
    facet_wrap(~Season, scales = "free_x") + 
    scale_shape_manual(values = c(21,22,24)) + 
    scale_fill_manual(values=c("blue", "red")) + 
    geom_point(data = dat2, aes(x = new_x, y = Count), shape = 23, 
      stroke = 2, size = 6) + 
    theme_bw() 

enter image description here

関連する問題