سؤال

لدي البيانات التالية:

bin groupname   total_dist
0   rowA    377
0   rowA    306.6
0   rowB    2.1
0   rowB    110.6
1   rowA    918.1
1   rowA    463.2
1   rowB    798.2
1   rowB    1196
2   rowA    1295.1
2   rowA    1269.1
2   rowB    698
2   rowB    1022.1

باستخدام R ، أريد إنشاء رسم بياني شريط حيث يوجد شريط لـ Rowa وشريط Rowb for كل سلة مهملات. يمكنني تجميع Total_dist بواسطة واحد أو آخر (plot(total_dist~bin) أو plot(total_dist~groupname)). لكن لا يمكنني معرفة كيفية الجمع بينهم.

أريد شيئًا يشبه هذا المثال:example grouped bar graph

هل كانت مفيدة؟

المحلول

هنا حل كلاسيكي. (لنفترض أن يتم تسمية DataFrame df )

data <- tapply(df$total_dist, list(df$groupname,df$bin), sum)

barplot(data,beside=T,col=c("#ee7700","#3333ff")
,main="European Parliament Elections",xlab="Group",ylab="Seats")

legend(locator(1),rownames(data),fill=c("#ee7700","#3333ff"))

وهنا حل باستخدام ggplot2

library(ggplot2)
qplot(factor(bin),data=df,geom="bar",fill=groupname,weight=total_dist,position="dodge",
main = "European Parliament Elections", xlab="Group",ylab="Seats")

alt text

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top