简体   繁体   English

使用R中的facet_grid的轴混乱的重叠数据

[英]Overlapped data with messed up axises using facet_grid in R

I am using facet grid to generate neat presentations of my data. 我正在使用构面网格生成我的数据的简洁演示。 Basically, my data frame has four columns: 基本上,我的数据框有四列:

idx, density, marker, case. idx,密度,标记,大小写。

There are 5 cases, each case corresponds to 5 markers, and each marker corresponds to multiple idx, each idx corresponds to one density. 有5种情况,每种情况对应5个标记,每个标记对应多个idx,每个idx对应一个密度。

The data is uploaded here: data frame link 数据上传到这里: 数据框链接

I tried to use facet_grid to achieve my goal, however, I obtained a really messed up graph: 我尝试使用facet_grid实现我的目标,但是,我得到了一个非常混乱的图表: 在此处输入图片说明

The x-axis and y-axis are messed up, the codes are: x轴和y轴被弄乱了,代码是:

library(ggplot2)
library(cowplot)
plot.density <-
  ggplot(df_densityWindow, aes(x = idx, y = density)) +
  geom_col() +
  facet_grid(marker ~ case, scales = 'free') +
  background_grid(major = 'y', minor = "none") + # add thin horizontal lines
  panel_border() # and a border around each panel
plot(plot.density)

EDIT: 编辑:

I reupload the file, now it should be work: download file here 我重新上传了文件,现在应该可以了: 在这里下载文件

All 4 columns have been read as factors. 所有4列均已被读取为因素。 This is an issue from however you loaded the data into R. Take a look at: 但是,这是由于您将数据加载到R中而引起的。请看一下:

df <- readRDS('df.rds')
str(df)
'data.frame':   52565 obs. of  4 variables:
 $ idx    : Factor w/ 4712 levels "1","10","100",..: 1 1112 2223 3334 3546 3657 3768 3879 3990 2 ...
 $ density: Factor w/ 250 levels "1022.22222222222",..: 205 205 204 203 202 201 199 198 197 197 ...
 $ marker : Factor w/ 5 levels "CD3","CD4","CD8",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ case   : Factor w/ 5 levels "Case_1","Case_2",..: 1 1 1 1 1 1 1 1 1 1 ...

Good news is that you can fix it with: 好消息是您可以使用以下方法修复它:

df$idx <- as.integer(as.character(df$idx))
df$density <- as.numeric(as.character(df$density))

Although you should look into how you are loading the data, to avoid future. 尽管您应该研究如何加载数据,以避免将来发生。

As another trick, try the above code without using the as.character calls, and compare the differences. 作为另一个技巧,请尝试使用使用as.character调用的上述代码,然后比较差异。

As already explained by MrGumble , the idx and density variables are of type factor but should be plotted as numeric. 正如MrGumble所解释的那样idxdensity变量是类型因子,但应将其绘制为数字。

The type.convert() function does the data conversion in one go: type.convert()函数可以一次完成数据转换:

library(ggplot2)
library(cowplot)
ggplot(type.convert(df_densityWindow), aes(x = idx, y = density))    + 
  geom_col() + 
  facet_grid(marker ~ case, scales = 'free') +
  background_grid(major = 'y', minor = "none") + # add thin horizontal   lines 
  panel_border() # and a border around each panel

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM