geom_point如何从两个不同的数据集geom_text赋值

问题描述 投票:0回答:1

我是来自两个不同的数据在一个图表集剧情尝试点。我想补充的与geom_text点的“价值”,但是这是行不通的。

我不能告诉你的数据集,但两者的数据集P1和P2有3列这是数。

我想情节列第2列的3个功能(从蓝色红色点dataset2从数据集1点)。并加入柱1的标签

ggplot()+geom_point(size=8,col="red",aes(x=p2[,3],y=p2[,2]/sum(p2[,2])))+
   geom_text(label=p2[,1])+ylab("frequence")+
     geom_point(size=8,col="blue",aes(x=p1[,3],y=p1[,2]/sum(p1[,2])))+
    geom_text(label=p1[,1])

画出我我想要的respectivly蓝色和红色的点,但没有任何标签。

我可以从两个数据设置打印标签

ggplot(p2,aes(x=p2[,3],y=p2[,2]/sum(p2[,2])))+geom_text(label=p2[,1])+ylab("frequence")+ geom_point(size=8,col="red",alpha=0.2)+
geom_point(size=8,col="blue",alpha=0.2,aes(x=p1[,3],y=p1[,2]/sum(p1[,2])))+geom_text(label=p1[,1])


这里的问题是,这两个标签都在我的红点打印

谢谢你的时间

r ggplot2 label geom-text
1个回答
1
投票

当您正在使用数据集2,应指定在每个geom function正确的数据集,具有参数data =

library(ggplot)

p1 <- data.frame(c1 = rnorm(10), c2 = rnorm(10), c3 = rnorm(10))
p2 <- data.frame(c1 = rnorm(10), c2 = rnorm(10), c3 = rnorm(10))

ggplot() +
  geom_point(aes(x = c3, y = c2 / sum(c2)), col = "red", data = p2) +
  geom_text(aes(x = c3, y = c2 / sum(c2), label = round(c1, 2)), data = p2) +
  geom_point(aes(x = c3, y = c2 / sum(c2)), col = "blue", data = p1) +
  geom_text(aes(x = c3, y = c2 / sum(c2), label = round(c1, 2)), data = p1)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.