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

China☆狼群 提交于 2019-11-27 19:08:00

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!

Yes, you can do this. Use scale_color_manual, scale_fill_manual and scale_x_discrete as follows:

# specify colors and order 
colorder <- c( "green", "orange", "red", "blue") 
bplot<-ggplot(boxplots, 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) +
  scale_fill_manual(breaks=colorder,  # fill scale (for boxes)
                    limits=colorder, 
                    values=colorder) +
  scale_x_discrete(limits=colorder)   # order of x-axis
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!