我在这里为我的大数据集创建了一个小例子,有超过400000条记录,我能够绘制point_plot,这里是代码:
Data1 <- data.frame(State=rep('SC',24),ID=rep(11,24),Month=c(rep(1,times=9),
rep(2,times=6),rep(3,times=9)),Day=c(rep(1:3,each=3),rep(1:2,each=3),
rep(1:3,each=3)),Group=rep(1:3,8),Value=rep(10.1:20.9,length.out=24))
Data2 <- Data1[rep(1:nrow(Data1),4),]
Data <- data.frame(State=c(rep('SC',48),rep('NC',48)),ID=c(rep(11:14,each=24)),
Month=Data2$Month,Day=Data2$Day,Group=rep(1:3,32),Value=rep(10:20,length.out=96))
states = unique(Data$State)
for(j in 1:length(states)) {
jpeg(file=paste("Pic", j, ".jpeg", sep=""))
data <- subset(Data,State==states[j])
plot(data$Month, data$Value, type="p", xlab="Months", ylab="Value")
colors <- rainbow(3)
for (i in 1:3) { # add lines
group <- subset(data, Group==i)
lines(group$Month, group$Value, type="p", col=colors[i])
}
title(paste(unique(data$State),"Value",sep=' ')) # add a title and subtitle
leg.txt <- c("G1","G2","G3") # add a legend
legend("topleft", legend=leg.txt, fill=colors, bty="o")
dev.off()
}
但是现在我需要每个月将3个组并排绘制bar_plot,我尝试了以下两个,但是无法做到正确:
1)ggplot(data, aes(factor(data$Month), data$Value, fill = factor(data$Group))) +
geom_bar(position = "dodge", width = 0.5)
scale_x_discrete(labels = data$Month)
2) barplot(data$Value,beside=T,names.arg=factor(data$Month))
任何帮助将不胜感激!
答案 0 :(得分:0)
您需要stat = "identity"
的参数geom_bar
:
ggplot(data, aes(as.factor(Month), Value, fill = as.factor(Group))) +
geom_bar(position = "dodge", width = 0.5, stat = "identity") +
scale_fill_discrete("Group", labels = c("N", "E", "L"))
答案 1 :(得分:0)
ggplot(Data, aes(x=Month, y=Value, fill=as.factor(Group))) +
geom_bar(stat="identity", position="dodge", color="black")