重叠并固定小平面(scale =“free”)以制作电影 [英] overlapping and fixing x-scales in facet (scale=“free”) in order to make a film

查看:194
本文介绍了重叠并固定小平面(scale =“free”)以制作电影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一篇文章中@Justin的帮助下,我用ggplot2中的 facet_grid 选项绘制了模拟结果。数据和答案的帖子在这里:使用facet_grid选项用ggplot2绘制数据框的列



这里是一个原始样本数据的副本,其中包含有关每种参数类型的scale_x最小值和最大值的附加信息:

  dat < -  read.table(textConnection(P1 P2 P3 P4 R 
1 2e-5 1.0 0.6 3 1
2 4e-6 1.5 0.7 1.5 2
3 6e-7 1.2 0.6 2.5 3
4 8e-8 1.45 0.65 3.2 4
))

scalx< - read.table(textConnection(P XMIN XMAX
1 1 10e-1 10e-10
2 2 0.5 3.0
3 3 0.0 1.5
4 4 2.0 5.0
))

@justin给出的代码用于绘制实际示例:

  library(ggplot2)
library(reshape2)
dat.melt< - melt(dat,id .vars ='R')

ggplot(dat.melt,aes(x = 0,y = value))+
geom_boxplot()+
geom_point(aes color = factor(R)))+
facet_wrap(〜variable,ncol = 1,scales ='free')+
coord_flip()




为了使这成为可能,我需要修正每个方面的值,因为每个参数都可以在固定的极值之间变化,尽管这些值是从facet到facet的变化。

你知道是否可以使用 facet_grid ,因为我最近发现这个问题涉及到在不同方面指定不同的限制在github ggplot2项目存储库上: https://github.com/hadley/ggplot2/issues/187 。是否有另一种替代解决方案使这成为可能?

解决方案

由于您想扩大每个方面的比例(并且不限制/合同他们),这是可能的。您需要将 scalx 重塑为类似于您融化的 dat 的格式:

  xlims < -  melt(scalx,id.vars =P)
xlims $ P < - paste0(P,xlims $ P )
名称(xlims)[1]< - variable

除了 melt ,我使facet具有相同的格式和变量名称。这个新的数据框然后可以被赋予一个 geom_blank 图层,这样它就会影响到这个尺度的限制,但实际上并没有绘制任何东西:

  ggplot(dat.melt,aes(x = 0,y = value))+ 
geom_boxplot()+
geom_point(aes color = factor(R)))+
geom_blank(data = xlims)+
facet_wrap(〜variable,ncol = 1,scales ='free')+
coord_flip()

P4 被列为至少有2个,但实际上有一个1.5的数据点。这表明,这种方法只会扩大规模,而不是缩小规模。


With the help of @Justin on another post, I plotted simulation result with the facet_grid option in ggplot2. The post with data and answer is here: Use facet_grid option to plot column of dataframe with ggplot2

Here a copy of original sample data with added information about scale_x min and max values for each parameter type :

dat <- read.table(textConnection("P1 P2 P3 P4 R
1 2e-5 1.0 0.6 3 1
2 4e-6 1.5 0.7 1.5 2
3 6e-7 1.2 0.6 2.5 3 
4 8e-8 1.45  0.65 3.2 4
"))

scalx <- read.table(textConnection("P XMIN XMAX
1 1 10e-1 10e-10
2 2 0.5 3.0
3 3 0.0 1.5
4 4 2.0 5.0
"))

The code given by @justin for actual example plotting:

library(ggplot2)
library(reshape2)
dat.melt <- melt(dat, id.vars='R')

ggplot(dat.melt, aes(x=0, y=value)) +
    geom_boxplot() +
    geom_point(aes(colour=factor(R))) +
    facet_wrap(~variable, ncol=1, scales='free') +
    coord_flip()

But now, my project is to make a little film to show evolution of multiple simulation result, at this time with the help of code given above I obtain great graphic like this.

To make this possible I need to fix the value for each facet, because each parameter can range between fixed extreme values, though what those values are changes from facet to facet.

Do you know if it's possible with facet_grid because I found recently this issue related to specifying different limits in different facets on the github ggplot2 project repository: https://github.com/hadley/ggplot2/issues/187. Is there another alternative solution to make this possible ?

解决方案

Since you are wanting to expand the scales in each facet (and not restrict/contract them), it is possible. You need to reshape your scalx into a format similar to your melted dat:

xlims <- melt(scalx, id.vars="P")
xlims$P <- paste0("P", xlims$P)
names(xlims)[1] <- "variable"

In addition to the melt, I made the facets have the same format and variable name. This new data frame can then be given to a geom_blank layer so that it affects the limits of the scales, but does not actually draw anything:

ggplot(dat.melt, aes(x=0, y=value)) +
    geom_boxplot() +
    geom_point(aes(colour=factor(R))) +
    geom_blank(data=xlims) +
    facet_wrap(~variable, ncol=1, scales='free') +
    coord_flip()

As a side note, your P4 is listed as having a minimum of 2, but actually has a data point at 1.5. This demonstrates that this approach will only expand the scales, not contract them.

这篇关于重叠并固定小平面(scale =“free”)以制作电影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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