R 在ggplot中的散点图上定义两点之间的直线
如果此问题重复,请道歉;查找堆栈溢出上的“在两点之间画一条线”给了我一些想法,但我不确定如何将它们应用到我的特定问题上 假设我的数据的形状为:R 在ggplot中的散点图上定义两点之间的直线,r,ggplot2,R,Ggplot2,如果此问题重复,请道歉;查找堆栈溢出上的“在两点之间画一条线”给了我一些想法,但我不确定如何将它们应用到我的特定问题上 假设我的数据的形状为: x <- runif(n = 10) y <- runif(n = 10) graphData <- data.frame(x, y) graphData x y 1 0.3328235 0.30122890 2 0.4886130 0.06072057 3 0.9544738 0.947
x <- runif(n = 10)
y <- runif(n = 10)
graphData <- data.frame(x, y)
graphData
x y
1 0.3328235 0.30122890
2 0.4886130 0.06072057
3 0.9544738 0.94772694
4 0.4829024 0.72059627
5 0.8903502 0.14229430
6 0.9144382 0.54928466
7 0.6087350 0.95409124
8 0.4106898 0.58548335
9 0.1470947 0.40451028
10 0.9352998 0.64789348
x我会尝试:
p <- ggplot(graphData, aes(x = x, y = y)) +
geom_point()+
geom_smooth(data = . %>% filter(x == max(x) | y == max(y)), method = lm)
p
我会尝试:
p <- ggplot(graphData, aes(x = x, y = y)) +
geom_point()+
geom_smooth(data = . %>% filter(x == max(x) | y == max(y)), method = lm)
p
我将过滤数据,使其仅包含这两点,然后将其用作data
参数,以geom\u path
。这样,您也可以使用它做任何您需要的事情,作为一个公式。我将过滤数据,使其仅包含这两点,然后将其用作data
的geom_path
参数。这样,你也可以做任何你需要它作为一个公式
lm(y ~ x, data = graphData %>% filter(x == max(x) | y == max(y)))