改变ggplot / R中每个面的轴标签格式化程序 [英] Varying axis labels formatter per facet in ggplot/R

查看:110
本文介绍了改变ggplot / R中每个面的轴标签格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧随着时间的推移捕获了几个衡量标准,我想要将一个3x1方面可视化。然而,每个度量都包含不同的单位/尺度,这些单位/尺度会受益于自定义转换和标签方案。



所以,我的问题是:如果单位和比例不同在不同的方面,我怎样才能在一个方面指定一个自定义的格式或转换(即log10)到特定的轴?

例如,让我们假设我有这样的数据:

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

如何建立一个2x1方面显示美元并用 labels = dollars 和<$计算索引c $ c> scale_y_continuous(trans =log10,...)用于 df $美元数据?



谢谢!

解决方案

正如您发现的那样,但它出现了很多。由于这类事情经常被问到,所以我发现解释为什么这很难,并且提出了一个潜在的解决方案。



我的经验是,来到 ggplot2 点阵图形的人从根本上误解了面(或格子点阵)的目的。这个功能是基于一个非常具体的想法而开发的:跨多个共享公共比例的组的数据可视化。它来自于一些被称为小倍数的原则,由Tufte和其他人所支持。

将面板放置在彼此相邻且尺寸非常不同的地方是视觉设计专家倾向于避免,因为它可能充其量是误导。 (我不是在这里骂你,只是解释理由......)

但是当然,一旦你公开了这个伟大的工具,你永远不会知道人们如何使用它。所以它会变得很紧张:请求的功能是允许按比例变化比例,并为每个面板分别设置曲线的各个方面。在 ggplot2 中这样的表现已经超出了它原来的意图。



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



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



这里的技巧是认识到你不是绘制共享比例的图 。对我而言,这意味着你甚至不应该考虑使用切面。相反,分别制作每个图,并将它们排列在一个图中:

  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)



我刚刚猜对了你想要使用的 geom _ * ,我确定这不是你想要的以绘制,但至少它说明了原则。


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.

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?

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?

Thank you!

解决方案

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.

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.

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)

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.

这篇关于改变ggplot / R中每个面的轴标签格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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