每个条形图具有不同背景颜色的条形图 ggplot

Nad*_* M. 1 r background-color bar-chart ggplot2

有人可以帮我生成一个包含所有显示属性的条形图,并为每对条形图添加不同的背景颜色吗?
SE 是误差线。

Mat <- matrix(c(1.97, 0.61, 0.06, 0.06, 0.61, 0.51, 0.03, 0.25, 2.25, 1.36, 0.15, 0.17, 1.19, 1.41, 0.04, 0.25),ncol=4,byrow=TRUE)

rownames(Mat) <- c("Cognitive Strategies","Motivational Strategies","SE Cognitive Strategies","SE Motivational Strategies")

colnames(Mat) <- c("No Problems","Motivational Problems","Knowledge Problems","Both Problems")

Mat <- as.data.frame(Mat)


barplot(as.matrix(Mat[1:2, 1:4]), main=NULL, ylab = "Number of \n motivational and cognitive Strateiges (CI 95%)", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=c("darkblue","red"))
Run Code Online (Sandbox Code Playgroud)

我不知道如何更改每对条的背景颜色,特别是如何将其与错误条和图例放在同一代码中。

在此输入图像描述

Paw*_*ros 5

Nadine,
首先ggplot喜欢长格式的数据。您可以像下面这样转换您的数据:

library(tidyverse)
library(wrapr)

Mat_long <-
  Mat %>%
  as_tibble() %>%
  mutate(
    Group = c('No Problems','Motivational Problems','Knowledge Problems','Both Problems'),
    xpos = row_number()
  ) %>%
  unite('Cognitive Strategies', c('Cognitive Strategies', 'SE Cognitive Strategies')) %>%
  unite('Motivational Strategies', c('Motivational Strategies', 'SE Motivational Strategies')) %>%
  gather(Type, val, `Motivational Strategies`:`Cognitive Strategies`) %>%
  separate(val, c('val', 'SE'), sep = '_') %>%
  mutate_at(4:5, as.numeric)
Run Code Online (Sandbox Code Playgroud)

Mat_long如下所示:

 A tibble: 8 x 5
  Group                  xpos Type                      val    SE
  <chr>                 <int> <chr>                   <dbl> <dbl>
1 No Problems               1 Motivational Strategies  0.61  0.06
2 Motivational Problems     2 Motivational Strategies  0.51  0.25
3 Knowledge Problems        3 Motivational Strategies  1.36  0.17
4 Both Problems             4 Motivational Strategies  1.41  0.25
5 No Problems               1 Cognitive Strategies     1.97  0.06
6 Motivational Problems     2 Cognitive Strategies     0.61  0.03
7 Knowledge Problems        3 Cognitive Strategies     2.25  0.15
8 Both Problems             4 Cognitive Strategies     1.19  0.04
Run Code Online (Sandbox Code Playgroud)

现在你可以做一个像这样的漂亮的情节:

在此输入图像描述

用这个代码:

Mat_long %.>%
  ggplot(
    data = .,
    aes(
      x = xpos,
      y = val,
      fill = Type
  )) +
  geom_rect(aes(
      xmin = xpos - .5,
      xmax = xpos + .5,
      ymin = -Inf,
      ymax = Inf,
      fill = Group
    ),
    alpha = .2
  ) +
  geom_col(
    position = position_dodge(.5),
    width = .5
  ) +
  geom_errorbar(aes(
      ymin = val - SE,
      ymax = val + SE
    ),
    position = position_dodge(.5),
    width = .2
  ) +
  geom_text(aes(
      y = val + SE + .1,
      label = val %>% str_replace('\\.', ',')
    ),
    position = position_dodge(.5)
  ) +
  scale_fill_manual(
    values = c(
      '#fb929e',  # Both Problems
      '#235784',  # Cognitive Strategies
      '#ffdfdf',  # Knowledge Problems
      '#fff6f6',  # Motivational Problems
      '#7a5d7e',  # Motivational Strategies
      '#aedefc'   # No Problems
    ),
    breaks = c(
      'Cognitive Strategies',
      'Motivational Strategies'
    )
  ) +
  scale_x_continuous(
    breaks = .$xpos %>% unique(),
    labels = .$Group %>% unique(),
    expand = c(0, 0)
  ) +
  scale_y_continuous(
    labels = function(x) str_replace(x, '\\.', ','),
    limits = c(0, 2.6),
    expand = c(0, 0)
  ) +
  ylab('Number of Motivational and Cognitive Strategies\n(CI 95%)') +
  theme_light() +
  theme(
    legend.position = 'top',
    legend.title = element_blank(),
    legend.spacing.x = unit(1, 'mm'),
    axis.title.x = element_blank()
  )
Run Code Online (Sandbox Code Playgroud)

但你的问题的答案是......你可以为每个组设置不同的背景颜色geom_rect。问题是geom_rect需要数值。因此,您必须向每个组添加数字(xpos在我的示例中),然后使用 更改 x 轴的标签scale_x_continous。您可以使用's管道传递Mat_long数据帧,以便稍后通过调用 dot 来使用它:wrapr%.>%scale_x_continous.

  scale_x_continuous(
    breaks = .$xpos %>% unique(),
    labels = .$Group %>% unique(),
    ...
  )
Run Code Online (Sandbox Code Playgroud)