我的df有两个数值变量(正值和负值)和两个分类变量.因为我想用相同 colored颜色 的shape/boundaries绘制负条,所以我在数据框中手动指定 colored颜色 ,并使用适当的代码.我的问题是如何避免预先指定 colored颜色 ,让R使用随机 colored颜色 ,但保持white space inside the negative bars

df <- data.frame(model  = c("A","B","C","D","B","C"),
                  category = c("origin", "origin","origin","abroad","abroad","abroad"),
                 pos = c(40,50,45,100,105,80),
                 neg = c(-10,-5,-4,-16,-7,-2),
                 Colour = c("chocolate","deeppink4","yellow","steelblue3","deeppink4","yellow"))

Colour <- as.character(df$Colour)
Colour <- c(Colour,"white")
names(Colour) <- c(as.character(df$model),"white")



df <- df %>% pivot_longer(., cols=c('pos','neg'),
                           names_to = 'sign') %>% 
  mutate(Groups = paste(category, model),
         sign = factor(sign, levels = c("neg", "pos")))



bar2 <- ggplot(df, aes(value, tidytext::reorder_within(model, value, category),
                fill = ifelse(sign == "neg", "white", model), 
                color = model))+
  geom_col(position = "stacked") +
  scale_fill_manual(values = Colour, breaks = df$model) +
  scale_color_manual(values = Colour, breaks = df$model) +
  tidytext::scale_y_reordered() +
  labs(fill = "model") +
  facet_grid(category ~ ., switch = "y",scales = "free_y") +
  theme(axis.text.x = element_text(angle = 90),
        strip.background = element_rect(fill = "white"),
        strip.placement = "outside",
        strip.text.y.left = element_text(angle = 0),
        panel.spacing = unit(0, "lines"))+theme(legend.position="none") + 
        labs( title = "BarPlot",
              subtitle = "changes",
              y = " ") 

bar2

enter image description here

推荐答案

将正极和负极条绘制为单独的图层:

ggplot() +
  # plot positive with fill and colour based on model
  geom_col(aes(value, tidytext::reorder_within(model, value, category),
               fill = model, color = model), 
           data = df[df$sign == "pos", ],
           position = "stack") +
  # plot negative with colour from based on model, but fill fixed as "white"
  geom_col(aes(value, tidytext::reorder_within(model, value, category),
               color = model), 
           data = df[df$sign == "neg", ], 
           fill = "white",
           position = "stack") +
  # the rest is same as OP's code
  tidytext::scale_y_reordered() +
  labs(fill = "model") +
  facet_grid(category ~ ., switch = "y",scales = "free_y") +
  theme(axis.text.x = element_text(angle = 90),
        strip.background = element_rect(fill = "white"),
        strip.placement = "outside",
        strip.text.y.left = element_text(angle = 0),
        panel.spacing = unit(0, "lines")) + 
  theme(legend.position="none") + 
  labs( title = "BarPlot",
        subtitle = "changes",
        y = " ") 

enter image description here

R相关问答推荐

如何根据R中其他列的值有条件地从列中提取数据?

使用Facet_WRAP时更改框图中线的 colored颜色

使用R中的dist()迭代ID匹配的欧几里德距离

R -使用矩阵reshape 列表

按列中显示的配对组估算NA值

R+reprex:在呈现R标记文件时创建可重现的示例

观察器中的inaliateLater的位置

如何使用FormC使简单算术运算得到的数字是正确的?

在不对R中的变量分组的情况下取两行的平均值

如果条件匹配,则使用Mariate粘贴列名

如何根据未知数的多列排除重复行

按组跨多列创建伪变量

使用LAG和dplyr执行计算,以便按行和按组迭代

修复标签重叠和ggploy内的空间

用从先前非NA值开始的递增序列替换NA值

错误:您是否拼错了`ggplot()`中的`data`参数

如何根据索引将R中的列表设置为子集?

为什么as.numic()函数在R中产生不同的结果?

将嵌套列表转换为R中的数据帧

如何在R中组合多个地块?