[英]Ordering the axis labels in geom_tile
I have a data frame containing order data for each of 20+ products from each of 20+ countries.我有一个数据框,其中包含来自 20 多个国家/地区的 20 多种产品中每一种的订单数据。 I have put it in a highlight table using
ggplot2
with code similar to this:我已经使用
ggplot2
将它放在一个高亮表中,代码与此类似:
require(ggplot2)
require(reshape)
require(scales)
mydf <- data.frame(industry = c('all industries','steel','cars'),
'all regions' = c(250,150,100), americas = c(150,90,60),
europe = c(150,60,40), check.names = FALSE)
mydf
mymelt <- melt(mydf, id.var = c('industry'))
mymelt
ggplot(mymelt, aes(x = industry, y = variable, fill = value)) +
geom_tile() + geom_text(aes(fill = mymelt$value, label = mymelt$value))
Which produces a plot like this:产生这样的情节:
In the real plot, the 450 cell table very nicely shows the 'hotspots' where orders are concentrated.在真实图中,450 个单元格的表格很好地显示了订单集中的“热点”。 The last refinement I want to implement is to arrange the items on both the x-axis and y-axis in alphabetical order.
我要实现的最后一个改进是按字母顺序排列 x 轴和 y 轴上的项目。 So in the plot above, the y-axis (
variable
) would be ordered as all regions
, americas
, then europe
and the x-axis ( industry
) would be ordered all industries
, cars
and steel
.因此,在上图中,y 轴(
variable
)将按all regions
排序, americas
,然后是europe
,x 轴( industry
)将按all industries
、 cars
和steel
排序。 In fact the x-axis is already ordered alphabetically, but I wouldn't know how to achieve that if it were not already the case.事实上,x 轴已经按字母顺序排列,但如果不是这样,我不知道如何实现。
I feel somewhat embarrassed about having to ask this question as I know there are many similar on SO, but sorting and ordering in R remains my personal bugbear and I cannot get this to work.我对不得不问这个问题感到有些尴尬,因为我知道 SO 上有很多类似的问题,但是 R 中的排序和排序仍然是我个人的烦恼,我无法让它工作。 Although I do try, in all except the simplest cases I got lost in a welter of calls to
factor
, levels
, sort
, order
and with
.尽管我确实尝试过,但除了最简单的情况外,我都迷失在对
factor
、 levels
、 sort
、 order
和with
的大量调用中。
Q. How can I arrange the above highlight table so that both y-axis and x-axis are ordered alphabetically?问:如何排列上面的突出显示表,使 y 轴和 x 轴都按字母顺序排列?
EDIT: The answers from smillig and joran below do resolve the question with the test data but with the real data the problem remains: I can't get an alphabetical sort.编辑:下面 smillig 和 joran 的答案确实用测试数据解决了这个问题,但对于真实数据,问题仍然存在:我无法按字母顺序排序。 This leaves me scratching my head as the basic structure of the data frame looks the same.
这让我摸不着头脑,因为数据框的基本结构看起来是一样的。 Clearly I have omitted something, but what??
显然我遗漏了什么,但是什么??
> str(mymelt)
'data.frame': 340 obs. of 3 variables:
$ Industry: chr "Animal and vegetable products" "Food and beverages" "Chemicals" "Plastic and rubber goods" ...
$ variable: Factor w/ 17 levels "Other areas",..: 17 17 17 17 17 17 17 17 17 17 ...
$ value : num 0.000904 0.000515 0.007189 0.007721 0.000274 ...
However, applying the with
statement doesn't result in levels with an alphabetical sort.但是,应用
with
语句不会导致级别按字母顺序排序。
> with(mymelt,factor(variable,levels = rev(sort(unique(variable)))))
[1] USA USA USA
[4] USA USA USA
[7] USA USA USA
[10] USA USA USA
[13] USA USA USA
[16] USA USA USA
[19] USA USA Canada
[22] Canada Canada Canada
[25] Canada Canada Canada
[28] Canada Canada Canada
All the way down to:一直到:
[334] Other areas Other areas Other areas
[337] Other areas Other areas Other areas
[340] Other areas
And if you do a levels()
it seems to show the same thing:如果你执行
levels()
它似乎显示相同的东西:
[1] "Other areas" "Oceania" "Africa"
[4] "Other Non-Eurozone" "UK" "Other Eurozone"
[7] "Holland" "Germany" "Other Asia"
[10] "Middle East" "ASEAN-5" "Singapore"
[13] "HK/China" "Japan" "South Central America"
[16] "Canada" "USA"
That is, the non-reversed version of the above.也就是说,上面的非反向版本。
The following shot shows what the plot of the real data looks like.下图显示了真实数据的图表。 As you can see, the x-axis is sorted and the y-axis is not.
如您所见,x 轴已排序,而 y 轴未排序。 I'm perplexed.
我很困惑。 I'm missing something but can't see what it is.
我错过了一些东西,但看不到它是什么。
The y-axis on your chart is also already ordered alphabetically, but from the origin.图表上的 y 轴也已按字母顺序排列,但从原点开始。 I think you can achieve the order of the axes that you want by using
xlim
and ylim
.我认为您可以通过使用
xlim
和ylim
来实现所需的轴顺序。 For example:例如:
ggplot(mymelt, aes(x = industry, y = variable, fill = value)) +
geom_tile() + geom_text(aes(fill = mymelt$value, label = mymelt$value)) +
ylim(rev(levels(mymelt$variable))) + xlim(levels(mymelt$industry))
will order the y-axis from all regions
at the top, followed by americas
, and then europe
at the bottom (which is reverse alphabetical order, technically).将从顶部的
all regions
排序 y 轴,然后是americas
,然后是底部的europe
(从技术上讲,这是反向字母顺序)。 The x-axis is alphabetically ordered from all industries
to steel
with cars
in between. x 轴按字母顺序从
all industries
到steel
,中间是cars
。
As smillig says, the default is already to order the axes alphabetically, but the y axis will be ordered from the lower left corner up.正如 smillig 所说,默认情况下已经按字母顺序对轴进行排序,但 y 轴将从左下角向上排序。
The basic rule with ggplot2 that applies to almost anything that you want in a specific order is: ggplot2的基本规则适用于几乎任何你想要的特定顺序是:
In this case, all you should need to do it this:在这种情况下,您只需要这样做:
mymelt$variable <- with(mymelt,factor(variable,levels = rev(sort(unique(variable)))))
which should work regardless of whether you're running R with stringsAsFactors = TRUE
or FALSE
.无论您是使用
stringsAsFactors = TRUE
还是FALSE
运行 R,它都应该有效。
This principle applies to ordering axis labels, ordering bars, ordering segments within bars, ordering facets, etc.此原则适用于排序轴标签、排序条、排序条内的段、排序面等。
For continuous variables there is a convenient scale_*_reverse()
but apparently not for discrete variables, which would be a nice addition, I think.对于连续变量,有一个方便的
scale_*_reverse()
但对于离散变量显然不是,我认为这将是一个很好的补充。
Another possibility is to use fct_reorder from forecast library.另一种可能性是使用预测库中的fct_reorder 。
library(forecast)
mydf %>%
pivot_longer(cols=c('all regions', 'americas', 'europe')) %>%
mutate(name1=fct_reorder(name, value, .desc=FALSE)) %>%
ggplot( aes(x = industry, y = name1, fill = value)) +
geom_tile() + geom_text(aes( label = value))
maybe this StackOverflow question can help:也许这个 StackOverflow 问题可以提供帮助:
Order data inside a geom_tile geom_tile 中的订单数据
specifically the first answer by Brandon Bertelsen:特别是 Brandon Bertelsen 的第一个答案:
"Note it's not an ordered factor, it's a factor in the right order"
“请注意,这不是有序因素,而是正确顺序的因素”
It helped me to get the right order of the y-axis in a ggplot2 geom_tile plot.它帮助我在 ggplot2 geom_tile 图中获得正确的 y 轴顺序。
Maybe a little bit late,也许晚了一点,
with(mymelt,factor(variable,levels = rev(sort(unique(variable)))))
this function doesn't order, because you are ordering "variable" that has no order (it's an unordered factor).这个函数没有顺序,因为你正在订购没有顺序的“变量”(这是一个无序的因素)。
You should transform first the variable to a character, with the as.character function, like so:您应该首先使用 as.character 函数将变量转换为字符,如下所示:
with(mymelt,factor(variable,levels = rev(sort(unique(as.character(variable))))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.