Plotting multiple density distributions on one plot

江枫思渺然 提交于 2020-12-06 12:14:14

问题


For teaching purposes I'm looking to create and plot multiple distributions on to one graph. The code I've been using to do this is:

library(ggplot2)
library(ggfortify)

# Create an initial graph with 1 distribution
p3 <- ggdistribution(dnorm,
                 seq(-5, 10,length=1000), 
                 colour='blue', 
                 mean=0.15,
                 sd=0.24,
                 fill='blue')

# Update p3 with second distribution
p3 <- ggdistribution(dnorm, seq(-5, 10,length=1000), 
      mean = 1.11, 
      sd = 0.55, 
      colour='green',
      fill='green',p=p3)

# View p3
p3

Initially, this seems great because it produces a graph with both distributions:

Two distributions on one graph

The problems start when I try to change the appearance of the graph.

(1) First when I attempt to change the y-axis scale so that it ranges from 0 to 1 instead of the percentages it shows by default, I am able to do so, but something happens to the distributions. Here is the code I am using:

p3 <- p3 + ylim(0,1) + xlim (-2, 6) + labs(title="Plotting Multiple Distributions",  x="Mean difference", y="Density")

And this returns the following graph:

enter image description here

Any advice on how I can change the y-axis without ruining the distribution would be very appreciated!

(2) Second, when I try to add 2 lines along the axes using this code:

p3 <- p3 + geom_segment(aes(x=0, y=0, xend=0, yend=0.98),
                    size=1,       
                    arrow = arrow(length = unit(0.4,"cm")))
p3 <- p3 + geom_segment(aes(x=-2, y=0, xend=6, yend=0), 
                    size=1)

...R returns the following error message:

Error in eval(expr, envir, enclos) : object 'ymin' not found

Any advice as to how I might add these lines to improve the aesthetics of the graph would be very appreciated.

Thank you in advance for your time.


回答1:


Sounds like you wish to change the y-axis labels to the range (0, 1), without actually changing the underlying distribution. Here's one approach:

# after obtaining p3 from the two ggdistribution() functions

# get the upper limit for p3's current y-axis range, rounded up
y.orig <- layer_scales(p3)$y$range$range[2] # = 1.662259 in my case, yours may 
                                            # differ based on the distribution
y.orig <- ceiling(y.orig * 10) / 10         # = 1.7

p3 + 
  xlim(-2, 6) +
  scale_y_continuous(breaks = seq(0, y.orig, length.out = 5),
                     labels = scales::percent(seq(0, 1, length.out = 5))) +
  annotate("segment", x = 0, y = 0, xend = 0, yend = y.orig, 
           size = 1, arrow = arrow(length = unit(0.4, "cm"))) +
  annotate("segment", x = -2, y = 0, xend = 6, yend = 0, 
           size = 1)

Or if you prefer to keep labels close to the fake axis created from line segments, include expand = c(0, 0) for x / y:

p3 + 
  scale_x_continuous(limits = c(-2, 6), expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(0, y.orig, length.out = 5),
                     labels = scales::percent(seq(0, 1, length.out = 5)),
                     expand = c(0, 0)) +
  annotate("segment", x = 0, y = 0, xend = 0, yend = y.orig, 
           size = 1, arrow = arrow(length = unit(0.4, "cm"))) +
  annotate("segment", x = -2, y = 0, xend = 6, yend = 0, 
           size = 1)



来源:https://stackoverflow.com/questions/47025907/plotting-multiple-density-distributions-on-one-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!