繁体   English   中英

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

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

我正在使用构面网格生成我的数据的简洁演示。 基本上,我的数据框有四列:

idx,密度,标记,大小写。

有5种情况,每种情况对应5个标记,每个标记对应多个idx,每个idx对应一个密度。

数据上传到这里: 数据框链接

我尝试使用facet_grid实现我的目标,但是,我得到了一个非常混乱的图表: 在此处输入图片说明

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)

编辑:

我重新上传了文件,现在应该可以了: 在这里下载文件

所有4列均已被读取为因素。 但是,这是由于您将数据加载到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 ...

好消息是您可以使用以下方法修复它:

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

尽管您应该研究如何加载数据,以避免将来发生。

作为另一个技巧,请尝试使用使用as.character调用的上述代码,然后比较差异。

正如MrGumble所解释的那样idxdensity变量是类型因子,但应将其绘制为数字。

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