0

I want to plot a Laplacian density function with ggplot2. I wrote the following code:

library(ggplot2)
plot <- ggplot() + 
  geom_function(fun = function(x) rmutil::dlaplace(x, m = 0, s = beta_true)) + 
  expand_limits(x=c(-10,10)) +
  geom_function(fun = function(x) rmutil::dlaplace(x, m = 0, s = beta_fake)) + 
  expand_limits(x=c(-10,10)) +
  xlab("Noise Range") +
  ylab("Probability")
  

This is what I got:

enter image description here

I want to give the two curves different colours, one pink another one purple. I also want to add a legend to indicate the colours of the corresponding curves.

I have also tried this:

library(ggplot2)
plot <- ggplot() + 
  geom_function(fun = function(x) rmutil::dlaplace(x, m = 0, s = beta_true), aes(color='beta_true')) + 
  expand_limits(x=c(-10,10)) +
  geom_function(fun = function(x) rmutil::dlaplace(x, m = 0, s = beta_fake), aes(color='beta_fake')) + 
  expand_limits(x=c(-10,10)) +
  xlab("Noise Range") +
  ylab("Probability") +
  scale_color_manual(name='Functions',values = c(beta_true='pink',beta_fake='purple'))
  • what is `beta_true` and `beta_fake` ? can you attach them ? – Samet Sökel Sep 09 '21 at 17:40
  • They are the scale parameters of the curves. Each curve has a unique scale parameter. They are just a value. – SssssssAaaaaaa Sep 09 '21 at 17:43
  • I will add a solution using function `sin` and `cos` , so you can apply for yours – Samet Sökel Sep 09 '21 at 17:43
  • Also see https://stackoverflow.com/q/65190074/5325862, https://stackoverflow.com/q/47791400/5325862, https://stackoverflow.com/q/54864805/5325862, https://stackoverflow.com/q/56560466/5325862, https://stackoverflow.com/q/61899270/5325862 – camille Sep 09 '21 at 18:14

1 Answers1

0

I don't have the package rmutil thus I couldn't draw your function; you can easily apply this method for yours;

library(ggplot2)
plot <- ggplot() + 
  geom_function(fun = function(x) sin(x),aes(color='Sinus')) + 
  expand_limits(x=c(-10,10)) +
  geom_function(fun = function(x) cos(x),aes(color='Cosinus')) + 
  expand_limits(x=c(-10,10)) +
  xlab("Noise Range") +
  ylab("Probability") +
  scale_color_manual(name='Functions',values = c(Sinus='pink',Cosinus='purple'))

fields; enter image description here

Samet Sökel
  • 2,515
  • 6
  • 21
  • I used your code excpet for lines 3, 5 and 9, where I have changed `Sinus` and `Cosinus` to`beta_true` and `beta_fake`. So for example, my line 3 looks like this: `geom_function(fun = function(x) rmutil::dlaplace(x, m = 0, s = beta_true), aes(color='beta_true')) +`. But nothing appeared on my plot environment. – SssssssAaaaaaa Sep 09 '21 at 18:44
  • did you also change in `scale_color_manual` ? – Samet Sökel Sep 09 '21 at 18:47
  • Yes, I changed it to `scale_color_manual(name='Functions',values = c(beta_true='pink',beta_fake='purple'))` – SssssssAaaaaaa Sep 09 '21 at 18:49
  • probably there is a typo, can you attach what you tried at main question ? – Samet Sökel Sep 09 '21 at 18:51
  • 1
    there is already the same named objects in your R environment. update aes(color='beta_true2') etc. everywhere (I mean do it for beta_fake also and in scale_color_manual) . it will be fixed. – Samet Sökel Sep 09 '21 at 18:56