简体   繁体   English

使用ggplot的Barplot

[英]Barplot using ggplot

I have a data frame like 我有一个数据框

  Model1    Model2      Model3    Model4
      4          4          5         5
      4          4         NA        NA
      3          2          5         5
      2          2          3         3
      3          3          3         3
      3          3          4         4

I want to plot a barplot like 我想绘制一个像

在此处输入图片说明

The x- axis will have the Model1,Model2,Model3 and Model4 and the bars will be the proportion of the scores in each column,ie, 6 bars for each Model (in the full data I have the scores from 0 to 5) x轴将具有Model1,Model2,Model3和Model4,并且条形图是每列中得分的比例,即每个模型有6条(在完整数据中,我的得分是0到5)

How can this be done without having to create a dataframe using rbind or creating a matrix? 无需使用rbind创建数据框或创建矩阵就可以做到这一点? Any help will be appreciated. 任何帮助将不胜感激。

You can't do it without changing the format of your data, but it isn't difficult to do so. 不更改数据格式就无法做到这一点,但这并不难。

You can use the melt option of the reshape2 package. 您可以使用reshape2包的melt选项。

I created a data frame of your data: 我为您的数据创建了一个数据框:

   df1<-data.frame("Model1"=c(4,4,3,2,3,3),
                   "Model2"=c(4,4,2,2,3,3),
                   "Model3"=c(5,NA,5,3,3,4),
                   "Model4"=c(5,NA,5,3,3,4))

  Model1 Model2 Model3 Model4
1      4      4      5      5
2      4      4     NA     NA
3      3      2      5      5
4      2      2      3      3
5      3      3      3      3
6      3      3      4      4

Next you can reshape the data using the reshape2 package: 接下来,您可以使用reshape2包来重塑数据:

library(reshape2)
df2<-melt(df1)

    variable value
1     Model1     4
2     Model1     4
3     Model1     3
4     Model1     2
5     Model1     3
6     Model1     3
7     Model2     4
8     Model2     4
9     Model2     2
10    Model2     2
11    Model2     3
12    Model2     3
13    Model3     5
14    Model3    NA
15    Model3     5
16    Model3     3
17    Model3     3
18    Model3     4
19    Model4     5
20    Model4    NA
21    Model4     5
22    Model4     3
23    Model4     3
24    Model4     4

Renamed the columns for ease: 为方便起见,将列重命名为:

names(df2)<-c("Model","Score")

Then calculate the proportions: 然后计算比例:

df3 <- as.data.frame(table(df2))
df3$prop<-df4$Freq/4*100

And finally, the plot: 最后,剧情:

ggplot(df3,aes(x=Model,y=prop,fill=as.factor(Score)))+
       geom_bar(stat="identity",position="dodge")+
       xlab("Models")+
       ylab("Prop. of Cases (%)")+
       ggtitle("Sample Data")+
       guides(fill=guide_legend(title="Scores"))+
       scale_fill_manual(values = c("2" = "lightblue", "3" = "mistyrose","4"="lightcyan","5"="lavender"))+
       theme_bw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM