3

誰かが次の問題で私を助けてくれることを望んでいました:

バイナリ変数 (性別) に対して記録された 3 つの異なる連続変数 (体温、長さ、質量) の平均と標準誤差を示す結合棒グラフを作成しようとしています。

各変数の平均値をプロットできましたが、試したコードを使用してこれら 3 つの変数の標準誤差をうまく計算できないようです。私は多くのことを試しましたが、これで正しい軌道に乗っていたと思います:

    View(test4)
    test4 <- aggregate(test4, 
             by = list(Sex = test4$Sex), 
             FUN = function(x) c(mean = mean(x), sd = sd(x),
                                 n = length(x)))
    test4
    #this produced mean, sd, length for ALL variables (including sex)
    test4<-do.call(test4)
    test4$se<-test4$x.sd / sqrt(test4$x.n)

その後、エラーが発生し続けました:

    Error in sqrt(test4$x.n) : non-numeric argument to mathematical function

集約(test4 ...)の後に3つの変数をターゲットにするように再コーディングしようとしましたが、機能しませんでした...次に、結果のデータフレームでサブセット化して性別を除外しましたが、機能しませんでした。次に、それを行列またはベクトルとして定義しようとしましたが、それでもうまくいきませんでした。

最終的なグラフで、y 軸 = 平均値、x 軸 = 変数 (3 つのサブグループ (Tb、Mass、Length)、比較のために男性と女性の値を示す 2 つのバーを並べて表示したいと思います。

誰かが提供できる助けや方向性は大歓迎です!!

よろしくお願いします!:)

4

2 に答える 2

3

aggregate複数の列を出力しようとすると、クレイジーな出力が得られます。使用したい場合aggregateは、 mean と SE を への個別の呼び出しとして実行しますaggregate

ただし、これは tidyr と dplyr を使用した解決策であり、それほど悪くはないと思います。

いくつかのデータを作成しました。あなたのように見えることを願っています。質問にシミュレートされたデータセットを含めると非常に便利です。

library(tidyr)
library(dplyr)
library(ggplot2)

# Create some data 
test4 <- data.frame(Sex = rep(c('M', 'F'), 50),
                    bodytemp = rnorm(100),
                    length = rnorm(100), 
                    mass = rnorm(100))

# Gather the data to 'long' format so the bodytemp, length and mass are all in one column
longdata <- gather(test4, variable, value, -Sex)
head(longdata)

# Create the summary statistics seperately for sex and variable (i.e. bodytemp, length and mass)
summary <- longdata %>%
             group_by(Sex, variable) %>%
             summarise(mean = mean(value), se = sd(value) / length(value))

# Plot
ggplot(summary, aes(x = variable, y = mean, fill = Sex)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),                            
                  width = 0.2,
                  position = position_dodge(0.9))

出力棒グラフ

于 2016-05-10T16:11:47.253 に答える
0

私の最終的なプロット

更新: timclucas スクリプトの最初の部分と、出力を 1 つだけプロットするときに使用した別の部分を組み合わせることで、質問に答えることができました。同様の質問への回答を探している可能性のある他の人のために、スクリプトと結果のグラフを投稿しました (上記のリンクを参照)。

View(test3) #this dataframe was organized as 'sex', 'tb', 'mass', 'svl' 
newtest<-test3
View(newtest)

#transform data to 'long' combining all variables in one column 
longdata<-gather(newtest, variable, value, -Sex)
View(longdata)

#set up table in correct format
longdata2 <- aggregate(longdata$value, 
                 by = list(Sex = longdata$Sex, Variable = longdata$variable),
                 FUN = function(x) c(mean = mean(x), sd = sd(x),
                                     n = length(x)))
longdata2 <- do.call(data.frame, longdata2)
longdata2$se<-longdata2$x.sd / sqrt(longdata2$x.n)
colnames(longdata2)<-c("Sex", "Variable", "mean", "sd", "n", "se")
longdata2$names<-c(paste(longdata2$Variable, "Variable /", longdata2$Sex,    "Sex"))
View(longdata2)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = longdata3$mean + longdata3$se,
          ymin = longdata3$mean - longdata3$se)

#To order the bars in the way I desire *might not be necessary for future scripts*
positions<-c("Tb", "SVL", "Mass")

#To plot new table: 

bfinal <- ggplot(data = longdata3, aes(x = factor(Variable), y = mean,
                             fill = factor(Sex)))+
geom_bar(stat = "identity",
         position = position_dodge(0.9))+
geom_errorbar(limits, position = position_dodge(0.9),
            width = (0.25)) +
labs(x = "Variable", y = "Mean") +
ggtitle("")+
scale_fill_discrete(name = "", 
                  labels=c("Male", "Female"))+
scale_x_discrete(breaks=c("Mass", "SVL", "Tb"),
               labels=c("Mass", "SVL", "Tb"), 
               limits=(positions))
bfinal  

:)

于 2016-05-11T04:29:21.573 に答える