r - ggplot2 - 突出显示选定的点和奇怪的行为 [英] r - ggplot2 - highlighting selected points and strange behavior

查看:843
本文介绍了r - ggplot2 - 突出显示选定的点和奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想突出显示选定的点并遇到一些奇怪的行为。首先一些虚拟数据:

  a < -  1:50 
b < - rnorm(50)
mydata < - data.frame(a = a,b = b)
ggplot(mydata,aes(x = a,y = b))+ geom_point()

这个工作正常。现在,为了突出一些要点,我添加了另一个 geom_point 图层:

  ggplot(mydata [20:40,],aes(x = a,y = b))+ 
geom_point()+
geom_point(aes(x = a [c(10,12,13) ],y = b [c(10,12,13)]),color =red)



<请注意,我只显示有限范围的数据( [20:40] )。现在出现了奇怪的现象:

$ $ $ $ $ $ $ $ $ $ $ $ $ ggplot(mydata [10:40,],aes(x = a,y = b) )+
geom_point()+
geom_point(aes(x = a [c(10,12,13)],y = b [c(10,12,13)]),color =红色)

更改所选范围的大小,我收到一个错误,粗略翻译成德文: 错误...:意味着不同行数的参数。奇怪的是,这与选定的范围有所不同。 [23:40] 会起作用, [22:40] 不会。






英文错误是:

 错误在data.frame中(x = c(19L,21L,22L),y = c(0.28198,-0.6215,:
参数意味着行数不同:3,31


解决方案

如果您的数据在不同图层之间不同,那么您需要为每个图层指定新数据。

您可以通过 data = ... 参数为每个 geom code>需要不同的数据:

$ p $ set.seed(1)
mydata< - data。框架(a = 1:50,b = rnorm(50))
ggplot(mydata,aes(x = a,y = b))+
geom_point(color =blue)+
geom_point(data = mydata [10:13,],aes(x = a,y = b),color =red,size = 5)


I want to highlight selected points and encountered some strange behaviour. First some dummy data:

a <- 1:50
b <- rnorm(50)
mydata <- data.frame(a=a,b=b)
ggplot(mydata,aes(x=a,y=b)) + geom_point()

This works correctly. Now,to highlight some points, I add another geom_point layer:

ggplot(mydata[20:40,],aes(x=a,y=b)) + 
    geom_point() + 
    geom_point(aes(x=a[c(10,12,13)],y=b[c(10,12,13)]),colour="red")

Note that I am displaying only a limited range of the data ([20:40]). Now comes the strange behavior:

ggplot(mydata[10:40,],aes(x=a,y=b)) + 
    geom_point() + 
    geom_point(aes(x=a[c(10,12,13)],y=b[c(10,12,13)]),colour="red")

Changing the size of the selected range, I get an error, roughly translated from German: Error...: Arguments implying different number of rows. Strangely, this varies with the selected range. [23:40] will work, [22:40] won't.


The error in English is:

Error in data.frame(x = c(19L, 21L, 22L), y = c(0.28198, -0.6215,  : 
  arguments imply differing number of rows: 3, 31

解决方案

If your data is different between different layers, then you need to specify the new data for each layer.

You do this with the data=... argument for each geom that needs different data:

set.seed(1)
mydata <- data.frame(a=1:50, b=rnorm(50))
ggplot(mydata,aes(x=a,y=b)) + 
  geom_point(colour="blue") +
  geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5)

这篇关于r - ggplot2 - 突出显示选定的点和奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆