0

I have those 2 lists :

> FHM_CS
$X3
[1] 100

$X5
[1] 100

$X7
 [1] 54.23706 63.48137 51.04026 60.14302 70.39396 56.59812 75.41480 88.26871 70.96976 54.20140 63.43252
[12] 51.00868 60.10348 70.33980 56.56310 75.36522 88.20079 70.92585

$X9
 [1]  38.63259  27.74551  21.17788 100.00000  73.08030  55.78148  85.86148  38.56665  27.71148  21.15804
11]  72.99067  55.72924  85.78107

$XAS
[1] 0

$XPW
[1]  49.07016  40.02288  23.87023 100.00000  89.30224  53.26115  69.98929   0.00000

and

> FHM_CD
$X3
 [1] 14.8840750 17.7316138  6.1164435  0.0000000  1.1435141 14.8904265 17.7375474  6.1241709  1.1506441
[10] 14.6751282 17.5364297  5.8621689  0.9089743

$X5
 [1] 74.41660 76.74417 80.95828 58.58119 62.34946 69.17199 57.25100 61.14029 68.18193 74.38872 76.72114
[12] 80.94284 58.53606 62.31217 69.14699 57.20442 61.10180 68.15613 74.34258 76.68302 80.91730 58.46136
[23] 62.25047 69.10565 57.12732 61.03811 68.11346

$X7
 [1] 66.30768 60.56507 68.29355 49.37678 40.74842 52.36058 36.42026 25.58356 40.16773 66.32983 60.59541
[12] 68.31317 49.41006 40.79401 52.39005 36.46206 25.64082 40.20475

$X9
 [1] 66.14771 75.68765 81.44262 55.01738 67.69397 75.34112 51.61251 65.24862 73.47461 66.20550 75.71747
[12] 81.46000 55.09417 67.73359 75.36421 51.69510 65.29125 73.49945

$XAS
[1] 25.62701 45.29201 44.51013  0.00000 17.99168 16.81963

$XPW
[1]   7.344758  24.428011  54.927770   0.000000   4.637824  43.124615  39.752560 100.000000

And I would like to do a "clustured jittered plot" for every line from both list. For example : X3 from FHM_CS right by X3 from FHM_CD and so one for every row of the lists.

I was thinking of using qplot from ggplot2 with geom="jitter", but I would also like to add an horizontal bar for each bar space to show the mean every list.

It would be something like this except I would like to add the mean for every list as a horizontal red bar (with its value if possible) and the clustering part (like FHM_CS in blue and FHM_CD in red).

jitterplot

So how to convert those list to dataframe and how to plot this from there ?

4

2 回答 2

1

要创建一个 data.frame,您可以像这样粗略地进行操作:

df <- data.frame(values=unlist(FHM_CS, use.names=FALSE), tag=rep(names(FHM_CS), times=sapply(FHM_CS, length))

但是对于使用ggplot2,我们应该将所有内容合并到一个数据框中:

df.CS <- data.frame(values=unlist(FHM_CS, use.names=FALSE), tag=rep(names(FHM_CS), times=sapply(FHM_CS, length)), class='CS', stringsAsFactors=TRUE)
df.CD <- data.frame(values=unlist(FHM_CD, use.names=FALSE), tag=rep(names(FHM_CD), times=sapply(FHM_CD, length)), class='CD', stringsAsFactors=TRUE)
my.data <- rbind(df.CS, df.CD)

编辑或者,正如 Michele 发现的那样,使用melt

library(reshape2)
df.CD <- data.frame(melt(FHM_CD), class='CD')
df.CS <- data.frame(melt(FHM_CS), class='CS')
## Except now, instead of `tag`, we have `L1`.
my.data <- rbind(df.CD, df.CS)
my.data$tag <- my.data$L1

编辑结束

然后根据需要进行绘图(我很懒,没有输入太多数据):

library(ggplot2)
ggplot(my.data, aes(x=interaction(tag, class), y=values)) + geom_point(position=position_jitter())

在此处输入图像描述

但是让我们尝试添加水平条。但是,我会使用 facetting,所以我们会得到以下结果:

ggplot(my.data, aes(x=tag, y=values)) + geom_point(position=position_jitter()) + stat_summary(fun.y='mean', geom='errorbarh', aes(xmin=as.integer(tag)-0.3, xmax=as.integer(tag)+0.3), height=0) + facet_grid(.~class)

在此处输入图像描述

编辑 2

首先手动创建交互向量:

my.data$it <- with(my.data, interaction(tag, class, sep=' - ', lex.order=TRUE))

然后我们像以前一样绘制。

ggplot(my.data, aes(x=it, y=values)) + geom_point(position=position_jitter()) + stat_summary(fun.y='mean', geom='errorbarh', aes(xmin=as.integer(it)-0.3, xmax=as.integer(it)+0.3, height=0, colour=class))

在此处输入图像描述

当然,您可能希望编辑参数以position_jitter()更接近点。

于 2013-06-24T07:24:57.007 回答
1
lst1 <- list("A", "B", "C")

lst2 <- list(rnorm(1000), rnorm(1000), rnorm(1000))


library(ggplot2)
library(reshape2)

df <- merge(melt(lst1, value.name="id"), melt(lst2), by="L1")
# L1 is just an output from melt.list method and represents the list items' index
> head(df)
  L1 id      value
1  1  A  2.0216986
2  1  A  1.4856589
3  1  A -0.2204599
4  1  A  0.6514056
5  1  A  0.3035737
6  1  A  0.8371660

qplot(id, value, data=df, geom="jitter")

在此处输入图像描述

于 2013-06-24T06:54:06.040 回答