简体   繁体   English

在一个图上绘制多个密度分布

[英]Plotting multiple density distributions on one plot

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: 最初,这看起来很棒,因为它会生成具有两种分布的图:

一张图上的两种分布

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. (1)首先,当我尝试将y轴刻度更改为从0到1而不是默认情况下显示的百分比时,我能够这样做,但是分布发生了变化。 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: 这将返回以下图形:

在此处输入图片说明

Any advice on how I can change the y-axis without ruining the distribution would be very appreciated! 我将如何在不破坏分布的情况下更改y轴的任何建议将不胜感激!

(2) Second, when I try to add 2 lines along the axes using this code: (2)其次,当我尝试使用此代码沿轴添加2条线时:

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: ... R返回以下错误消息:

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. 预先感谢您的宝贵时间。

Sounds like you wish to change the y-axis labels to the range (0, 1), without actually changing the underlying distribution. 听起来好像您希望将y轴标签更改为范围(0,1),而无需实际更改基础分布。 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)

情节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: 或者,如果您希望使标签保持靠近由线段创建的伪轴expand = c(0, 0)为x / y包含expand = c(0, 0)

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)

情节2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM