簡體   English   中英

如何:在 R 中為 3 個分類變量和一個連續變量創建 plot?

[英]How to: Create a plot for 3 categorical variables and a continuous variable in R?

我想使用 R 創建一個 plot,最好使用 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 軸上具有准確性。 然后,四個條形代表句子和試驗的兩種可能組合,顯示高度的准確性,並在此周圍顯示誤差條以反映不確定性。

我怎么能做出這樣的圖表? 或者,有沒有人認為這不是正確的圖表類型,然后會是什么(以及如何 plot 它......)?

提前致謝!

根據您分享的描述使用一些模擬數據,您可以嘗試:

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')

Output:

在此處輸入圖像描述

或這個:

#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')

Output:

在此處輸入圖像描述

了解您的問題需要更多詳細信息和數據。

您是否正在尋找類似的東西?

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())

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM