简体   繁体   English

从x和y轴绘制带有其索引的通用标签

[英]Plot common labels from x and y axis with their index

I am new to R and I want to plot graph as example. 我是R的新手,我想以图表为例。 My x and y values are as follow: 我的x和y值如下:

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

Now on the x-axis I want the the value (not the index but the names which are shown above as 'a') of a, and on the y-axis 'b' from above table. 现在在x轴上,我想要a的值(不是索引,而是上面显示为'a'的名称),在上表的y轴上是'b'。
Now I want to plot scatter points at intersecting point. 现在,我想在相交点处绘制散点。 For ex. 对于前。 in 'a' the index of 'orange' is 1 and in 'b' the index of 'orange' is 4, so in the graphIi want point at (1,4) the intersect point of orange. 在'a'中,'orange'的索引为1,在'b'中,'orange'的索引为4,因此在图上,想要的点位于(1,4)橙色的相交点。
Same for the rest of the values. 其余值相同。 I don't know how to plot this and I am suffering from past few days. 我不知道该如何绘图,而我正遭受过去几天的困扰。

I changed one of the butters to potato since I assumed this was a typo. 因为我认为这是错字,所以我将其中一种黄油改成了马铃薯。

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)

在此处输入图片说明

edit 编辑

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM