[R] Sin curve question

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Jul 24 21:49:18 CEST 2021


Hello,

You can use stat_function, it will take care of all the details, all you 
have to do is to pass xlim.


library(ggplot2)
library(cowplot)

ggplot() +
   stat_function(fun = sin, xlim = c(0, pi)) +
   xlab("x") +
   ylab("sin(x)") +
   scale_x_continuous(breaks = seq(0, pi, pi/6), labels = seq(0, 180, 30)) +
   ggtitle("sin(x) vs x", subtitle = "x is in degrees") +
   theme_cowplot()


Hope this helps,

Rui Barradas

Às 19:41 de 24/07/21, Thomas Subia via R-help escreveu:
> Colleagues,
> 
> Here is my code which plots sin(x) vs x, for angles between 0 and 180
> degrees.
> 
> library(ggplot2)
> library(REdaS)
> copdat$degrees <- c(0,45,90,135,180)
> copdat$radians <- deg2rad(copdat$degrees)
> copdat$sin_x <- sin(copdat$radians)
> 
> ggplot(copdat,aes(x=degrees,y=sin_x))+
>    geom_point(size = 2)+ geom_line()+
>    theme_cowplot()+xlab("x")+
>    ylab("sin(x)")+
>    scale_x_continuous(breaks=seq(0,180,30))+
>    ggtitle("sin(x) vs x\nx is in degrees")
> 
> My trig students would prefer a curved line plot similar to what can be
> plotted with Excel smooth line functionality.
> I wanted to provide a relatively simple R script using ggplot to do this
> without having to resort to fitting a sine curve to these points.
> 
> Some guidance would be appreciated.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list