R:如何在barplot中指定条颜色?

发布于 2025-02-04 10:00:45 字数 2150 浏览 2 评论 0原文

我的数据框有2个数值和分类列。负条填充白色,但带有与相应的正杆相同的彩色边界。 现在,我需要拥有具有相同颜色和B类的酒吧,以不同的颜色

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")))

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 = " ")

原始输出

预期输出:

My dataframe has 2 numerical and categorical columns. Negative Bars are filled with white but with with the same coloured borders as corresponding positive bars.
Now I need to have bars with same colour and B category in different colour

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")))

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 = " ")

Original output
enter image description here

Expected output:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2025-02-11 10:00:45

我已将mod_col添加到DF SO绿色和其他所有紫色。然后使用mod_col 填充colorGEOM _ s中。并添加比例_*_ Identity使用这些颜色。

library(tidyverse)

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")),
         mod_col = if_else(model == "B", "green", "purple"))

ggplot() +
  # plot positive with fill and colour based on model
  geom_col(aes(value, tidytext::reorder_within(model, value, category),
               fill = mod_col, color = mod_col), 
           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 = mod_col), 
           data = df[df$sign == "neg", ], 
           fill = "white",
           position = "stack") +
  # the rest is same as OP's code
  scale_fill_identity() +
  scale_colour_identity() +
  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 = " ")

“”

reprex软件包(v2.0.1)

I've added mod_col to the df so B's green and everything else purple. Then used mod_col for the fill and colour in the geom_s. And added scale_*_identity to use those colours.

library(tidyverse)

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")),
         mod_col = if_else(model == "B", "green", "purple"))

ggplot() +
  # plot positive with fill and colour based on model
  geom_col(aes(value, tidytext::reorder_within(model, value, category),
               fill = mod_col, color = mod_col), 
           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 = mod_col), 
           data = df[df$sign == "neg", ], 
           fill = "white",
           position = "stack") +
  # the rest is same as OP's code
  scale_fill_identity() +
  scale_colour_identity() +
  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 = " ")

Created on 2022-06-02 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文