2017-03-18 36 views
1

我想独立使用色标,即连续和分类比例。ggplot2 - 使用不同类型的色标

我心里有一个特定的应用程序,其中我有一个简单的情节像这里

mtcars %>% 
    mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>% 
    ggplot(aes(x = mpg, y = wt)) + 
    geom_point(aes(color = drat)) + 
    facet_wrap(~ cyl) 

enter image description here

但我也想highlight border, like in the answer of Seth_P, of a specific condition(我dont't想用填充面的背景!)。例如

mtcars %>% 
    mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>% 
    ggplot(aes(x = mpg, y = wt)) + 
    geom_point() + 
    geom_rect(xmin = -Inf, xmax = Inf, show.legend = FALSE, 
      ymin = -Inf, ymax = Inf, aes(col = cylcol), fill = "#00000000") + 
    facet_wrap(~ cyl) 

enter image description here

现在我想以 “合” 这两个,像例如:

mtcars %>% 
    mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>% 
    ggplot(aes(x = mpg, y = wt)) + 
    geom_point(aes(color = drat)) + 
    geom_rect(xmin = -Inf, xmax = Inf, show.legend = FALSE, 
      ymin = -Inf, ymax = Inf, aes(col = cylcol), fill = "#00000000") + 
    facet_wrap(~ cyl) 

这就产生了一个错误Error: Discrete value supplied to continuous scale。这使得一方面感觉到,但另一方面,因为两者都使用自变量,所以我想使用“不同的颜色比例”。我可以使用覆盖图,如here, where facet colors are plotted over the plot,但我会非常感谢一个更简单的解决方案。我知道specifying fill and color separately - 但这不是目标。我真的想在不同的尺度上使用颜色。有什么建议么 ?

回答

1

我不知道有任何方式同时具有离散和连续的尺度。但是,你可以解决它通过使用geom_point形状由fill而不是colour填充:

library(ggplot2) 
library(dplyr) 
mtcars %>% 
    mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>% 
    ggplot(aes(x = mpg, y = wt)) + 
    geom_point(aes(fill = drat), shape = 21) + 
    geom_rect(aes(colour = cylcol), xmin = -Inf, xmax = Inf, 
    ymin = -Inf, ymax = Inf, fill = NA) + 
    facet_wrap(~ cyl) 

demo plot

下面是第二种方式,这可能是有点过于复杂,但通过拆分工作数据并有效地进行手动分割。宽度需要根据y轴标签大小,图例大小和绘图大小进行手动调整。

library(ggplot2) 
library(dplyr) 
library(purrr) 
lims <- list(x = range(mtcars$mpg), y = range(mtcars$wt)) 

make_plot <- function(data) { 
    cyl <- data$cyl[1] 
    if (cyl == 6) { 
    rec_col <- "light blue" 
    } else { 
    rec_col <- "red" 
    } 
    p <- data %>% 
    ggplot(aes(x = mpg, y = wt)) + 
    geom_point(aes(colour = drat)) + 
    geom_rect(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, 
       fill = NA, colour = rec_col) + 
    xlim(lims$x) + ylim(lims$y) + 
    facet_wrap(~ cyl) 
    if (cyl != 4) { 
    p <- p + theme(axis.title.y = element_blank(), axis.ticks.y = element_blank(), 
        axis.text.y = element_blank()) 
    } 
    if (cyl != 8) { 
    p <- p + theme(legend.position = "none") 
    } 
    if (cyl != 6) { 
    p <- p + theme(axis.title.x = element_text(colour = "white")) 
    } 
    p 
} 

mtcars %>% 
    split(.$cyl) %>% 
    map(make_plot) %>% 
    grid.arrange(grobs = ., layout_matrix = matrix(1:3, nrow = 1), 
       widths = c(0.32, 0.28, 0.4)) 

demo plot 2

最后,值得一提的,这是considered three years ago但哈德利感到没有足够的带宽发展。

+0

谢谢。在这篇文章中,我已经写道,我知道使用填充和颜色电子音。但是,在我的实际情节中(这里没有显示),我已经使用填充来填充其他类型的信息。此解决方法也可以工作,但对我无效。 – Drey

+0

对不起@Drey。我知道你知道单独的美学,只是人们常常不知道'geom_point'的一些形状使用填充颜色而不是颜色。 –

相关问题