如何用R绘制椭圆
用plot函数用ggplot函数根据函数式的基本绘图。直角坐标系下可使用参数方程: \text{根据函数式的基本绘图。直角坐标系下可使用参数方程:} 根据函数式的基本绘图。直角坐标系下可使用参数方程:
x 2 a 2 + y 2 b 2 = 1 → x = a sin θ , y = b cos θ , 0 < θ < 2 π \frac{x^2}{a^2}+\frac{y^2}{b^2}=1\xrightarrow{}x=\mathrm{a}\sin \theta ,y=b\cos \theta ,0<\theta <2\pi a2x2+b2y2=1 x=asinθ,y=bcosθ,0<θ<2π
用plot函数
t=seq(0,2*pi,length=100)x=sin(t)#a=1y=2*cos(t)plot(x,y,type="l",xlab="")#添加x=0和y=0两条线abline(h=0,v=0, col = "lightgray", lty = 3)#添加主标题title(main=expression(frac(x^2,a^2)+frac(y^2,b^2)==1))
用ggplot函数
区别就是我们要使用一个数据框格式的数据
library(ggplot2)t=seq(0,2*pi,length=1000)x=sin(t)#a=1y=2*cos(t)xy <- data.frame(x,y)ggplot(xy,aes(x,y))+geom_point(size=0.5,alpha=1/5)+labs(title=expression(frac(x^2,a^2)+frac(y^2,b^2)==1))+theme(plot.title=element_text(size=12,hjust=0.5))#标题居中