你如何具体订购ggplot2 x轴而不是按字母顺序? [英] How do you specifically order ggplot2 x axis instead of alphabetical order?

查看:658
本文介绍了你如何具体订购ggplot2 x轴而不是按字母顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 ggplot2 来使用 geom_tiles 来制作 heatmap code> function
这里是我的代码如下:

  p <-ggplot(data,aes ,生物体))+ geom_tile(aes(fill = S))+ 
scale_fill_gradient(low =black,high =red)+
scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0))+
theme(legend.position =right,
axis.ticks = element_blank(),
axis.text。 x = element_text(size = base_size,angle = 90,hjust = 0,color =black),
axis.text.y = element_text(size = base_size,hjust = 1,color =black)) 。

data是我的data.csv文件

我的X轴是治疗类型

我的Y轴是生物体的类型

我不太熟悉命令和编程,因此我在这方面比较新。我只是想能够在x轴上指定标签的顺序。在这种情况下,我试图指定治疗的顺序。默认情况下,它按字母顺序排列。我如何覆盖这个/保持数据的顺序与我原来的csv文件一样?



我试过这个命令了!

  scale_x_discrete(limits = c(Y,X,Z))

其中x,y和z是我的治疗条件顺序。但它不能很好地工作,并给我缺少热箱。

解决方案

有点难以回答您的具体情况问题没有一个完整的,可重复的例子。然而,像这样的东西应该可以工作:

 #将您的'治疗'列转换为字符向量
data $ Treatment< ; - as.character(数据$治疗)
#然后将其重新转换为有序因子
data $治疗< - 因子(数据$治疗,水平=唯一(数据$治疗))

在本例中,该因子的顺序与数据中的顺序相同.csv 文件。



如果您喜欢不同的订单,您可以亲自订购:

  data $治疗< - 因素(数据$治疗,水平= c(Y,X,Z))

然而,如果你有很多关卡,这是很危险的:如果你弄错了他们,那会导致问题。


I'm trying to make a heatmap using ggplot2 using the geom_tiles function here is my code below:

p<-ggplot(data,aes(Treatment,organisms))+geom_tile(aes(fill=S))+
  scale_fill_gradient(low = "black",high = "red") + 
  scale_x_discrete(expand = c(0, 0)) + 
  scale_y_discrete(expand = c(0, 0)) + 
  theme(legend.position = "right", 
    axis.ticks = element_blank(), 
    axis.text.x = element_text(size = base_size, angle = 90, hjust = 0, colour = "black"),
    axis.text.y = element_text(size = base_size, hjust = 1, colour = "black")).

data is my data.csv file
my X axis is types of Treatment
my Y axis is types of organisms

I'm not too familiar with commands and programming and I'm relatively new at this. I just want to be able to specify the order of the labels on the x axis. In this case, I'm trying to specify the order of "Treatment". By default, it orders alphabetically. How do I override this/keep the data in the same order as in my original csv file?

I've tried this command

scale_x_discrete(limits=c("Y","X","Z"))

where x, y and z are my treatment condition order. It however doesn't work very well, and give me missing heat boxes.

解决方案

It is a little difficult to answer your specific question without a full, reproducible example. However something like this should work:

#Turn your 'treatment' column into a character vector
data$Treatment <- as.character(data$Treatment)
#Then turn it back into an ordered factor
data$Treatment <- factor(data$Treatment, levels=unique(data$Treatment))

In this example, the order of the factor will be the same as in the data.csv file.

If you prefer a different order, you can order them by hand:

data$Treatment <- factor(data$Treatment, levels=c("Y", "X", "Z"))

However this is dangerous if you have a lot of levels: if you get any of them wrong, that will cause problems.

这篇关于你如何具体订购ggplot2 x轴而不是按字母顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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