Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

ggplot2 - How to change x-axis tick label names, order and boxplot colour using R ggplot?

I have a folder containing csv files, each with two columns of data e.g.:

0,red
15.657,red
0,red
0,red
4.429,red
687.172,green
136.758,green
15.189,red
0.152,red
23.539,red
0.348,red
0.17,blue
0.171,red
0,red
61.543,green
0.624,blue
0.259,red
338.714,green
787.223,green
1.511,red
0.422,red
9.08,orange
7.358,orange
25.848,orange
29.28,orange

I am using the following R code to generate the boxplots:

files <- list.files(path="D:/Ubuntu/BoxPlots/test/", pattern=NULL, full.names=F, recursive=FALSE)
files.len<-length(files)
col_headings<-c("RPKM", "Lineage")

for (i in files){
  i2<-paste(i,"png", sep=".")
  boxplots<-read.csv(i, header=FALSE)
  names(boxplots)<-col_headings
  png(i2)
  bplot<-ggplot(boxplots, aes(Lineage, RPKM)) + geom_boxplot(aes(fill=factor(Lineage))) + geom_point(aes(colour=factor(Lineage)))
  print(bplot)
  graphics.off()
}

Now I want to change the colour of the boxplots to match their corresponding x-axis colour labels. I also want to change the names of the x-axis labels, and also their order. Is there a way to do this using ggplot or qplot?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Building off of @shadow's answer, here's how you can manually change the x-axis labels. I also threw in a couple other changes which help improve the look of the graph and legend:

colorder <- c( "green", "orange", "red", "blue")
bplot<-ggplot(temp, aes(Lineage, RPKM)) + 
    geom_boxplot(aes(fill=factor(Lineage))) + 
    geom_point(aes(colour=factor(Lineage))) + 
    scale_color_manual(breaks=colorder, # color scale (for points)
                     limits=colorder, 
                     values=colorder,
                     labels=c("hESC1","hESC2","hESC3","hESC4"),
                     name="Group") +
    scale_fill_manual(breaks=colorder,  # fill scale (for boxes)
                     limits=colorder, 
                     values=colorder,
                     labels=c("hESC1","hESC2","hESC3","hESC4")
                     name="Group") +
    scale_x_discrete(limits=colorder,labels=c("hESC1","hESC2","hESC3","hESC4")) +
    theme_bw()

Adding the labels option to the scale_x_discrete layer of the plot allows you to change the axis labels. Adding labels to both scale_fill_manual and scale_color_manual allows you to change the legend labels. Adding name to both lets you change the legend heading. Finally, I added theme_bw() to the plot to make the background white and add a border around the plot. Hope that helps!

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...