r - 在 geom_tile 中对轴标签进行排序

标签 r ggplot2

我有一个数据框,其中包含来自 20 多个国家/地区的 20 多种产品中每种产品的订单数据。我已使用 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))

这会产生这样的情节:

highlight table

在实际图中,450 个单元格的表格很好地显示了订单集中的“热点”。我要实现的最后一个改进是按字母顺序排列 x 轴和 y 轴上的项目。所以在上图中,y 轴 (variable) 将被排序为 all regions , americas ,然后 europe并且 x 轴 ( industry ) 将被订购 all industries , carssteel .事实上,x 轴已经按字母顺序排序,但如果不是这样,我不知道如何实现这一点。

我对不得不问这个问题感到有些尴尬,因为我知道在 SO 上有很多类似的问题,但是在 R 中排序和排序仍然是我个人的问题,我无法让它发挥作用。尽管我确实尝试过,但除了最简单的情况外,我在大量调用 factor 的电话中迷失了方向。 , levels , sort , orderwith .

Q. 如何排列上面的高亮表,使 y 轴和 x 轴都按字母顺序排列?

编辑:下面 smillig 和 joran 的答案确实用测试数据解决了这个问题,但使用真实数据,问题仍然存在:我无法按字母顺序排序。这让我摸不着头脑,因为数据框的基本结构看起来是一样的。显然我遗漏了一些东西,但是什么?
> 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 ...

但是,应用 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    

一直到:
 [334] Other areas           Other areas           Other areas          
 [337] Other areas           Other areas           Other areas          
 [340] Other areas

如果您执行 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"  

即上述的非反转版本。

下图显示了真实数据图的样子。如您所见,x 轴已排序,y 轴未排序。我很困惑。我错过了一些东西,但看不到它是什么。

screenshot of plot with real data

最佳答案

另一种可能性是使用 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))

关于r - 在 geom_tile 中对轴标签进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599023/

相关文章:

r - 如何将 "roll up"值写入后续记录?

sql - R 是否提供比 tryCatch() 更干净的东西来安全地进行 SQL 查询?

r - 如何在R中设置动画的速度?

r - 如何创建具有负轴和正轴的散点图?

r - ggplot中的堆栈点

r - R 中的交互式热图

r - 如何使用 ggplot2 在表达式中进行替换?

r - 在ggplot中添加手动比例时如何修复错误?

json - 大型 json 文件示例

r - 如何将两个具有大矩阵的向量连接到一个数据框中?