2017-09-02 6 views
3

これは比較的単純に思えるはずですが、これを可能にする引数を見つけることができず、GoogleとStackを検索して回答しました。ggplot2のgeom_boxplot内の枠線を削除する

サンプルコード:

library(ggplot2) 
library(plotly) 

dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm(200, mean=.8))) 

p <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() 

p <- ggplotly(p) 

これは最初のグラフを出力し、私は2番目のような何かをしたいと思います。 Fig

私はcolour=condを含めようとしましたが、中央値を取り除きました。

ここ

答えて

1

はgrobsに基づいて無粋なソリューションです:

set.seed(1) 
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), 
        rating = c(rnorm(200),rnorm(200, mean=.8))) 

library(ggplot2) 
library(plotly) 
p <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() 

# Generate a ggplot2 plot grob 
g <- ggplotGrob(p) 

# The first box-and-whiskers grob 
box_whisk1 <- g$grobs[[6]]$children[[3]]$children[[1]] 
pos.box1 <- which(grepl("geom_crossbar",names(box_whisk1$children))) 
g$grobs[[6]]$children[[3]]$children[[1]]$children[[pos.box1]]$children[[1]]$gp$col <- 
    g$grobs[[6]]$children[[3]]$children[[1]]$children[[pos.box1]]$children[[1]]$gp$fill 

# The second box-and-whiskers grob  
box_whisk2 <- g$grobs[[6]]$children[[3]]$children[[2]] 
pos.box2 <- which(grepl("geom_crossbar",names(box_whisk2$children))) 
g$grobs[[6]]$children[[3]]$children[[2]]$children[[pos.box2]]$children[[1]]$gp$col <- 
    g$grobs[[6]]$children[[3]]$children[[2]]$children[[pos.box2]]$children[[1]]$gp$fill 

library(grid) 
grid.draw(g) 

enter image description here

P.S.私の知るところでは、上記のコードはplotlyグラフの生成には使用できません。

2

マルコ・サンドリの答えと同じデータセットを使用して、考えられる2つのハッキングが考えられます。

ハック1。あなたが本当にそれだけで静的ggplotイメージ、plotlyで作業する必要がない場合:

ggplot(dat, aes(x=cond, y=rating, fill=cond)) + 
    geom_boxplot() + 
    geom_boxplot(aes(color = cond), 
       fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0, 
       show.legend = F) 

Hack 1

これは中央値を隠し、基本的に外箱の輪郭だバージョンとオリジナルの箱ひげ図をオーバーレイ(fatten = NULL)、塗りつぶし色(fill = NA)、ひげ(coef = 0)&外れ値(outlier.alpha = 0)。

ただし、プロットしてもうまく機能しないようです。私はggplot2の開発版(プロットされているように)を無駄にテストしました。参照してください以下の出力:

Hack 1 plotly

ハック2。あなたはそれがplotlyで作業する必要がある場合:(ggplot出力は上記と同じである)

ggplot(dat %>% 
     group_by(cond) %>% 
     mutate(rating.IQR = case_when(rating <= quantile(rating, 0.3) ~ quantile(rating, 0.25), 
             TRUE ~ quantile(rating, 0.75))), 
     aes(x=cond, y=rating, fill=cond)) + 
    geom_boxplot() + 
    geom_boxplot(aes(color = cond, y = rating.IQR), 
       fatten = NULL, fill = NA) 

はplotly coef = 0 & output.alpha = 0コマンドを理解していないようですので、このハックはの修正バージョンを作成します変数P30の下のすべてがP25に設定され、上記のすべてがP75に設定されます。これにより、外れ値がなく、ウィスカーがなく、中央値がP75の上限ボックス限界値と一緒になるボックスプロットが作成されます。

それは面倒だが、それはplotlyで動作します。

Hack 2 plotly

関連する問題