【问题标题】:Violin Plots by Year按年份划分的小提琴情节
【发布时间】:2020-07-13 20:23:32
【问题描述】:

我今天在 ggplot 中对 geom_violin 没有任何运气。有什么帮助吗?

我在当地的比赛中有 10 年的跑步时间。我想绘制每年前 10 次的分布。例如:2018 年:14:51-17:29、2017 年:15:03-18:22 等

每次我运行代码时,它都会为我提供 10 年内前 10 名的分布(100 个数据点),而不是每年细分(每年 10 个数据点)。

代码:

violin <- 5kAthletes %>%
  filter(Category=="Top10")

(^^^这对我来说很合适^^^

vplot<-violin %>%
  ggplot(aes(x=Year, y=Minutes, fill=Year)) +
  geom_violin() +
  xlab("Year") +
  theme(legend.position = "none") +
  xlab("")  
vplot

【问题讨论】:

    标签: r ggplot2 violin-plot


    【解决方案1】:

    您必须在美学中添加group = Year,即ggplot(aes(x=Year, y=Minutes, fill=Year, group = Year))。使用gapminder 数据集作为示例数据:

    library(ggplot2)
    library(dplyr)
    library(gapminder)
    
    df <- gapminder::gapminder %>% 
      group_by(year) %>% 
      top_n(10, lifeExp)
    
    vplot <- df %>%
      ggplot(aes(x=year, y=lifeExp, fill=year, group = year)) +
      geom_violin() +
      xlab("Year") +
      theme(legend.position = "none") +
      xlab("")  
    vplot
    

    reprex package (v0.3.0) 于 2020 年 4 月 2 日创建

    【讨论】:

      猜你喜欢
      • 2018-05-15
      • 1970-01-01
      • 2021-02-16
      • 2019-05-21
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      相关资源
      最近更新 更多