在一个图上绘制多个密度分布 [英] Plotting multiple density distributions on one plot

查看:67
本文介绍了在一个图上绘制多个密度分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于教学目的,我希望在一个图形上创建并绘制多个分布.我一直使用的代码是:

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)首先,当我尝试将y轴比例更改为从0到1而不是默认显示的百分比时,我可以这样做,但是发生在分布上.这是我正在使用的代码:

(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")

这将返回以下图形:

任何有关如何更改y轴而不破坏分布的建议,将不胜感激!

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

(2)其次,当我尝试使用此代码沿轴添加2条线时:

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

...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.

提前感谢您的时间.

推荐答案

像您希望将y轴标签更改为范围(0,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)

或者,如果您希望将标签保持在由线段创建的伪轴附近,请为x/y添加 expand = c(0,0):

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)

这篇关于在一个图上绘制多个密度分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆