facet_wrap绘制错误的x轴坐标 [英] facet_wrap plotting incorrect x axis co-ordinates

查看:34
本文介绍了facet_wrap绘制错误的x轴坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不明白这一点.

这是我的基因组数据

structure(list(chr = c(10, 10, 11, 12, 13, 13, 17, 2, 20, 22, 
3, 3, 4, 4, 4, 4, 5, 7, 7, 8), leftPos = c(240000, 24840000, 
7200000, 6120000, 14880000, 18120000, 8760000, 53280000, 10680000, 
8640000, 13320000, 46920000, 12000000, 13560000, 16680000, 30360000, 
16440000, 2280000, 31560000, 28320000), Means.x = c(255.903115167852, 
250.944147412273, 221.51819750622, 351.093122004609, 289.007439556107, 
219.45204288982, 225.535183746474, 457.871356482534, 253.497055532121, 
252.20121505887, 342.200678275566, 373.699212483745, 1014.42590543955, 
221.696823711274, 240.80888805777, 249.180706358065, 284.401983997314, 
269.740366732235, 278.570789472848, 280.990393375634), Def.x = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Means.y = c(236.86281805995, 
226.558139428814, 242.372785637286, 250.366569266078, 300.979628259253, 
241.055506095359, 227.580531582224, 373.326888100031, 212.752136489909, 
422.948449610324, 224.089190457845, 310.029877851832, 1014.42590543955, 
249.285880751277, 285.16587617125, 230.051744541219, 221.151463979895, 
289.409617875006, 317.10711734718, 262.296533161901), Def.y = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("chr", 
"leftPos", "Means.x", "Def.x", "Means.y", "Def.y"), row.names = c(NA, 
-20L), class = "data.frame")

我试图根据每个染色体的位置(leftPos)简单地绘制1的值.

I am trying to simply plot the values of 1 according to their position (leftPos) according to each chromosome.

但是使用以下代码:

ggplot(ZoutliersM,aes(x = ZoutliersM$leftPos,y = as.numeric(ZoutliersM$Def.x),
                         xend=ZoutliersM$leftPos,yend=0))+

  geom_point(fill="magenta",size=2,colour="red")+
  facet_wrap(~ chr)

我得到如下图:

这看起来不错,但是这些点不在该染色体的x轴上的正确位置.例如,第22号染色体有一个点,根据数据集应为

This looks fine but the points are not at the correct position on the x-axis for that chromosome. For example, chromosome 22 has one point which according to the dataset should be

 chr leftPos    Means     Def
  22 8640000    422.9484   1

但是当我查看该图时,它位于20到30M之间的某个地方

but when I look at the plot it is somewhere over between 20 and 30M

为什么这样绘制图,我该如何纠正?好像x轴刻度与图表无关.

Why is this being plotted like this and how can I correct it? Its as if the x axis scale has nothing to do with the chart.

推荐答案

ggplot 具有 data 参数是有原因的.当您在 aes()中重新指定数据框时,它会覆盖构面的子设置和排序.只是不必重新指定数据框的名称(没有 mydata $ column ),一切正常:

ggplot has a data argument for a reason. When you re-specify the data frame inside aes(), it overrides the subsetting and ordering done for the faceting. Just don't re-specify the name of the data frame (no mydata$column) and everything works fine:

ggplot(ZoutliersM,
       aes(x = leftPos,
           y = as.numeric(Def.x),
           xend = leftPos,
           yend = 0)) +
  geom_point(fill = "magenta", size = 2, colour = "red") +
  facet_wrap(~chr)

现在我们可以看到,在"22"小平面上,该点比预期的要少10M.

Now we can see that in the "22" facet, the point is a little under 10M, as expected.

另外两个注意事项:

  • 指定 geom_point 的填充"不会做任何事情,除非您还使用具有单独填充和颜色的形状,例如 shape = 21

  • specifying a "fill" for geom_point won't do anything unless you also use a shape that has separate fill and colors, such as shape = 21

在您的 dput 数据中, Def.x 已经是数字,因此您无需对其进行转换.如果以前是一个因素,请确保使用 as.numeric(as.character(Def.x))进行转换,否则,您将只是获取级别而不是将值转换为数值./p>

In your dput data, Def.x is already numeric, so you don't need to convert it. If it was a factor previously, make sure you convert with as.numeric(as.character(Def.x)), otherwise you'll simply be taking the levels rather than the value to numeric.

这篇关于facet_wrap绘制错误的x轴坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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