我有一個散點圖:
ex<- ggplot(rdr, aes(x = hb, y = iv, color = pt)) geom_point()
ylim(-20,20)
xlim(-20,20)
annotate(geom = "rect", xmin = -10, xmax = 10, ymin = -10, ymax = 10,
fill = "gray100", colour = "black", alpha = 0.5)
annotate( geom = "rect", xmin = -10, xmax = 10, ymin = -15, ymax = -10, fill = "palegreen", color = "blue", alpha =0.5)
我想計算每個矩形中有多少個點。根據點的顏色,我想給它們賦值。例如:藍色(0),綠色(1)。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
如果您事先知道每個變數的范圍(這似乎是基于您擁有的xmin/ yminargs 所做的),那么我會先做注釋。
您沒有散點圖,您有以散點圖表示的資料,并且您想要注釋這些點,以便它們在散點圖上顯示為不同的顏色。
library(dplyr)
library(ggplot2)
data(mtcars)
mtcars2 = mtcars %>%
mutate(good = case_when(
# this is the annotation step
between(hp, 100, 150) & between(mpg, 15, 20) ~ 1,
# this says everything else gets this value
TRUE ~ 0
))
# we need as.factor to get different colors displayed
ggplot(mtcars2, aes(x = mpg, y = hp, color = as.factor(good))) geom_point()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452356.html
上一篇:R預測建議
下一篇:r中ggplot2的主題
