Skip to content

Sin curve question

8 messages · Thomas Subia, Sorkin, John, Eric Berger +4 more

#
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.
#
Try something like the following

copdat$degrees <- c(1:180)

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street<x-apple-data-detectors://12>
GRECC<x-apple-data-detectors://12> (BT/18/GR)
Baltimore, MD 21201-1524<x-apple-data-detectors://13/0>
(Phone) 410-605-711<tel:410-605-7119>9
(Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing)
On Jul 24, 2021, at 2:41 PM, Thomas Subia via R-help <r-help at r-project.org> wrote:
?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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=04%7C01%7CJSorkin%40som.umaryland.edu%7C1ffa4922f2ba41588da908d94ed2a982%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637627488997910453%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=90qApIoS6rwqkQuKPzy3x2AUPntuJ2W%2FtJgPGfiddEI%3D&amp;reserved=0
PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=04%7C01%7CJSorkin%40som.umaryland.edu%7C1ffa4922f2ba41588da908d94ed2a982%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637627488997910453%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=s9YIcjlEo4MvI6hcX%2FkV4gwJJKa172QrPEnHsqTiRa8%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.
#
Alternatively with base graphics

N <- 500 ## number of points (arbitrary)
degrees <- seq(from=0,to=180,length=N)
degreesToRadians <- function(d) { pi * d / 180.0}  ## vectorIzed!
plot(x=degrees,y=sin(degreesToRadians(degrees)),type='l',
     xlab="x",ylab="sin(x)",main="sin(x) vs x\nx is in degrees")


On Sat, Jul 24, 2021 at 9:52 PM Sorkin, John <jsorkin at som.umaryland.edu>
wrote:

  
  
#
plot(sin, to=pi) # also works but with x labeled in radians.


# With x axis labeled in degrees
plot(sin, to=pi, axes=FALSE)
axis(2)
lbls <- seq(0, 180, 30)
axis(1, pi*lbls/180, lbls)


	  This can probably be done in ggplot2, but I don't know how off the 
top of my head.


	  Hope this helps.
	  Spencer
On 7/24/21 2:04 PM, Eric Berger wrote:
#
This is not excel-help, but to the best of my knowledge Excel interpolates with splines when you select curved point interpolation.

R, being primarily a science tool rather than a business tool, assumes you want to be precise about how new points are to be interpolated between original data points. Thus, the task of building the spline and deciding how close the points in that interpolation need to be is typically left to the user. If the user knows that the points are part of a periodic continuous function and extrapolation is desired, then choosing a fourier series may be a better choice than a spline. But the data you have provided is insufficient to support a fourier analysis, so there is some sophisticated math in between you and your goal unless you build in heuristics about how to interpolate within these points, or just live with whatever a spline function will give you... but the choice is yours to make.
On July 24, 2021 11:41:05 AM PDT, Thomas Subia via R-help <r-help at r-project.org> wrote:

  
    
#
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:
1 day later
#
plot(function(x)sin(deg2rad(x)),0,180)
#
With ggplot

tibble(x=0:180,y=sin(deg2rad(x))) |>
ggplot(aes(x=x,y=y)) +
  geom_line() +
  scale_x_continuous(breaks=seq(0,180,30)) +
    labs(x="x",y="sin(x)",
         title="sin(x) vs x\nx is in degrees")