1

ファセットボックスプロットの塗りつぶしをgeom_rectで色付けしています。塗りつぶしの色を scale_fill_manual で手動で設定すると、boxplot の塗りつぶしと geom_rect の塗りつぶしを区別できず、凡例が台無しになります。

イムグル

mtcars$type <- "Small"
mtcars$type[mtcars$cyl >= 6] <- "Medium"
mtcars$type[mtcars$cyl >= 8] <- "Big"
mtcars$type <- factor(mtcars$type)

mtcars$typeColor <- "black"
mtcars$typeColor[mtcars$cyl >= 8] <- "white"
mtcars$typeColor <- factor(mtcars$typeColor)

p <- ggplot(mtcars, aes(factor(gear), mpg, fill=factor(gear)))
p <- p + scale_x_discrete()
p <- p + geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill=(typeColor)))
p <- p + geom_boxplot()
p <- p + facet_grid(. ~ type)
p <- p + scale_fill_manual( values = c("black" = "black","white" = "white","3" = "green","4" = "red","5" = "blue"), limits=c("3","4","5"))
show(p)

それを防ぐ方法のヒントはありますか?

4

1 に答える 1

5

ガイドgeom_rectを表示しないように指示:

ggplot(mtcars, aes(factor(gear), mpg, fill=factor(gear))) + 
        scale_x_discrete() + 
        geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill=(typeColor)),
                  show.legend = FALSE) + 
        geom_boxplot() + 
        facet_grid(. ~ type) + 
        scale_fill_manual(values = c("black" = "black","white" = "white","3" = "green","4" = "red","5" = "blue"), 
                          limits=c("3","4","5"))
于 2013-10-17T17:06:42.767 に答える