使用geom_bar彼此相邻的两个条 [英] two bars next to each other using geom_bar

查看:44
本文介绍了使用geom_bar彼此相邻的两个条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

 df
decades marxftext durkftext
1 1970-1979      3009       393
2 1980-1989      3468       469
3 1990-1999      3420       657 
4 2000-2009      3162       700

并且我想绘制一个marxftext和durkftext彼此相邻的小图

and I want to draw a barplot where marxftext and durkftext are next to each other

我尝试了以下

 melt(df,id.vars ='decades')

得到了

  decades  variable value
1 1970-1979 marxftext  3009
2 1980-1989 marxftext  3468
3 1990-1999 marxftext  3420
4 2000-2009 marxftext  3162
5 1970-1979 durkftext   393
6 1980-1989 durkftext   469
7 1990-1999 durkftext   657
8 2000-2009 durkftext   700

如何使用geom_bar创建并排的条形图?

how I can I create a side by side bar graph using geom_bar with this?

推荐答案

在这里共享一个示例,以防其他人想对其进行改进...

Sharing a sample here, in case other folks want to improve on it...

df <- data.frame(c("1970-1979", "1980-1989", "1990-1999"), c(3009, 3468, 3420), c(393, 469, 657))
colnames(df) <- c("decades", "marxftext", "durkftext")

require(ggplot2)
require(reshape2)

df <- melt(df, id = "decades")

ggplot() + geom_bar(data = df, aes(x = decades, y = value, fill = variable), position = "dodge", stat = "identity")

修改请求?发表评论,我会发表修改

Modification requests? Leave a comment and I'll post edits

这篇关于使用geom_bar彼此相邻的两个条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆