在ggplot / R中改变每个面的轴标签格式器

时间:2021-12-08 20:11:50

I have a dataframe capturing several measures over time that I would like to visualize a 3x1 facet. However, each measure contains different units/scales that would benefit from custom transformations and labeling schemes.

我有一个数据框,随着时间的推移捕获了几个我希望可视化3x1方面的措施。但是,每个度量包含不同的单位/比例,可以从自定义转换和标签方案中受益。

So, my question is: If the units and scales are different across different facets, how can I specify a custom formatter or transformation (i.e., log10) to a particular axis within a facet?

所以,我的问题是:如果不同方面的单位和比例不同,我如何指定自定义格式器或转换(即log10)到构面内的特定轴?

For example, let's say I have the data:

例如,假设我有数据:

df = data.frame(dollars=10^rlnorm(50,0,1), counts=rpois(50, 100))
melted.df = melt(df, measure.var=c("dollars", "counts"))

How would one go upon setting up a 2x1 facet showing dollars and counts over the index with labels=dollars and scale_y_continuous(trans = "log10", ...) for the df$dollars data?

如何设置一个显示美元的2x1方面,并使用labels = dollar和scale_y_continuous(trans =“log10”,...)计算df $ dollar数据?

Thank you!

谢谢!

1 个解决方案

#1


41  

As you discovered, there isn't an easy solution to this, but it comes up a lot. Since this sort of thing is asked so often, I find it helpful to explain why this is hard, and suggest a potential solution.

正如您所发现的那样,没有一个简单的解决方案,但它出现了很多。由于这种事情经常被问到,我发现解释为什么这很困难很有帮助,并建议一个潜在的解决方案。

My experience has been that people coming to ggplot2 or lattice graphics fundamentally misunderstand the purpose of faceting (or trellising, in lattice). This feature was developed with a very specific idea in mind: the visualization of data across multiple groups that share a common scale. It comes from something called the principle of small multiples, espoused by Tufte and others.

我的经验是,人们来ggplot2或格子图形从根本上误解了刻面(或格子化格子)的目的。此功能的开发考虑了一个非常具体的想法:跨多个组共享共同规模的数据可视化。它来自Tufte和其他人所支持的小倍数原理。

Placing panels next to each other with very different scales is something that visual design experts will tend to avoid, because it can be at best misleading. (I'm not scolding you here, just explaining the rationale...)

将面板放在彼此相邻的不同尺度上是视觉设计专家倾向于避免的,因为它最多可能会产生误导。 (我不是在这里骂你,只是解释理由......)

But of course, once you have this great tool out in the open, you never know how folks are going to use it. So it gets stretched: the requests come in for the ability to allows the scales to vary by panel, and to set various aspects of the plot separately for each panel. And so faceting in ggplot2 has been expanded well beyond its original intent.

但是,当然,一旦你拥有这个伟大的工具,你永远不会知道人们将如何使用它。因此它会被拉伸:请求允许按面板改变比例,并为每个面板分别设置图的各个方面。因此,ggplot2中的分面已经超出了原来的意图。

One consequence of this is that some things are difficult to implement simply due to the original design intent of the feature. This is likely one such instance.

这样做的一个结果是,由于该特征的原始设计意图,有些事情很难实现。这可能是一个这样的例子。

Ok, enough explanation. Here's my solution.

好的,足够的解释。这是我的解决方案。

The trick here is to recognize that you aren't plotting graphs that share a scale. To me, that means you shouldn't even be thinking of using faceting at all. Instead, make each plot separately, and arrange them together in one plot:

这里的诀窍是要认识到你没有绘制共享比例的图形。对我来说,这意味着你根本不应该考虑使用刻面。相反,分别制作每个图,并将它们排列在一个图中:

library(gridExtra)

p1 <- ggplot(subset(melted.df,variable == 'dollars'),
                aes(x = value)) + 
            facet_wrap(~variable) + 
            geom_density() + 
            scale_x_log10(labels = dollar_format())

p2 <- ggplot(subset(melted.df,variable == 'counts'),
                aes(x = value)) + 
            facet_wrap(~variable) + 
            geom_density()

grid.arrange(p1,p2)

在ggplot / R中改变每个面的轴标签格式器

I've just guessed at what geom_* you wanted to use, and I'm sure this isn't really what you wanted to plot, but at least it illustrates the principle.

我猜想你想要使用什么geom_ *,我确信这不是你想要绘制的,但至少它说明了原理。

#1


41  

As you discovered, there isn't an easy solution to this, but it comes up a lot. Since this sort of thing is asked so often, I find it helpful to explain why this is hard, and suggest a potential solution.

正如您所发现的那样,没有一个简单的解决方案,但它出现了很多。由于这种事情经常被问到,我发现解释为什么这很困难很有帮助,并建议一个潜在的解决方案。

My experience has been that people coming to ggplot2 or lattice graphics fundamentally misunderstand the purpose of faceting (or trellising, in lattice). This feature was developed with a very specific idea in mind: the visualization of data across multiple groups that share a common scale. It comes from something called the principle of small multiples, espoused by Tufte and others.

我的经验是,人们来ggplot2或格子图形从根本上误解了刻面(或格子化格子)的目的。此功能的开发考虑了一个非常具体的想法:跨多个组共享共同规模的数据可视化。它来自Tufte和其他人所支持的小倍数原理。

Placing panels next to each other with very different scales is something that visual design experts will tend to avoid, because it can be at best misleading. (I'm not scolding you here, just explaining the rationale...)

将面板放在彼此相邻的不同尺度上是视觉设计专家倾向于避免的,因为它最多可能会产生误导。 (我不是在这里骂你,只是解释理由......)

But of course, once you have this great tool out in the open, you never know how folks are going to use it. So it gets stretched: the requests come in for the ability to allows the scales to vary by panel, and to set various aspects of the plot separately for each panel. And so faceting in ggplot2 has been expanded well beyond its original intent.

但是,当然,一旦你拥有这个伟大的工具,你永远不会知道人们将如何使用它。因此它会被拉伸:请求允许按面板改变比例,并为每个面板分别设置图的各个方面。因此,ggplot2中的分面已经超出了原来的意图。

One consequence of this is that some things are difficult to implement simply due to the original design intent of the feature. This is likely one such instance.

这样做的一个结果是,由于该特征的原始设计意图,有些事情很难实现。这可能是一个这样的例子。

Ok, enough explanation. Here's my solution.

好的,足够的解释。这是我的解决方案。

The trick here is to recognize that you aren't plotting graphs that share a scale. To me, that means you shouldn't even be thinking of using faceting at all. Instead, make each plot separately, and arrange them together in one plot:

这里的诀窍是要认识到你没有绘制共享比例的图形。对我来说,这意味着你根本不应该考虑使用刻面。相反,分别制作每个图,并将它们排列在一个图中:

library(gridExtra)

p1 <- ggplot(subset(melted.df,variable == 'dollars'),
                aes(x = value)) + 
            facet_wrap(~variable) + 
            geom_density() + 
            scale_x_log10(labels = dollar_format())

p2 <- ggplot(subset(melted.df,variable == 'counts'),
                aes(x = value)) + 
            facet_wrap(~variable) + 
            geom_density()

grid.arrange(p1,p2)

在ggplot / R中改变每个面的轴标签格式器

I've just guessed at what geom_* you wanted to use, and I'm sure this isn't really what you wanted to plot, but at least it illustrates the principle.

我猜想你想要使用什么geom_ *,我确信这不是你想要绘制的,但至少它说明了原理。