【问题标题】:R Coding for ggridgesggridges的R编码
【发布时间】:2018-01-26 00:23:26
【问题描述】:

我是 R 编码的新手,所以请原谅这个简单的问题。我正在尝试在 R 中运行 ggridges geom 来创建每月密度图。代码如下,但它创建的月份顺序错误:

代码引用了一个包含 3 列的 csv 数据文件(见图) - MST、Aeco_5a 和月份: 任何有关如何解决此问题的建议将不胜感激。这是我的代码:

> library(ggridges)
> read_csv("C:/Users/Calvin Johnson/Desktop/Aeco_Price_2017.csv")
Parsed with column specification:
cols(
  MST = col_character(),
  Month = col_character(),
  Aeco_5a = col_double()
)
# A tibble: 365 x 3
         MST   Month Aeco_5a
       <chr>   <chr>   <dbl>
 1  1/1/2017 January  3.2678
 2  1/2/2017 January  3.2678
 3  1/3/2017 January  3.0570
 4  1/4/2017 January  2.7811
 5  1/5/2017 January  2.6354
 6  1/6/2017 January  2.7483
 7  1/7/2017 January  2.7483
 8  1/8/2017 January  2.7483
 9  1/9/2017 January  2.5905
10 1/10/2017 January  2.6902
# ... with 355 more rows
> 
> mins<-min(Aeco_Price_2017$Aeco_5a)
> maxs<-max(Aeco_Price_2017$Aeco_5a)
> 
> ggplot(Aeco_Price_2017,aes(x = Aeco_5a,y=Month,height=..density..))+
+     geom_density_ridges(scale=3) +
+     scale_x_continuous(limits = c(mins,maxs)) 

【问题讨论】:

    标签: r ridgeline-plot ggridges


    【解决方案1】:

    这有两个部分:(1) 您希望您的月份为 factor 而不是 chr,以及 (2) 您需要按照我们通常订购月份的方式订购因子。

    使用一些可重现的数据:

    library(ggridges)
    df <- sapply(month.abb, function(x) { rnorm(10, rnorm(1), sd = 1)}) 
    df <- as_tibble(x) %>% gather(key = "month")
    

    然后您需要将 mutate 月份作为一个因素,并使用它们在 data.frame 中显示的实际顺序定义的级别(unique 给出数据集中的唯一级别,并在它们在您的数据中的排序方式(“Jan”、“Feb”、...))。然后你需要反转它们,因为这样“Jan”将位于底部(这是第一个因素)。

    df %>% 
      # switch to factor, and define the levels they way you want them to show up 
      # in the ggplot; "Dec", "Nov", "Oct", ... 
      mutate(month = factor(month, levels = rev(unique(df$month)))) %>% 
      ggplot(aes(x = value, y = month)) + 
      geom_density_ridges()
    

    【讨论】:

    • 感谢您的帮助。我尝试了您的建议,并收到以下错误消息:UseMethod(“mutate_”)中的错误:没有适用于“mutate_”的方法应用于类“c('matrix','double','numeric')”的对象.知道这里发生了什么吗?
    • mutate() 是库 dplyr 的一部分。您可以输入library(tidyverse)它会加载dplyrggplot2,还有tidyr,其中包括gather()函数(来自Jesse的答案)。