绘制并比较条形图之间的差异

时间:2014-11-20 09:44:19

标签: r

我有一项工作是在同一图表上绘制几个数据,并在视觉上区分它们之间的差异。以下是我的代码。假设我们绘制数据[4]和数据[3]。在线性线与x轴交叉的右侧,数据[4]的斜率低于数据[3]。同时,在左侧,数据[4]的斜率高于数据[3]。所以,问题出现在右侧,如果我首先绘制数据[4],然后是数据[3];数据[3]将覆盖现有数据[4]的条形图。左手边对我来说还可以,我可以在视觉上区分btw 2的差异区域...... 任何建议都会受到高度赞赏。非常感谢。

############################################################
rm(list=ls())
cat("\014")
dev.off()

#number of barplot
n=4

#plotting order, from the graph with lowest slope to highest slope
case = seq(4,1,-1)
case
##########
data = matrix(
  0,# the data elements
  nrow=n,              # number of rows = #row of dataset
  ncol=64,              # number of columns of wji = #row of dataset
  byrow = TRUE)        # fill matrix by rows 

# ############################################################
# draw empty graph
par(new=TRUE)
plot(0,0,type = "b", xlim=c(0,64), ylim=c(-8,8),xlab = "x", ylab = "y")
abline(a=0,b=0,v=0)

############################################################
# from lighter to darker when index increases
color_range<-gray.colors(n+1,start=1,end=0)
color_range


n=4
##########
data = matrix(
  0,# the data elements
  nrow=n,              # number of rows = #row of dataset
  ncol=64,              # number of columns of wji = #row of dataset
  byrow = TRUE)   
data[1,]=seq(0,0,length.out=64);data[1,]
data[2,]=seq(-6,6,length.out=64);data[2,]
data[3,]=seq(-7,5,length.out=64);data[3,]
data[4,]=seq(-8,4,length.out=64);data[4,]

for (n in case){
  data_plot <- data[n,]
  data_plot    
  #===========

  readline(prompt = "Pause. Press <Enter> to continue...")

  barplot(data_plot,  col = color_range[n], add=T, density=-1)

  readline(prompt = "Pause. Press <Enter> to continue...")

  #save each plot into file
  file_ext="jpg"
  output_file = paste("plot_with_n_", n,file_ext, sep = ".")
  dev.copy(jpeg,filename=output_file);
  dev.off()

}#end for

enter image description here enter image description here

数据[4](见plot_with_n_.4.jpg) 数据[3](见plot_with_n_.3.jpg)

1 个答案:

答案 0 :(得分:2)

也许和ggplot一起?我认为透明度在这里很重要(因此你可以看到一切)。然而,与灰度一起,这可能仍然没有像希望的那样清晰......

n=4
data = matrix(
0,# the data elements
nrow=n,              # number of rows = #row of dataset
ncol=64,              # number of columns of wji = #row of dataset
byrow = TRUE)   
data[1,]=seq(0,0,length.out=64);data[1,]
data[2,]=seq(-6,6,length.out=64);data[2,]
data[3,]=seq(-7,5,length.out=64);data[3,]
data[4,]=seq(-8,4,length.out=64);data[4,]

#retransform data so it can be used easily with ggplot
mydata=melt(data.frame(t(data)))
mydata$x=1:ncol(data)

color_range<-gray.colors(n+1,start=1,end=0)

ggplot(mydata,aes(x=x,y=value,fill=variable))+
  geom_bar(stat="identity",alpha=0.3,color="black",position=position_dodge(width = 0),width=4) +  # dodged to avoid stacking, width=0 to have bars at same X and width=4 (objective) to have bars touching for a nicer look
  scale_fill_manual(values=color_range)+
  theme_classic()+  # this and the next two lines to make it look like your plot
  ylab("y")+
  theme(legend.position="none") 

enter image description here