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

geom_density_ridges requires the following missing aesthetics: y

无论我怎么尝试,我都无法使用 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,但这也不起作用。

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

在谷歌搜索此错误消息时,这个问题似乎位于列表的顶部,所以我想我会插话:您可能遇到此错误的另一个原因是如果您有 xy 切换:

例如,使用上述数据(使用 id 代替),这有效:

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

虽然这会引发错误:

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

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

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

似乎错误消息有点误导,根据documentation您需要为数值变量添加组美学:

The grouping aesthetic does not need to be provided if a categorical variable is mapped onto the y axis, but it does need to be provided if the variable is numerical.

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

受上述讨论和文档的启发,我在这两种模型中的任何一种上都取得了成功: ggplot(CSVdotcsv, aes(x = TSH, y = as.factor(t))) + #as factor 提供小时的图例 ggplot(CSVdotcsv, aes(x = TSH, y = t, group = t)) + # 添加组很重要 =

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