2015-10-18 81 views
0

我是R新手,我想绘制图形作为示例。我的X和Y值如下:从x和y轴绘制常用标签及其索引

a   b 
orange  tomato 
apple  butter 
tomato  graps 
chiku  orange 
graps  apple 
potato  chiku 
onion  butter 
ginger  cheese 
cheese  onion 
butter  ginger 

现在在x轴我希望的值(而不是索引但是这在上面示出为“A”的名称)的一个,并且在y从上表中的轴'b'。
现在我想绘制相交点的散点。例如。在'a'中,'orange'的索引是1,在'b'中'orange'的索引是4,因此在图中我想指出在(1,4)橙色的相交点。
其余的值相同。我不知道该如何策划这一点,而且我受到过去几天的苦难。

回答

1

因为我认为这是一个错字,所以我将其中一个黄油换成了马铃薯。

dd <- read.table(header = TRUE, text = "a   b 
orange  tomato 
apple  butter 
tomato  graps 
chiku  orange 
graps  apple 
potato  chiku 
onion  potato 
ginger  cheese 
cheese  onion 
butter  ginger") 

dd <- within(dd, { 
    x <- factor(a, levels = a) 
    y <- factor(x, levels = b) 
}) 

plot(idx <- sapply(dd[, c('x','y')], as.numeric)) 
text(idx, labels = dd$a, pos = 1, xpd = NA) 

enter image description here

编辑

plot(idx <- sapply(dd[, c('x','y')], as.numeric), xaxt = 'n', yaxt = 'n') 
text(idx, labels = dd$a, pos = 1, xpd = NA) 

axis(1, at = seq_along(dd$a), labels = dd$a) 
axis(2, at = seq_along(dd$b), labels = dd$b, las = 1) 

enter image description here

+0

我还以为你上面的澄清的问题是一个笑话。否则这没有说服我。 –

+0

非常感谢你BondedDust。我是R新手,所以我不知道。但你能告诉我,这是可能的,在X轴和Y轴我想要的名称,而不是数值在X我想橙,苹果,番茄,...和Y相同。谢谢 – Maulik

+0

@Maulik你可以通过抑制勾号标签'xaxt ='n''并使用'axis'来实现自定义标签,参见编辑 – rawr