我的目标是创建一个曲线图,显示基于这样的分组变量M1分组的分类变量X的4个不同级别的效果估计.下面的示例中每个变量有5个级别,但我的X有4个级别

enter image description here

假设这是我的带有效果估计的测试数据集

effect_data <- data.frame(
  Level = factor(rep(1:4, each = 12)),  # Assuming 12 sets of analyses
  EffectEstimate = runif(48, min = -2, max = 2),  # Replace with your actual effect estimates
  LowerCI = runif(48, min = -3, max = -1),  # Replace with your actual lower CIs
  UpperCI = runif(48, min = 1, max = 3),  # Replace with your actual upper CIs
  M1  = factor(rep(letters[1:12], times = 4))
)

我的情节最终是这样的

# Plot the effect estimate as a bar chart
ggplot(effect_data, aes(x = Level, y = EffectEstimate)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.7, fill = "blue") +
  geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI), position = position_dodge(0.7), width = 0.25) +
  coord_flip() +
  facet_grid(.~Level)+
  labs(
    title = "Effect Estimates with Confidence Intervals",
    x = "Results from 12 different Models",
    y = "Effect Estimate"
  ) +
  theme_minimal()

这不是我想要的,我正试图生成一个像上面这样的情节,任何帮助都是非常感激的.谢谢.

enter image description here

推荐答案

effect_data <- data.frame(
  Level = factor(rep(1:4, each = 12)),
  # Assuming 12 sets of analyses
  EffectEstimate = runif(48, min = -2, max = 2),
  # Replace with your actual effect estimates
  LowerCI = runif(48, min = -3, max = -1),
  # Replace with your actual lower CIs
  UpperCI = runif(48, min = 1, max = 3),
  # Replace with your actual upper CIs
  M1  = factor(rep(letters[1:12], times = 4))
)

从你的描述来看,我不确定你在看什么. 这里有两个变种.也许其中一个就是你想要的?

library(tidyverse)

# Variant A
ggplot(effect_data,
       aes(
         x = Level,
         y = EffectEstimate,
         group = M1,
         fill = M1
       )) +
  geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI),
                position = position_dodge(0.7),
                width = 0.25) +
  labs(title = "Effect Estimates with Confidence Intervals",
       x = "Results from 12 different Models",
       y = "Effect Estimate") +
  geom_bar(stat = "identity",
           position = "dodge",
           width = 0.7) +
  theme_minimal()


# Variant B
ggplot(effect_data,
       aes(
         x = M1,
         y = EffectEstimate,
         group = Level,
         fill = Level
       )) +
  geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI),
                position = position_dodge(0.7),
                width = 0.25) +
  labs(title = "Effect Estimates with Confidence Intervals",
       x = "Results from 12 different Models",
       y = "Effect Estimate") +
  geom_bar(stat = "identity",
           position = "dodge",
           width = 0.7) +
  theme_minimal()

R相关问答推荐

Facet_wrap具有不同bin宽度值的图表

使用gsim删除特殊词

分组时间连续值

创建重复删除的唯一数据集组合列表

R箱形图gplot 2 4组但6个参数

如何修复R码的置换部分?

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

如何得到R中唯一的组合群?

如何从R ggplot图片中获取SVG字符串?

将多列合并为单独的名称—值对

使用rest从header(h2,h3,table)提取分层信息

识别连接的子网(R-igraph)

在列表中排列R数据框中的列顺序

R如何将列名转换为更好的年和月格式

有没有办法将不等长的列表转换为R中的数据帧

R预测包如何处理ARIMA(Auto.arima函数)中的缺失值

R中的Desolve:返回的导数数错误

子样本间系数检验的比较

如何编辑被动式数据表?

如何使用list_rind在列表中保留已命名但不包含第0行的记录?