绘制一个x = 1,y = 1和R中的另一个函数

时间:2019-04-27 03:26:34

标签: r plot

我是R的新手,我正在尝试做看似最简单的事情,但是对于上帝的爱,我不知道该怎么做!

正如标题所述,我想绘制x = 1,y = 1和y = 1 /(2 * x),最好用不同的颜色绘制,然后,我要绘制x,y之间的区域轴和绘制的线。像这样:

预先感谢

1 个答案:

答案 0 :(得分:2)

有多种方法可以做到这一点。例如,使用library(ggplot2)您可以

# define how far beyond the intersection we calculate curve values
xmax = 1.1        
xmin = 1/(2*xmax)

# calculate coordinates of the curve
x = seq(xmin, xmax, length.out = 100)
y = 1/(2*x)

# create polygon coordinates that follow the curve and ...
# ...extend down the staight lines to infinity
poly = data.frame(
  x = c(x[x<1 & y<1], 1,   1,    -Inf, -Inf, 0.5), 
  y = c(y[x<1 & y<1], 0.5, -Inf, -Inf, 1, 1))

ggplot(data.frame(x,y), aes(x,y)) +
  geom_polygon(data = poly, fill='yellow') +
  geom_line() +
  geom_hline(aes(yintercept=1)) +
  geom_vline(aes(xintercept=1)) +
  coord_equal(1, c(0,1), c(0,1))

enter image description here