3 回答
TA贡献1801条经验 获得超8个赞
使用ggplot2:
library(ggplot2)
Animals <- read.table(
header=TRUE, text='Category 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')
ggplot(Animals, aes(factor(Reason), Species, fill = Category)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_brewer(palette = "Set1")
TA贡献1815条经验 获得超10个赞
有多种方法可以在R中进行绘图。lattice是其中之一,并且始终是合理的解决方案,对@agstudy +1。如果要在基本图形中执行此操作,则可以尝试以下操作:
Reasonstats <- read.table(text="Category Reason Species
Decline Genuine 24
Improved Genuine 16
Improved Misclassified 85
Decline Misclassified 41
Decline Taxonomic 2
Improved Taxonomic 7
Decline Unclear 41
Improved Unclear 117", header=T)
ReasonstatsDec <- Reasonstats[which(Reasonstats$Category=="Decline"),]
ReasonstatsImp <- Reasonstats[which(Reasonstats$Category=="Improved"),]
Reasonstats3 <- cbind(ReasonstatsImp[,3], ReasonstatsDec[,3])
colnames(Reasonstats3) <- c("Improved", "Decline")
rownames(Reasonstats3) <- ReasonstatsImp$Reason
windows()
barplot(t(Reasonstats3), beside=TRUE, ylab="number of species",
cex.names=0.8, las=2, ylim=c(0,120), col=c("darkblue","red"))
box(bty="l")
下面是我所做的:我创建了一个矩阵具有两列(因为你的数据均列)其中列是种计数Decline和Improved。然后,我将这些类别作为列名。我还给了Reasons行名。该barplot()函数可以在此矩阵上运行,但希望数据以行而不是列的形式存在,因此我将其转置为矩阵。最后,我删除了一些barplot()不再需要的函数
- 3 回答
- 0 关注
- 938 浏览
添加回答
举报