gpt4 book ai didi

r - 做分组条形图的最简单方法

转载 作者:行者123 更新时间:2023-12-03 08:59:27 26 4
gpt4 key购买 nike

我有以下数据框:

 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")

现在我想要的是不必将两个条标记两次并将它们分开,我已经尝试将空间值更改为各种东西,但它似乎并没有将条分开。谁能告诉我我做错了什么?

最佳答案

使用 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")

Bar Chart

关于r - 做分组条形图的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17721126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com