r - 按年份划分的小提琴情节
问题描述
我今天在 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
解决方案
你必须增加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 包(v0.3.0)于 2020 年 4 月 2 日创建
推荐阅读
- c - 为什么不能为 *P 分配数字?
- ios - 在 iOS 版 Google Analytics(分析)中跟踪自然搜索流量
- xquery - and-query 匹配一个属性值和一个子元素值
- oauth-2.0 - 使用 symfony 4 获取 OAuth 凭据时出错:“invalid_client”
- kotlin - 当使用可变列表编译器的 .remove 函数抛出异常时,还有什么选择吗?
- powershell - 使用powershell手动创建文件索引,制表符分隔
- flutter - 未知操作系统
- bash - 如何删除不包含特定字符的行?
- openlayers - 开放层 - GeoJSON 错误投影
- python - python将复杂的列表列表转换为字符串