最简单的分组分组方法

我有以下数据框:


 Catergory        Reason Species

1   Decline       Genuine      24

2  Improved       Genuine      16

3  Improved Misclassified      85

4   Decline Misclassified      41

5   Decline     Taxonomic       2

6  Improved     Taxonomic       7

7   Decline       Unclear      41

8  Improved       Unclear     117

我正在尝试制作分组的条形图,将种类作为高度,然后将2种颜色用于类别。


我会发布一张我所拥有的图像,但是我没有足够的声誉点...但这是我的代码:


Reasonstats<-read.csv("bothstats.csv")

Reasonstats2<-as.matrix(Reasonstats[,3])



barplot((Reasonstats2),beside=T,col=c("darkblue","red"),ylab="number of 

species",names.arg=Reasonstats$Reason, cex.names=0.8,las=2,space=c(0,100)

,ylim=c(0,120))

box(bty="l")

现在,我想要的是不必两次标记两个小节并将它们分组在一起,我尝试将空间值更改为各种事物,并且似乎没有将小节分开。谁能告诉我我在做什么错?


慕斯王
浏览 888回答 3
3回答

蝴蝶刀刀

使用ggplot2:library(ggplot2)Animals <- read.table(&nbsp; header=TRUE, text='Category&nbsp; &nbsp; &nbsp; &nbsp; Reason Species1&nbsp; &nbsp;Decline&nbsp; &nbsp; &nbsp; &nbsp;Genuine&nbsp; &nbsp; &nbsp; 242&nbsp; Improved&nbsp; &nbsp; &nbsp; &nbsp;Genuine&nbsp; &nbsp; &nbsp; 163&nbsp; Improved Misclassified&nbsp; &nbsp; &nbsp; 854&nbsp; &nbsp;Decline Misclassified&nbsp; &nbsp; &nbsp; 415&nbsp; &nbsp;Decline&nbsp; &nbsp; &nbsp;Taxonomic&nbsp; &nbsp; &nbsp; &nbsp;26&nbsp; Improved&nbsp; &nbsp; &nbsp;Taxonomic&nbsp; &nbsp; &nbsp; &nbsp;77&nbsp; &nbsp;Decline&nbsp; &nbsp; &nbsp; &nbsp;Unclear&nbsp; &nbsp; &nbsp; 418&nbsp; Improved&nbsp; &nbsp; &nbsp; &nbsp;Unclear&nbsp; &nbsp; &nbsp;117')ggplot(Animals, aes(factor(Reason), Species, fill = Category)) +&nbsp;&nbsp; geom_bar(stat="identity", position = "dodge") +&nbsp;&nbsp; scale_fill_brewer(palette = "Set1")

动漫人物

有多种方法可以在R中进行绘图。lattice是其中之一,并且始终是合理的解决方案,对@agstudy +1。如果要在基本图形中执行此操作,则可以尝试以下操作:Reasonstats <- read.table(text="Category&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reason&nbsp; Species&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Decline&nbsp; &nbsp; &nbsp; &nbsp; Genuine&nbsp; &nbsp; &nbsp; &nbsp;24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Improved&nbsp; &nbsp; &nbsp; &nbsp; Genuine&nbsp; &nbsp; &nbsp; &nbsp;16&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Improved&nbsp; Misclassified&nbsp; &nbsp; &nbsp; &nbsp;85&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Decline&nbsp; Misclassified&nbsp; &nbsp; &nbsp; &nbsp;41&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Decline&nbsp; &nbsp; &nbsp; Taxonomic&nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Improved&nbsp; &nbsp; &nbsp; Taxonomic&nbsp; &nbsp; &nbsp; &nbsp; 7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Decline&nbsp; &nbsp; &nbsp; &nbsp; Unclear&nbsp; &nbsp; &nbsp; &nbsp;41&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Improved&nbsp; &nbsp; &nbsp; &nbsp; Unclear&nbsp; &nbsp; &nbsp; 117", header=T)ReasonstatsDec <- Reasonstats[which(Reasonstats$Category=="Decline"),]ReasonstatsImp <- Reasonstats[which(Reasonstats$Category=="Improved"),]Reasonstats3&nbsp; &nbsp;<- cbind(ReasonstatsImp[,3], ReasonstatsDec[,3])colnames(Reasonstats3) <- c("Improved", "Decline")rownames(Reasonstats3) <- ReasonstatsImp$Reasonwindows()&nbsp; barplot(t(Reasonstats3), beside=TRUE, ylab="number of species",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cex.names=0.8, las=2, ylim=c(0,120), col=c("darkblue","red"))&nbsp; box(bty="l")下面是我所做的:我创建了一个矩阵具有两列(因为你的数据均列)其中列是种计数Decline和Improved。然后,我将这些类别作为列名。我还给了Reasons行名。该barplot()函数可以在此矩阵上运行,但希望数据以行而不是列的形式存在,因此我将其转置为矩阵。最后,我删除了一些barplot()不再需要的函数
打开App,查看更多内容
随时随地看视频慕课网APP