Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a graph for a given function in R

Suppose there is this function sqrt(x^2)+0.9*sqrt(3.3-x^2)*sin(30*pi*x) This function generate plot in the shape of a heart

enter image description here

Is there the way using ggplot2 reproduce this function to get a red heart as output?
Thanks you for your help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You will get better resolution if you directly calculate along a predefined sequence. Also, the formula is not quite right, so I have amended it:

heart <- function(x) abs(x)^(2/3) + 0.9 * sqrt(3.3 - x^2) * sin(18 * pi * x)

df <- data.frame(x = seq(-sqrt(3.3), sqrt(3.3), length = 3000),
                 y = heart(seq(-sqrt(3.3), sqrt(3.3), length = 3000)))


ggplot(df, aes(x, y)) + 
  geom_line(size = 1, colour = "red") +
  scale_x_continuous(limits = c(-2, 2)) +
  coord_equal() +
  theme_void() +
  theme(plot.background = element_rect(fill = "#400000"))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading