是否有R函数将多列转换为一列,创建另一个新列以为每个值命名?

时间:2019-08-15 18:13:33

标签: r

使用R,我有7列寄生虫丰度数据和计数数据。每行都有一个脾脏重量值,代表该特定样品的脾脏重量。为了分析的目的,我希望创建一个图,在x轴上具有脾脏重量,在y轴上具有这七个物种的寄生虫丰度。在使用ggplot2之前,我已经制作了这种类型的图,但是,为此,我首先在excel中重新排列了值。

我希望将其转换为:

data.frame(
    Heligmosomoides = c(0L, 0L, 42L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 8L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 19L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Heligmosoma = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 19L, 2L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Trichuris = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L), 
    Mastophorus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L), 
    Aoncotheca = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 11L, 0L, 4L, 0L, 0L, 0L, 0L, 10L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Syphacia = c(0L, 50L, 0L, 0L, 0L, 71L, 0L, 0L, 212L, 0L, 0L, 0L, 0L, 0L, 0L, 361L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 532L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Tapeworms = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L), 
    spleenweight = c(1.111, 1.119, 1.112, 1.137, 1.137, 1.14, 1.15, 1.262, 1.121, 1.196, 1.129, 1.147, 1.226, 1.22, 1.118, 1.165, 1.114, 1.354, 1.137, 1.175, 1.3811, 1.239, 1.26, 1.216, 1.141, 1.226, 1.366, 1.155, 1.221, 1.156, 1.87, 1.211, 1.4, 1.219, 1.166, 1.227, 1.153, 1.331, 1.232)
)

对此:

data.frame(
    Spleen.weight = c(1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4, 1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4, 1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4, 1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4, 1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4, 1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4, 1.155, 1.15, 1.175, 1.118, 1.239, 1.14, 1.121, 1.87, 1.141, 1.156, 1.26, 1.119, 1.137, 1.129, 1.112, 1.111, 1.114, 1.137, 1.166, 1.137, 1.153, 1.331, 1.262, 1.22, 1.354, 1.3811, 1.226, 1.366, 1.232, 1.147, 1.211, 1.219, 1.227, 1.216, 1.165, 1.226, 1.221, 1.196, 1.4), 
    Count = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 8L, 0L, 0L, 0L,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factor(c("Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosomoides", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Heligmosoma", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Trichuris", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Mastophorus", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Aonchotheca", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Syphacia", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms", "Tapeworms"))
)

获得转换后的数据后,使用ggplot绘制数据。

splab <- ggplot(DF8, aes(x=ï..Spleen.weight, y=Count), group=Parasite, color=Parasite) + 
  geom_point()

由于某种原因,此ggplot代码似乎没有使用颜色来区分寄生虫组。

0 个答案:

没有答案