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

R 条形图每个条形图对具有不同背景颜色的ggplot,r,ggplot2,bar-chart,background-color,R,Ggplot2,Bar Chart,Background Color,有人能帮我生成一个包含所有显示属性的条形图,并且每个条形图的背景颜色不同吗? 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是误差条

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"))
我不知道如何更改每对条的背景色,尤其是如何将其与错误条和图例放在同一代码中


没有完整的答案,但有一些提示:

要使用ggplot,数据必须具有正确的形状这可能会有所帮助:。您可以使用tidyverse库中的“聚集”功能实现这一点:

library (tidyverse)

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

别看颜色。。。。有关添加错误条的信息,请参见,例如,

无完整答案,但有一些提示:

要使用ggplot,数据必须具有正确的形状这可能会有所帮助:。您可以使用tidyverse库中的“聚集”功能实现这一点:

library (tidyverse)

plot_df <- gather(Mat, "Strategies", "Number")
plot_df$Problems <- factor(rep(rownames(Mat), 4), levels = rownames(Mat))
别看颜色。。。。有关添加错误条的信息,请参见,例如,

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)
现在你可以做一个漂亮的图,像这样:

使用此代码:

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为每个组设置不同的背景色。问题是几何体需要数值。因此,在我的示例中,您必须为每组XPO添加编号,然后使用scale_x_continuous更改x轴的标签。您可以使用wrapr的%.>%管道向下传递Mat_long数据帧,以便稍后通过调用点在scale_x_continuous中使用它:

纳丁, 首先,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 %.>%
  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为每个组设置不同的背景色。问题是几何体需要数值。因此,在我的示例中,您必须为每组XPO添加编号,然后使用scale_x_continuous更改x轴的标签。您可以使用wrapr的%.>%管道向下传递Mat_long数据帧,以便稍后通过调用点在scale_x_continuous中使用它:


你生成条形图的代码是什么?你到底在哪里卡住了?有很多关于在ggplot2中创建条形图的帖子,我相信你的每个问题都已经在某个地方单独讨论过了。你生成条形图的代码是什么?你到底在哪里被卡住了?有很多关于在ggplot2中创建条形图的帖子,我相信你的每一个问题都已经在某个地方单独讨论过了。非常感谢你,看起来太完美了!剩下的唯一问题是,当我复制你的语法时,总是会出现错误消息:“未知列认知策略”。您知道此错误是如何发生的吗?这是因为您在编辑问题时更改了data.frame。这是错误的,因为你的激励策略值为1.19,没有问题。谢谢。早先的代码是:是的。这是正确的。现在我的代码应该可以工作了;非常感谢你,看起来太完美了!剩下的唯一问题是,当我复制你的语法时,总是会出现错误消息:“未知列认知策略”。您知道此错误是如何发生的吗?这是因为您在编辑问题时更改了data.frame。这是错误的,因为你的激励策略值为1.19,没有问题。谢谢。早先的代码是:是的。这是正确的。现在我的代码应该可以工作了;