Barplot ggplot с разным цветом фона для пары баров - PullRequest
0 голосов
/ 29 января 2019

Может ли кто-нибудь помочь мне создать барплот со всеми отображаемыми свойствами и, кроме того, с разным цветом фона для каждой пары полос?
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"))

Я не знаю, как изменить цвет фона для каждой пары столбцов и особенно поместить его в один и тот же код с панелями ошибок и легендой.

enter image description here

Ответы [ 2 ]

0 голосов
/ 30 января 2019

Надин,
прежде всего 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)

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

Теперь вы можете сделать красивый сюжет, подобный этому:

enter image description here

с этим кодом:

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

Но ответ на ваш вопрос: ... Вы можете сделать разные цвета фона для каждой группы с помощью geom_rect.Проблема в том, что geom_rect нужны числовые значения.Таким образом, вы должны добавить номер к каждой группе (xpos в моем примере), а затем изменить метки оси X на scale_x_continous.Вы можете передать Mat_long фрейм данных с wrapr '%.>% pipe, чтобы использовать его позже в scale_x_continous, вызвав точку .:

  scale_x_continuous(
    breaks = .$xpos %>% unique(),
    labels = .$Group %>% unique(),
    ...
  )
0 голосов
/ 30 января 2019

Нет полного ответа, но есть несколько подсказок:

Для использования ggplot данные должны быть в правильной форме (это может помочь: https://r4ds.had.co.nz/tidy-data.html). Этого можно добиться с помощью gather() из библиотеки tidyverse:

library (tidyverse)

plot_df <- gather(Mat, "Strategies", "Number")
plot_df$Problems <- factor(rep(rownames(Mat), 4), levels = rownames(Mat))

Я использую фактор для «Проблем», чтобы иметь возможность предоставить значения xmin и xmax для geom_rect, который используется для рисования фона для групп баров:

ggplot(plot_df, aes(x = as.numeric(Problems), y = Number, fill = Strategies)) +
  geom_rect(aes(xmin = as.numeric(Problems) - 0.4, xmax = as.numeric(Problems) + 0.4, ymin = 0, ymax = max(Number),  fill = Problems), alpha = 0.2) +
  geom_bar(stat = "identity", position = "dodge2") +
  scale_x_continuous(labels = levels(plot_df$Problems), breaks = seq(1, length(unique(as.character(plot_df$Problems))))) +
  xlab("Problems")

Не смотрите на цвета .... Для добавления панелей ошибок смотрите, например, https://ggplot2.tidyverse.org/reference/geom_linerange.html

...