需要在 R 中对图进行编码

问题描述 投票:0回答:1

我正在尝试在 R 中制作一个如下所示的图表:

我有下面的数据,但我无法找出获取图表的代码:

我尝试运行箱线图代码,但我不知道如何获取同一图中的所有列

r graph rstudio coding-style
1个回答
0
投票
library(tidyverse)

# Create a dataset that is similar to yours.  
df1 <-
  tibble(
    y = 1:10,
    women_v1 = abs(rnorm(10)),
    women_v2 = abs(rnorm(10)),
    men_v1 = abs(rnorm(10)),
    men_v2 = abs(rnorm(10))
  )

# Pivot the dataset longer and derive `sum` and `sd` for the plot.
df2 <- 
  df1 |>
  pivot_longer(-y, names_to = c("sex", "var"), names_sep = "_") |>
  summarise(
    sd = sd(value, na.rm = TRUE),
    value = sum(value),
    .by = c(sex, var)
  )

# Create a barplot with errorbars.
ggplot(df2, aes(x = var, y = value, fill = sex)) +
  geom_bar(stat = "identity",
           color = "black",
           position = position_dodge()) +
  geom_errorbar(aes(ymin = value, ymax = value + sd),
                width = .2,
                position = position_dodge(.9))

© www.soinside.com 2019 - 2024. All rights reserved.