我需要做一个"堆叠条形图"使用ggplot2 FOE以下数据(类似于下面的图片)

stacked bar plot- what i needed

State <- c("All India", "Arunachal Pradesh", "Gujarat", "Jammu & Kashmir", "Madhya Pradesh", "Maharashtra", "Meghalaya", "Mizoram", "Orissa", "Punjab", "Tamil Nadu", "Uttar Pradesh", "West Bengal")
Mean <- c(217, 227, 214, 217, 217, 204, 209, 215, 223, 243, 220, 230, 210)
Percentile_5th <- c(163, 193, 162, 185, 177, 146, 169, 173, 167, 183, 156, 163, 170)
Percentile_95th <- c(271, 262, 265, 249, 258, 261, 249, 257, 280, 302, 284, 297, 249)


我需要为所有州

我还需要go 任何两个邦("全印度","旁遮普")

我已经try 使用以下代码为两个州创建了它

data <- data.frame(
      State = rep(c("All India",  "Punjab"   ), each = 3),
     Metric = rep(c("5th Percentile", "Mean", "95th Percentile"), 2),
     Value = c(163, 217, 271, 183, 243, 302)  # 5th P, Mean increment, 95th P increment for each state
 )

ggplot(data, aes(x = State, y = Value, fill = Metric)) +
    geom_bar(stat = "identity") +
    geom_text(aes(label = Value), position = position_stack(vjust = 0.5)) +
    scale_fill_manual(values = c("5th Percentile" = "orange", "Mean" = "blue", "95th Percentile" = "green")) +
    labs(title = "Stacked Bar Plot for All India and Punjab", 
         x = "State", 
         y = "Values", 
         fill = "Metric") +
    theme_minimal()

我收到的输出是

The output plot

我需要的曲线图(如我前面所说)是条形图底部的第五个百分位数,条形图中间的下一个平均值,以及条形图顶部的第95个百分位数.但它不是这样给予的.此外,我需要为所有的州做一个类似的情节.请帮帮我

推荐答案

第一个问题可以很容易地解决,方法是将您的指标列转换为一个以您所需的顺序设置的级别顺序的因子.其次,要 for each 州创建类似的绘图,请将数据放入数据框中,将其整形为长格式,将绘图代码放入函数中,过滤所需州的数据,最后使用lapply对所有州进行循环:

library(ggplot2)
library(tidyr)

data <- data.frame(
  State = State,
  Mean = Mean,
  Percentile_5th = Percentile_5th,
  Percentile_95th = Percentile_95th
)

data_long <- data |>
  tidyr::pivot_longer(
    -State,
    names_to = "Metric",
    values_to = "Value"
  )

data_long$Metric <- factor(
  data_long$Metric,
  levels = rev(c("Percentile_5th", "Mean", "Percentile_95th")),
  labels = rev(c("5th Percentile", "Mean", "95th Percentile"))
)

plot_fun <- function(state) {
  data_long |>
    subset(
      State %in% c("All India", state)
    ) |>
    ggplot(aes(x = State, y = Value, fill = Metric)) +
    geom_col(
      color = "white",
      linewidth = .25,
      width = .75
    ) +
    geom_text(aes(label = Value),
      position = position_stack(vjust = 0.5)
    ) +
    scale_fill_manual(
      values = c(
        "5th Percentile" = "orange",
        "Mean" = "blue",
        "95th Percentile" = "forestgreen"
      )
    ) +
    scale_y_continuous(expand = c(0, 0, .05, 0)) +
    labs(
      title = paste0(
        "Stacked Bar Plot for All India and ",
        state
      ),
      x = "State",
      y = "Values",
      fill = "Metric"
    ) +
    theme_bw() +
    theme(
      panel.grid = element_blank()
    )
}

# For all states run:
# lapply(State[-1], plot_fun)

lapply(State[-1][1:2], plot_fun)
#> [[1]]

#> 
#> [[2]]

R相关问答推荐

通过绘图 Select 线串几何体并为其着色

如何在R中正确对齐放射状图中的文本

编码变量a、b、c以匹配来自另一个数据点的变量x

如果列中存在相同的字符串,则对行值进行总和

更改绘图上的x轴断点,而不影响风险?

根据模式将一列拆分为多列,并在R中进行拆分

在数组索引上复制矩阵时出错

plotly hover文本/工具提示在shiny 中不起作用

如何用书面利率绘制geom_bar图

计算数据帧中指定值之前的行数,仅基于每行之后的future 行,单位为r

R:如果为NA,则根据条件,使用列名模式将缺少的值替换为另一列中的值

WRS2包中带有bwtrim的简单ANOVA抛出错误

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

优化从每个面的栅格中提取值

通过R:文件名未正确写入[已解决]将.nc文件转换和导出为.tif文件

如何将宽格式的患者信息数据高效地转换为患者计数的时间序列?

ArrangeGrob()和类似的替代方法不接受Grob列表.在Grid.Draw,返回:glist中的错误(...):仅允许在glist";中使用Grobs;

按镜像列值自定义行顺序

R:使用ApexCharge更改标签在饼图中的位置

是什么打破了此Quarto仪表板中的工具提示?