修复ggplot2中的x轴排序 [英] fix x-axis ordering in ggplot2

查看:269
本文介绍了修复ggplot2中的x轴排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

I have the following data

dftmp
   z.transient kprimes       groupname
1    -1.244061      10    k=9.8,p=56.4
2    -0.995249      20    k=9.8,p=56.4
3    -0.746437      30    k=9.8,p=56.4
4    -0.497625      40    k=9.8,p=56.4
5    -0.248812      50    k=9.8,p=56.4
6            0      60    k=9.8,p=56.4
7     0.248812      70    k=9.8,p=56.4
8     0.497625      80    k=9.8,p=56.4
9     0.746437      90    k=9.8,p=56.4
10    0.995249     100    k=9.8,p=56.4
11    1.244061     110    k=9.8,p=56.4
12   -1.244061     100 k=103.2,p=155.5
13   -0.995249     200 k=103.2,p=155.5
14   -0.746437     300 k=103.2,p=155.5
15   -0.497625     400 k=103.2,p=155.5
16   -0.248812     500 k=103.2,p=155.5
17           0     600 k=103.2,p=155.5
18    0.248812     700 k=103.2,p=155.5
19    0.497625     800 k=103.2,p=155.5
20    0.746437     900 k=103.2,p=155.5
21    0.995249    1000 k=103.2,p=155.5
22    1.244061    1100 k=103.2,p=155.5
23   -1.244061    1000   k=786.9,p=849
24   -0.995249    2000   k=786.9,p=849
25   -0.746437    3000   k=786.9,p=849
26   -0.497625    4000   k=786.9,p=849
27   -0.248812    5000   k=786.9,p=849
28           0    6000   k=786.9,p=849
29    0.248812    7000   k=786.9,p=849
30    0.497625    8000   k=786.9,p=849
31    0.746437    9000   k=786.9,p=849
32    0.995249   10000   k=786.9,p=849
33    1.244061   11000   k=786.9,p=849

我想用ggplot2来绘制它

I would like to plot it with ggplot2

p <- ggplot(dftmp, aes(x=z.transient, y=kprimes, group=groupname))
p <- p + geom_line(aes(colour=groupname), size=2) 
p <- p + scale_y_log10()

但是,ggplot2似乎将因子从0开始排序,然后替换为负值和正值,这样我就可以在每个小区中获得一条波浪线:

But, ggplot2 seems to be ordering the factor as starting from 0, then alternatiing negative and positive, such that I get a wavy line in each plot:

如何重新排列x因子。另外,如何为y轴指定轴限制?

How do I reorder the x factor. Also, how do I specify the axis limits for the y axis?

推荐答案

这是因为您的 z。瞬态是你说的一个因素。在我看来,它是一个连续变量不是?如果是这样,请将其从一个因子转换为值(请参阅?factor )。

That's because your z.transient is a factor like you said. It seems to me that its a continuous variable no? If so, convert it from a factor to the value (see ?factor).

dftmp$z.transient <- as.numeric(levels(dftmp$z.transient))[dftmp$z.transient]

此外,由于您的数据是现在的,如果我直接使用它,因为 z.transient 是数字,所以情节看起来很好。试试吧,使用:

Also, as your data is now, if I use it directly the plot looks fine since z.transient is numeric. Try it, use:

dftmp <- read.table('clipboard')

然后按照你的绘图步骤......

Then follow your plotting steps...

就轴限制而言,一个href =https://stackoverflow.com/questions/3606697/how-to-set-x-axis-limits-in-ggplot2-r-plots>这篇文章应该引导你在正确的方向。

As far as axis limits, this post should steer you in the right direction.

这篇关于修复ggplot2中的x轴排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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