如何在ggplot2中添加X和Y点坐标?

时间:2018-04-20 15:30:20

标签: r ggplot2 visualization

我正在尝试制作ggplot,如何为点添加x和y坐标?我尝试了这段代码,但它不起作用:

d4<-Restriangle[1:(n-1),5]
xaxis=c(1:(n-1))
yaxis=Restriangle[1:(n-1),4]
data=data.frame(xaxis,yaxis)
library(ggplot2)
res=ggplot(df, aes(x = d, y = d4)) +  
# Set up canvas with outcome variable on y-axis
geom_point(data=data,size=4, shape=19,color = "blue"),aes(x = xaxis, y 
 = yaxis) )  
# Plot the actual points
res

这样做时我得到了这个

output

如何添加X-Y点坐标?

>Restriangle 
        1          2           3           4          5         6         
7
1  -897.75585 -412.69207   0.6328851    4.276630 -55.581006 -32.54413 
-84.15410
2 -1126.80146 -177.45009 -96.0334938   -4.558513 -48.247435 -78.89057  
29.46455
3  -402.33108 -101.71760  48.3551429 -149.761948  21.155187  50.29719  
50.74081
4   -88.04207   19.12321 -25.5603718   55.228526  81.502399  49.54745   
0.00000
5   219.95450   65.39756 -28.5352641   39.075518  -8.099494   0.00000   
0.00000
6   427.28240  142.97238   1.0954190   61.520149   0.000000   0.00000   
0.00000
7   669.78983  217.68299  98.3532236    0.000000   0.000000   0.00000   
0.00000
8   850.60581  322.69272   0.0000000    0.000000   0.000000   0.00000   
0.00000
9  1194.20921    0.00000   0.0000000    0.000000   0.000000   0.00000   
0.00000
      8     9
1 -63.06562 0
2  63.88373 0
3   0.00000 0
4   0.00000 0
5   0.00000 0
6   0.00000 0
7   0.00000 0
8   0.00000 0
9   0.00000 0

1 个答案:

答案 0 :(得分:2)

你在寻找像geom_labels这样的东西吗?

x <- c(1,3,4,6,9)
y <- x^2
coords = paste(x,y,sep=",")

df = data.frame(x,y)

ggplot(df,aes(x,y))+geom_point(col="blue")+
  geom_label(aes(x+.5,y+0.5,label=coords))

enter image description here