R编码为ggridges

时间:2018-01-26 00:23:26

标签: r ggridges

我是R的新编码所以请原谅这个简单的问题。我试图在R中运行ggridges geom来创建月度密度图。代码如下,但它创建的图表中的月份顺序错误enter image description here。该代码引用了一个包含3列的csv数据文件(参见图片) - MST,Aeco_5a和month。enter image description here。任何关于如何解决这个问题的建议都将非常感激。

> 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)) 

1 个答案:

答案 0 :(得分: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()