r - ggplot2 - 突出显示选定的点和奇怪的行为
问题描述:
我想突出显示选定的点并遇到一些奇怪的行为。首先一些虚拟数据: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)]),colour="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)]),colour="red")
更改选定范围的大小,我得到一个错误,从德国大致翻译:Error...: Arguments implying different number of rows
。奇怪的是,这与选定的范围有所不同。 [23:40]
将工作,[22:40]
不会。
英语中的错误是:
Error in data.frame(x = c(19L, 21L, 22L), y = c(0.28198, -0.6215, :
arguments imply differing number of rows: 3, 31
答
如果你的数据是不同层之间的不同,那么你需要为每个层指定新的数据。
你这样做与data=...
论据需要不同的数据每个geom
:
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)
答
另一种选择加入的条件,这两个属性,颜色和大小,里面geom_point
。然后我们手动控制分别使用scale_colour_manual
和scale_size_manual
的那些。
set.seed(1)
mydata <- data.frame(a = 1:50, b = rnorm(50))
ggplot(mydata) +
geom_point(aes(x = a, y = b, colour = a > 9 & a < 14, size = a > 9 & a < 14)) +
scale_colour_manual(values = c("blue", "red")) +
scale_size_manual(values =c(1, 4))+
theme(legend.position = "none")
我希望你不介意,但我已在英国 – csgillespie 2012-07-13 09:54:03