geom_density_ridges需要以下缺失的美感:y

时间:2019-01-11 13:13:35

标签: r ggplot2 ggridges

无论我做什么尝试,都无法使用ggridges创建山脊线图。使用如下所示的数据帧graphing_dataframe

str(graphing_dataframe)
summary(graphing_dataframe)

> str(graphing_dataframe)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   14 obs. of  3 variables:
 $ id    : chr  "00343" "00343" "00343" "00343" ...
 $ week  : num  14 1 2 3 4 5 6 7 8 9 ...
 $ rating: num  14 4 12 8 14 19 16 16 7 8 ...
 - attr(*, "spec")=
  .. cols(
  ..   id = col_character(),
  ..   week = col_double(),
  ..   rating = col_double()
  .. )
> summary(graphing_dataframe)
      id                 week           rating     
 Length:14          Min.   : 1.00   Min.   : 4.00  
 Class :character   1st Qu.: 4.25   1st Qu.: 8.00  
 Mode  :character   Median : 7.50   Median :10.50  
                    Mean   : 7.50   Mean   :11.43  
                    3rd Qu.:10.75   3rd Qu.:15.50  
                    Max.   :14.00   Max.   :19.00 

我的数据是

structure(list(id = c("00343", "00343", "00343", "00343", "00343", 
"00343", "00343", "00343", "00343", "00343", "00343", "00343", 
"00343", "00343"), week = c(14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13), rating = c(14, 4, 12, 8, 14, 19, 16, 16, 7, 8, 9, 
18, 9, 6)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-14L), spec = structure(list(cols = list(id = structure(list(), class = c("collector_character", 
"collector")), week = structure(list(), class = c("collector_double", 
"collector")), rating = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector"))), class = "col_spec"))

我的代码是

ggplot(graphing_dataframe, 
       aes(x = rating, y = week, fill = ..x..)
       ) +
  geom_density_ridges()

Picking joint bandwidth of 2.53
Error: geom_density_ridges requires the following missing aesthetics: y

我已经尝试过每this question使用unlist,但这也不起作用。

6 个答案:

答案 0 :(得分:2)

似乎错误消息有点让人误解,根据documentation,您需要为数字变量添加整体美感:

  

如果将分类变量映射到y轴,则不需要提供分组美观性,但是如果变量是数字,则必须提供分组美观性。

因此将group = week添加到ggplot(aes(…))应该可以解决问题。 (我自己也遇到过同样的问题。)

答案 1 :(得分:1)

正如@JonnyPhelps所说,我的数据与山脊线图不兼容(反之亦然)。

答案 2 :(得分:0)

在搜索该错误消息时,这个问题似乎在列表的顶部,所以我想听一听:出现此错误的另一个原因是,如果您有x和{{ 1}}切换:

例如,使用上面的数据(改为使用y),这有效:

id

这会引发错误:

ggplot(graphing_dataframe, 
        aes(x = rating, y = id)) +
  geom_density_ridges()

答案 3 :(得分:0)

我还注意到,如果y是一个因素,则不会弹出此错误。 试试这个:)

ggplot(graphing_dataframe, 
        aes(x = id, y=as.factor(rating))) +
  geom_density_ridges()

答案 4 :(得分:0)

在上述讨论和文档的启发下,我成功使用了这两种模型: ggplot(CSVdotcsv,aes(x = TSH,y = as.factor(t)))+ #as factor提供小时的图例 ggplot(CSVdotcsv,aes(x = TSH,y = t,group = t))+#重要的是添加group =

答案 5 :(得分:0)

y 需要是一个分类变量。你可以试试:

ggplot(graphing_dataframe, 
   aes(x = rating, y = as.factor(week), fill = ..x..))+
geom_density_ridges()

ggplot(graphing_dataframe, 
   aes(x = rating, y = as.character(week), fill = ..x..))+
geom_density_ridges()