ggplot中的分组条形图

编程入门 行业动态 更新时间:2024-10-27 11:25:40
本文介绍了ggplot中的分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于示例数据框:

df <- structure(list(year = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4), imd.quintile = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), average_antibiotic = c(1.17153515458827, 1.11592565388857, 1.09288449967773, 1.07442652168281, 1.06102887394413, 1.0560582933182, 1.00678980505929, 0.992997489072538, 0.978343676071694, 0.967900478870214, 1.02854157116164, 0.98339099101476, 0.981198852494798, 0.971392872980818, 0.962289579742817, 1.00601488964457, 0.951187417739673, 0.950706064156994, 0.939174499710836, 0.934948233015044)), .Names = c("year", "imd.quintile", "average_antibiotic"), row.names = c(NA, -20L ), vars = "year", drop = TRUE, class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

我想生成一个分组的条形图,非常类似于此帖子.

I want to produce a grouped bar chart, very similar to this post.

我想在x轴上输入年份,并在y轴上输入average_antibiotic.我想要五个条形图(对于每个imd.quintile-这都是图例).

I want year on the x axes, and average_antibiotic on the y axes. I want the five bar charts (for each imd.quintile - which is the legend).

我已经尝试了几种方法(基于帖子和其他地方),但是无法使其正常工作.

I have tried a couple of options (based on the post and elsewhere), but can't make it work.

ggplot(df, aes(x = imd.quintile, y = average_antibiotic)) + geom_col() + facet_wrap(~ year) ggplot(df, aes(x = imd.quintile, y = average_antibiotic)) + geom_bar(aes(fill = imd.quintile), position = "dodge", stat="identity")

有什么想法吗?

推荐答案

我相信您正在寻找这样的东西:

I believe you are looking for something like this:

library(ggplot2) ggplot(df ) + geom_col(aes(x = year, y = average_antibiotic, group=imd.quintile, fill=imd.quintile), position = "dodge" )

更多推荐

ggplot中的分组条形图

本文发布于:2023-07-18 00:49:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条形图   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!