3

您好,我有这组数据:

 my.data=read.table(text="         values tag class
 1   100.0000000  X3    CS
 2   100.0000000  X5    CS
 3    54.2370601  X7    CS
 4    63.4813701  X7    CS
 5    51.0402580  X7    CS
 6    60.1430236  X7    CS
 7    70.3939619  X7    CS
 8    56.5981165  X7    CS
 9    75.4148038  X7    CS
 10   88.2687052  X7    CS
 11   70.9697583  X7    CS
 12   54.2014007  X7    CS
 13   63.4325244  X7    CS
 14   51.0086771  X7    CS
 15   60.1034812  X7    CS
 16   70.3397973  X7    CS
 17   56.5630966  X7    CS
 18   75.3652206  X7    CS
 19   88.2007869  X7    CS
 20   70.9258461  X7    CS
 21   38.6325912  X9    CS
 22   27.7455102  X9    CS
 23   21.1778773  X9    CS
 24  100.0000000  X9    CS
 25   73.0803007  X9    CS
 26   55.7814809  X9    CS
 27   85.8614803  X9    CS
 28   38.5666461  X9    CS
 29   27.7114796  X9    CS
 30   21.1580450  X9    CS
 31   72.9906659  X9    CS
 32   55.7292434  X9    CS
 33   85.7810739  X9    CS
 34    0.0000000 XAS    CS
 35   49.0701633 XPW    CS
 36   40.0228822 XPW    CS
 37   23.8702273 XPW    CS
 38  100.0000000 XPW    CS
 39   89.3022370 XPW    CS
 40   53.2611491 XPW    CS
 41   69.9892886 XPW    CS
 42    0.0000000 XPW    CS
 43   14.8840750  X3    CD
 44   17.7316138  X3    CD
 45    6.1164435  X3    CD
 46    0.0000000  X3    CD
 47    1.1435141  X3    CD
 48   14.8904265  X3    CD
 49   17.7375474  X3    CD
 50    6.1241709  X3    CD
 51    1.1506441  X3    CD
 52   14.6751282  X3    CD
 53   17.5364297  X3    CD
 54    5.8621689  X3    CD
 55    0.9089743  X3    CD
 56   74.4165986  X5    CD
 57   76.7441666  X5    CD
 58   80.9582780  X5    CD
 59   58.5811936  X5    CD
 60   62.3494607  X5    CD
 61   69.1719883  X5    CD
 62   57.2509995  X5    CD
 63   61.1402872  X5    CD
 64   68.1819250  X5    CD
 65   74.3887236  X5    CD
 66   76.7211353  X5    CD
 67   80.9428400  X5    CD
 68   58.5360647  X5    CD
 69   62.3121736  X5    CD
 70   69.1469947  X5    CD
 71   57.2044213  X5    CD
 72   61.1018026  X5    CD
 73   68.1561287  X5    CD
 74   74.3425809  X5    CD
 75   76.6830205  X5    CD
 76   80.9173038  X5    CD
 77   58.4613610  X5    CD
 78   62.2504669  X5    CD
 79   69.1056522  X5    CD
 80   57.1273184  X5    CD
 81   61.0381141  X5    CD
 82   68.1134585  X5    CD
 83   66.3076784  X7    CD
 84   60.5650688  X7    CD
 85   68.2935472  X7    CD
 86   49.3767792  X7    CD
 87   40.7484217  X7    CD
 88   52.3605769  X7    CD
 89   36.4202616  X7    CD
 90   25.5835605  X7    CD
 91   40.1677332  X7    CD
 92   66.3298303  X7    CD
 93   60.5954120  X7    CD
 94   68.3131654  X7    CD
 95   49.4100626  X7    CD
 96   40.7940127  X7    CD
 97   52.3900536  X7    CD
 98   36.4620636  X7    CD
 99   25.6408202  X7    CD
 100  40.2047541  X7    CD
 101  66.1477122  X9    CD
 102  75.6876522  X9    CD
 103  81.4426220  X9    CD
 104  55.0173821  X9    CD
 105  67.6939692  X9    CD
 106  75.3411217  X9    CD
 107  51.6125069  X9    CD
 108  65.2486246  X9    CD
 109  73.4746140  X9    CD
 110  66.2054974  X9    CD
 111  75.7174719  X9    CD
 112  81.4600003  X9    CD
 113  55.0941665  X9    CD
 114  67.7335933  X9    CD
 115  75.3642139  X9    CD
 116  51.6951034  X9    CD
 117  65.2912480  X9    CD
 118  73.4994541  X9    CD
 119  25.6270138 XAS    CD
 120  45.2920118 XAS    CD
 121  44.5101287 XAS    CD
 122   0.0000000 XAS    CD
 123  17.9916840 XAS    CD
 124  16.8196263 XAS    CD
 125   7.3447585 XPW    CD
 126  24.4280115 XPW    CD
 127  54.9277702 XPW    CD
 128   0.0000000 XPW    CD
 129   4.6378241 XPW    CD
 130  43.1246155 XPW    CD
 131  39.7525599 XPW    CD
 132 100.0000000 XPW    CD")

用 qplot() 绘制这些数据会给我以下结果:

qplot(tag,values,colour=factor(class),data=my.data,geom="jitter")+stat_summary(fun.y='mean', geom='errorbarh', aes(xmin=as.integer(tag)-0.3, xmax=as.integer(tag)+0.3), height=0)

抖动图

我想将同一列中的蓝色和红色点拆分为 2 个独立且彼此相邻的列,以提供类似的内容,但使用我的抖动点:

例子

既然我们在这里,你知道如何减少列的宽度(减少给定 x 的点的宽度空间)以使列更窄吗?另外如何在列之间添加空间甚至是水平条,以便我们清楚地看到不同的列?

4

2 回答 2

3

这是一个解决方案。它的灵感来自这个答案的总体思路。

# create an interaction of both factors
int <- with(my.data, interaction(class, tag)) 
# create a numeric variable (tag2 is used instead of tag)
my.data <- transform(my.data, tag2 = as.numeric(int))
# calculate positions of x-axis breaks
breaks <- seq_along(levels(int))[c(TRUE, FALSE)] + .5

library(ggplot2)
ggplot(data = my.data, aes(x = tag2, y = values, color = as.factor(class))) +
  geom_jitter(position = position_jitter(width = .4)) +
  stat_summary(fun.y = 'mean', geom = 'errorbarh', 
               aes(xmin = tag2 - 0.4, xmax = tag2 + 0.4), height = 0) +
  scale_x_continuous(breaks = breaks, labels = levels(my.data$tag))

在此处输入图像描述

于 2013-06-24T16:40:43.957 回答
1

看看这是否可行。. .

ggplot(data=my.data, aes(x = factor(tag), fill = factor(class), y = values )) + 
  geom_dotplot(binaxis = "y", stackdir="center", position = "dodge", size = 1, 
               binwidth = 4, stackratio = .5) + 
  stat_summary(fun.y='mean', geom='errorbarh', aes(
    xmin=as.integer(tag)-0.3, xmax=as.integer(tag)+0.3), height=0) + 
  scale_fill_brewer(palette="Set1")

抖动点

我通过在线文档发现了这一点。

于 2013-06-24T16:55:09.913 回答