R上的集群条形图

时间:2015-11-12 04:36:33

标签: r

我正在尝试在R上获得一个聚类条形图,显示多年来分布的物种类型。我使用的代码是:

Fish<- read.table( header=TRUE, text="Mbio$Date, Mbio$Roach,Mbio$Green.sunfish,
Mbio$Sucker, Mbio$Rainbow.trout, Mbio$Squawfish, Mbio$Mosquito.fish")

library(ggplot2)

ggplot(Fish, aes(factor(Mbio$Date), Mbio$Roach,Mbio$Green.sunfish, Mbio$Sucker, Mbio$Rainbow.trout, Mbio$Squawfish, Mbio$Mosquito.fish)) + geom_bar(stat="identity", position = "dodge") + scale_fill_brewer(palette = "Set1")

这给出了这样的图表:

Cluster Graph: Click to View

然而,这并没有给我我想要的图表:它需要显示所有年份中每种鱼类的数量。

我希望它看起来像这个图像。请帮助我,谢谢。 Correct graph:Click to view

2 个答案:

答案 0 :(得分:2)

创建数据并制作条形图

column1 <- c(rep("1984", 3), rep("1985", 3), rep("1986", 3))
column2 <- gl(3, 1, 9, labels=c("Roach", "Green", "Sucker"))
column3 <- c(20, 25, 10, 70, 85, 30, 10, 65, 70)
d <- data.frame(column1=column1, column2=column2, column3=column3)
require(lattice)
barchart(column3 ~ column1, groups=column2, d, auto.key = list(columns = 3))

enter image description here

答案 1 :(得分:0)

    column1 <- c(rep("1984", 3), rep("1985", 3), rep("1986", 3))
 column2 <- gl(3, 1, 9, labels=c("Roach", "Green", "Sucker"))
column3 <- c(20, 25, 10, 70, 85, 30, 10, 65, 70)
d <- data.frame(column1=column1, column2=column2, column3=column3)
require(ggplot2)
ggplot(d, aes(x=column1, y=column3, fill=column2)) + geom_bar(position="dodge", stat = "identity")

enter image description here