【问题标题】:How to: Create a plot for 3 categorical variables and a continuous variable in R?如何:在 R 中为 3 个分类变量和一个连续变量创建图?
【发布时间】:2020-12-13 12:53:31
【问题描述】:

我想使用 R 创建一个绘图,最好使用 ggplot。我有以下变量要可视化,其中大部分是二进制的:

试用:cong/incon

句子:他/他自己

条件:正常/慢

准确度:数字

SE:数字

structure(list(TrialType = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L), .Label = c("congruent", "incongruent"), class = "factor"), 
    SentenceType = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
    ), .Label = c("him", "himself"), class = "factor"), Condition = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("normal_speech", 
    "slow_speech"), class = "factor"), MeanAccuracy = c(0.794871794871795, 
    0.762820512820513, 0.967948717948718, 0.967948717948718, 
    0.237179487179487, 0.342105263157895, 0.942307692307692, 
    0.83974358974359), SE = c(0.0342056016493384, 0.0430264468743046, 
    0.0389087806837746, 0.0496183045476835, 0.0135583881898854, 
    0.0163760608630386, 0.0170869868584354, 0.0311270245470197
    )), class = "data.frame", row.names = c(NA, -8L))

SE 代表标准误差,这意味着我想在准确度得分周围显示误差条。

我认为我最好的选择是制作两个条形图,每个条件分别制作一个,并在 x 轴上准确。然后,四个条形代表句子和试验的两种可能组合,显示高度的准确性,并在此周围显示误差条以反映不确定性。

如何制作这样的图表?或者,有没有人认为这不是正确的图表类型,然后会是什么(以及如何绘制它......)?

提前致谢!

【问题讨论】:

  • 您能否向我们展示实际数据以便我们为您提供帮助?如果您在控制台中输入dput(my_dataframe)(显然将my_dataframe 替换为您自己的数据框),然后编辑您的问题以包含dput 的输出,我相信这里有人可以帮助您
  • 喜欢这个?我非常确定这是如何工作的

标签: r ggplot2


【解决方案1】:

您是否正在寻找类似的东西?

library(ggplot2)

ggplot(df, aes(TrialType, MeanAccuracy, fill = SentenceType)) +
  geom_col(position = position_dodge(width = 1), color = "gray50") +
  geom_errorbar(aes(ymin = MeanAccuracy - SE, 
                    ymax = MeanAccuracy + SE), width = 0.25,
                position = position_dodge(width = 1)) +
  scale_fill_manual(values = c("gold", "deepskyblue4")) +
  facet_grid(.~Condition, switch = "x") +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        panel.border = element_blank(),
        panel.spacing = unit(0, "points"),
        axis.line = element_line())

【讨论】:

    【解决方案2】:

    根据你分享的描述使用一些模拟数据,你可以试试:

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    #Data
    df <- data.frame(Trial=rep(c('cong','incong'),4),
                     Sentence= rep(c('him','himself'),4),
                     Condition=rep(c('normal','slow'),4),
                     Accuracy=runif(8,0,1),
                     SE=runif(8,0,10),stringsAsFactors = F)
    #Plot 1
    df %>% pivot_longer(-c(Trial,Sentence,Condition)) %>%
      ggplot(aes(x=name,y=value,fill=Condition))+
      geom_bar(stat = 'identity')+
      facet_wrap(.~Trial+Sentence,scales = 'free')
    

    输出:

    或者这个:

    #Plot 2
    df %>% pivot_longer(-c(Trial,Sentence,Condition)) %>%
      ggplot(aes(x=name,y=value,fill=Condition))+
      geom_bar(stat = 'identity')+
      facet_grid(Trial~Sentence,scales = 'free')
    

    输出:

    了解您的问题需要更多详细信息和数据。

    【讨论】:

    • 嗨!谢谢,但在这些图中,SE 没有正确显示,SE 涉及一个误差范围,应该在准确度分数周围可见。
    猜你喜欢
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2020-09-16
    • 2022-08-04
    • 2021-05-18
    相关资源
    最近更新 更多