我使用ggplot2創建了以下示例圖。
繪圖區域被劃分為灰線之間的各種區域(SD、TC、CC、CCS)。我知道這些灰線中每一條的方程式。我也繪制了我的資料,我想做的是找到每個資料點所繪制的區域,這樣我就可以把它們分成不同的類別,例如為每個區域賦予不同的顏色。有沒有人有什么想法?非常感謝。
EDIT 我添加了代碼來重現一個簡單的例子。
# load libraries
library(tidyverse)
# sample data
sample_data<-
tibble()
x = c(8。 8, 8。 8, 8。 0),
y = c(5。 10。 20, 50。 100, 100)
)
#磁區名稱
zoning_data <-
tibble()
x = c()
18,
18,
0,
18
),
y = c()
70,
25,
600,
600>
),
標簽 = c()
"CC"/span>,
"CCS"/span>,
"SD"/span>,
"TC"
)
)
# plot
ggplot()
data = sample_data,/span>
aes()
x,
y
)
)
# point
geom_point()
scale_x_continuous()
展開 = c(0。 0),
minor_breaks = NULL,
breaks = c()
seq(-2。 20, 由= 2)
)
)
scale_y_continuous()
trans = "log10",
擴展 = c(0。 0),
minor = c()
seq(1, 10。 通過= 1),
seq(10, 100。 通過= 10),
seq(100, 1000。 通過= 100)
)
)
coord_cartesian()
xlim = c(-2。 20),
ylim = c(1。 1000),
擴展 = c(0。 0)
)
# 磁區線的方程
geom_function()
fun = function( x) ((x - 1. 05) * 5)^1. 0526,
xlim = c(1. 31, 20)
)
geom_function()
fun = function(x) ((x - 1. 1) * 1. 5)^1.0989,
xlim = c(2. 82, 20)
)
geom_function()
fun = function(x) x,
xlim = c(-2。 20)
)
#磁區名稱
geom_text()
data = zoning_data,
aes()
x = x,
y = y,
標簽 = 標簽
)
)
# theme
主題(aspect.ratio = 1)
uj5u.com熱心網友回復:
如果沒有一個最小可復制的例子,就很難回答你的問題,但也許你可以根據你的資料集來調整這個方法:
library(tidyverse)
# 使用 "鉆石 "示例資料集
data(diamonds)
鉆石%>%
ggplot(aes(x = carat。 y = 價格))
geom_point()
coord_cartesian(ylim = c(0。 20000))

# example equations:
# price = 7000 * carat - 3000
# price = 7050 * carat - 1750
# 創建一個新變數("zone")
# 并對每個資料點進行分類。
# using mutate and case_when
鉆石%>%
mutate(zone = case_when(price < = 7000 * carat - 3000 ~ "zone 1"。
價格 > 7000 * 卡 - 3000 &。 價格 < 7050 * carat - 1750 ~ "zone 2"。
價格 > = 7050 * carat - 1750 ~ "zone 3") %> %
ggplot(aes(x = carat。 y = 價格))
geom_point(aes(colour=) 運算子">=zone))
coord_cartesian(ylim = c(0。 20000))

創建于2021-09-15,由reprex包(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318908.html
標籤:
下一篇:如何將y軸標簽改為10^n
