不知道如何绘制这个,一个带有类别的散点图

时间:2014-04-12 16:56:47

标签: r excel plot

我有以下数据集

SIZE ALG1 ALG2 ALG3
  A   2    3    5
  A   3    2    1
  A   1    2    2
  B   10  10    11
  B   12  12   12

我喜欢SIZE列的水平轴和YG1 ALG2 ALG3系列的Y值。

E.g。

enter image description here

我如何通过Excel(实际上是OOCALC)或R?

获得它

一般情况下,我会在x轴上重复SIZE值,然后显示更长的时间。

TIA

2 个答案:

答案 0 :(得分:0)

在R中你可以这样做:

df <- data.frame(
   size = c(rep('A', 3), rep('B', 2)),
   alg1 = c(2,3,1,10,12),
   alg2 = c(3,2,2,10,12),
   alg3 = c(5,1,2,11,12)
   )

plot(alg1 ~ jitter(as.numeric(size)), data = df,
  xlim = c(0.5, 2.5), ylim = c(0, 14), col = 'red',
  axes = FALSE, xlab = 'Size', ylab = 'Alg')
points(alg2 ~ jitter(as.numeric(size)), data = df, col = 'blue')
points(alg3 ~ jitter(as.numeric(size)), data = df, col = 'black')
axis(side = 2)
axis(side = 1, at = c(1, 2), labels = c('A', 'B'))
legend(x = 2, y = 4, legend = c('alg1', 'alg2', 'alg3'),
  col = c('red', 'blue', 'black'), lty = 1)

答案 1 :(得分:0)

假设您的数据框名为df1

您必须使用reshape2::melt

以长格式转换它
melt(df1) -> dfmelt

library(ggplot2)

ggplot(data=dfmelt, aes(x=SIZE, y=value)) + geom_jitter(aes(color=variable))

或者,更简单快速:

qplot(data=dfmelt, x=SIZE, y=value, geom="jitter", color=variable)

如果你只是想要没有&#34;抖动&#34;它们:

qplot(data=dfmelt, x=SIZE, y=value, geom="point", color=variable)