MSD*_*MSD 4 r bar-chart ggplot2
使用,我正在为和 fillmtcars创建条形图。没有cyl==8 & gear==4的汽车。绘制时,我习惯保留条形的宽度。然后,使用I 向栏添加标签。cylgearposition_dodge(preserve = "single")geom_text
library(ggplot)
library(dplyr)
df <- count(mtcars, cyl, gear)
ggplot(df, aes(x = factor(cyl), y = n, fill = factor(gear))) +
geom_col(position = position_dodge(preserve = "single")) +
geom_text(aes(label = n, y = n + .07), size = 5, vjust = 0,
position = position_dodge(width=.9))
Run Code Online (Sandbox Code Playgroud)
出来两个问题:
这就是completefromtidyr可以通过填写这些缺失值来提供帮助的地方。如果您愿意,您也可以完全使用vjust而不是添加到 y 值。
library(ggplot2)
library(dplyr)
library(tidyr)
df <- count(mtcars, cyl, gear) %>%
complete(cyl, gear, fill = list(n = 0))
ggplot(df, aes(x = factor(cyl), y = n, fill = factor(gear))) +
geom_col(position = position_dodge()) +
geom_text(aes(label = n, y = n), size = 5, vjust = -0.5,
position = position_dodge(width=.9))
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.1)于 2019-01-17 创建
| 归档时间: |
|
| 查看次数: |
1651 次 |
| 最近记录: |