【发布时间】:2017-12-09 23:57:43
【问题描述】:
我正在尝试制作一个函数来生成大量(垂直)条形图,以按四分位数为各个组生成收入均值。我希望我的四分位列在数据框中使用它们的列名单独命名。
这使得我的测试数据:
quart_nms <- c("1st", "2nd", "3rd", "4th")
tile_nms <- function(vec, nms){
if (length(vec) != length(nms)) stop(
"length vec ", length(vec), " must equal length nms ", length(nms))
out <- data.frame(t(unlist(vec)))
colnames(out)<- nms
out
}
t1 <- tile_nms(1:4, quart_nms)
这是我的功能。我认为,我理解 ggplot2 需要此类图表的宽格式数据,但我怀疑我没有正确传递变量列表。它们不应该是 y 值吗?
bar_plot <- function(.dt, .tit="U.S. Personal Income Distribution by Quartile",
.sub = NULL, .xl = "Income Quartiles", .yl = "Mean Income",
...){
ggplot(data =.dt) +
geom_col(mapping = aes(y = list(1st, 2nd, 3rd, 4th)), stat = identity) +
labs(title = .tit, subtitle = .sub)
}
gr1 <- bar_plot(.dt = t1, .sub = "pop subgroup name")
【问题讨论】:
标签: r debugging ggplot2 graphics