GGplot颜色轮廓

时间:2020-06-06 20:55:47

标签: r ggplot2

data(mtcars)
library(ggplot2)
ggplot(mtcars, aes(x = reorder(row.names(mtcars), mpg), y = mpg, fill = factor(cyl))) +
        geom_bar(stat = "identity")

这将ggplot带有实心填充的条,但是如果我希望对某些度量使用与轮廓相同的填充颜色,而对另一些度量使用实心填充,该怎么办。例如,如果“ am”等于1,则为实心填充,但如果“ am”等于0,则为实心填充,如下例所示:

enter image description here

3 个答案:

答案 0 :(得分:4)

根据逻辑条件删除填充的一种方法是将这些值更改为NA

library(tidyverse)

d <- head(mtcars) %>%
  rownames_to_column() %>%
  # make a new variable for fill
  # note: don't use ifelse on a factor!
  mutate(cyl_fill = ifelse(am == 0, NA, cyl),
         # now make them factors 
         # (you can do this inside ggplot, but that is messy)
         cyl = factor(cyl),
         cyl_fill = factor(cyl_fill, levels = levels(cyl)))

# plot 
p <- ggplot(d) +
  aes(x = rowname, 
      y = mpg, 
      color = cyl,
      fill = cyl_fill
  ) +
  geom_bar(stat = "identity") +

  theme(axis.text.x = element_text(angle = 90))

# change the fill color of NA values
p +   scale_fill_discrete(drop=FALSE, na.value="white")  

enter image description here

如果您希望NA填充值为空并从图例中省略:

# omit the fill color of NA values
# note: drop=FALSE is still needed to keep the fill and (outline) color values the same
p +   scale_fill_discrete(drop=FALSE, na.translate = F)  

enter image description here

您可以用相同的方式更改轮廓的颜色(例如cyl_color = ifelse(am != 0, NA, Cyl)),但是如果要指定白色或黑色之类的颜色,则(应该)它会出现在图例中。您可以通过在主图层后面绘制非美学图层来尝试绕过这些明智的默认设置,但这通常很丑陋:

head(mtcars) %>%
  rownames_to_column() %>%
  mutate(cyl_fill = ifelse(am == 0, NA, cyl),
         cyl_color = ifelse(am != 0, NA, cyl),
         cyl = factor(cyl),
         cyl_fill = factor(cyl_fill, levels = levels(cyl)),
         cyl_color = factor(cyl_color, levels = levels(cyl))) %>% 
  ggplot() +
  aes(x = rowname, 
      y = mpg, 
      color = cyl_color,
      fill = cyl_fill
  ) +
  geom_bar(stat = "identity", color = "black") + # NON-AES LAYER FIRST
  geom_bar(stat = "identity") + # Covers up the black except where omitted
  theme(axis.text.x = element_text(angle = 90))+
  scale_fill_discrete(drop=FALSE, na.translate = F) + 
  scale_color_discrete(drop=FALSE, na.translate = F) 

enter image description here

答案 1 :(得分:1)

您可以为填充和颜色变量的每个级别分配所需的颜色。例如:

library(tidyverse)

mtcars %>% 
  rownames_to_column() %>% 
  arrange(mpg) %>% 
  mutate(rowname=factor(rowname, levels=rowname)) %>% 
  ggplot(aes(x = rowname, y = mpg, fill = factor(am), colour=factor(cyl))) +
    geom_col(size=1) +
    scale_fill_manual(values=c("0"="white", "1"="red")) +
    scale_color_manual(values=c("4"="blue", "6"="orange", "8"="white")) +
    theme_classic() +
    theme(axis.text.x=element_text(angle=-90, vjust=0.5, hjust=0)) 

enter image description here

答案 2 :(得分:1)

也许可以,我们可以

library(dplyr)
library(ggplot2)
mtcars %>% 
  mutate(new = case_when(am == 1 ~ factor(cyl)),
    new1 = case_when(am !=1 ~ factor(cyl))) %>% 
 ggplot(aes(x = reorder(row.names(mtcars), mpg), y = mpg, 
          fill = new, color = new1)) + 
    geom_bar(stat = 'identity') + 
    scale_fill_discrete(na.value= NA) + # similar to Devin Judge-Lord post
    theme_classic() +       
    theme(axis.text.x=element_text(angle=-90, vjust=0.5, hjust=0))