Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

r - barplot x-axis labels with hierarchical grouping variables in separate rows

In Excel, with data organized like so:

                                              Bare NP                   Singular-Marked NP               Plural-Marked NP   
                                    BrP Speakers    AmE Speakers    BrP Speakers    AmE Speakers    BrP Speakers AmE Speakers
Plural Interpretation               0.005747126 0.006896552         0.194117647 0.124567474         0.872093023 0.985815603
Plural & Singular Interpretation    0.649425287 0.910344828         0.029411765 0.051903114         0.127906977 0.014184397
Singular Interpretation             0.344827586 0.082758621         0.776470588 0.823529412         0           0

I am able to produce a stacked barchart that looks like this:

enter image description here

Is there an easy way to reproduce the way that the x-axis is partitioned into the separate conditions (i.e., "Bare NP", "Singular-Marked NP", and "Plural-Marked NP") using R?

All that I can come up with at the moment is this:

pluralInterp <- c(0.005747126,0.006896552,0.194117647,0.124567474,0.872093023,0.985815603)

pluralAndSingInterp <- c(0.649425287,0.910344828,0.029411765,0.051903114,0.127906977,0.014184397)

singInterp <- c(0.344827586,0.082758621,0.776470588,0.823529412,0,0)

a  <- rbind(pluralInterp,pluralAndSingInterp,singInterp)

colnames(a) <- c("Bare NP ~ BrP Speakers",
                 "Bare NP ~ AmE Speakers",
                 "Singular-Marked NP ~ BrP Speakers",
                 "Singular-Marked NP ~ AmE Speakers",
                 "Plural-Marked NP ~ BrP Speakers",
                 "Plural-Marked NP ~ AmE Speakers"
                 )

barplot(a,
        col=c("blue","red","purple"),
        ylab="Frequency of Interpretation",
        xlab="Form of NP and Native Language",
        main="Frequency of BrP and AmE Interpretations of NPs in Neutral Environments"
        )

Which produces the following graph:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A bit of manual adjustment of the margins and calling axis() twice can get you there. Saving a barplot like bp <- barplot(...) saves the midpoints of each bar for future reference.

oldpar <-par(mar=c(7,5.1,4.1,2.1))

bp <- barplot(a,
        col=c("blue","red","purple"),
        ylab="Frequency of Interpretation",
        xlab="",
        main="Frequency of BrP and AmE Interpretations 
 of NPs in Neutral Environments",
        axisnames=FALSE
       )

You can then use the values stored in bp to align things properly. You can align the group and sub-group labels using the line=... argument to axis()

avgpts <- tapply(bp,rep(1:3,each=2),mean)
grps <- c("Bare NP","Singular-Marked NP","Plural-Marked NP")
subgrps <- c("BrP","AmE")
axis(1,at=bp,labels=rep(subgrps,3), cex.axis=0.7)
axis(1,at=avgpts,labels=grps, cex.axis=0.7,line=1.5,lwd=0)

title(xlab="Form of NP and Native Language",line=4.5)

Resulting in:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...