【问题标题】:Struggles with tidyverse and ridgeline plots与 tidyverse 和山脊线图作斗争
【发布时间】:2021-05-08 19:53:43
【问题描述】:

我是一名开始编程的学生,我正在尝试制定温度数据的脊线图,使用 tidyverse 按月对温度进行分组。初始数据如下所示:

我正在尝试使用此代码按月对数据进行分组:

class(weather)  #what class is dataset = dataframe
head(weather)  #structure of the dataset  
attach(weather)  #attach column names
weather.month <- weather %>%
  mutate(month = weather$Day) %>%  #sort data by month
  group_by(month)
head(weather.month)  #view dataset with new month column
class(weather.month$month) #view class of column month = character

通过这段代码,我得到下面的图像:

ggplot(weather.month, aes(x = `High`, y = 'month', fill = stat(x))) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis_c(name = "Temp. [F]", option = "C") +
  labs(title = 'Temperatures in Brookings in 2019')

我正在尝试获取这样的图像:

我假设我没有正确分组数据,但我不知道如何修复它....有什么建议吗?

【问题讨论】:

  • 目前我们为每个组month 提供一个High 值。能否提供完整的数据?
  • 是的,我包含了原始数据头部的片段。
  • 以后可以使用dput(weather.month)创建一个重现性更强的例子。
  • 我认为您有一个包含 2019 年所有 12 个月的数据集?如果这是真的并且您的数据框被称为df,那么请执行以下操作:在控制台中写入:dput(df) 然后发布结果。
  • 好的,我刚刚更新了帖子以包含 dput() 的输出

标签: r tidyverse ridgeline-plot


【解决方案1】:

正如评论中所说,发布dput(your.Data)的输出以帮助社区帮助您。此外,在脚本中包含您已加载的library,以让人们知道您使用的功能来自哪里。但是,正如@mhovd 所建议的,您必须取消引用变量名。由于您没有以可重现的格式发布数据,因此我使用内置数据集 iris 举例说明:

library(ggridges)
data(iris)
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = stat(x))) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis_c(name = "mm", option = "C") +
  labs(title = 'Sepal Length')

给你:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2012-12-12
    • 2020-03-28
    相关资源
    最近更新 更多