如何使用R ggplot更改x轴刻度标签名称,顺序和boxplot颜色? [英] How to change x-axis tick label names, order and boxplot colour using R ggplot?

查看:2620
本文介绍了如何使用R ggplot更改x轴刻度标签名称,顺序和boxplot颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含csv文件的文件夹,每个文件包含两列数据,例如:

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

我使用以下R代码生成箱形图:

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()
}

现在我想改变boxplots的颜色以匹配它们相应的x轴颜色标签。我也想更改x轴标签的名称,以及它们的顺序。有没有办法使用ggplot或qplot做到这一点?

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?

推荐答案

建立@ shadow的答案,以下是如何手动更改x轴标签。我还引入了一些其他的改变,这些改变有助于改善图形和图例的外观:

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



标签选项添加到图的 scale_x_discrete 图层可以让您更改轴标签。将标签添加到 scale_fill_manual scale_color_manual 可让您更改图例标签。将名称添加到两者都可以让您更改图例标题。最后,我在图中添加了 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!

这篇关于如何使用R ggplot更改x轴刻度标签名称,顺序和boxplot颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆