R ggplot2条形图在多列上 [英] R ggplot2 Bar Chart on Multiple Columns
问题描述
这似乎很简单,但是有些基本的内容我并没有在这里讲到.我有一个数据框,其中每个月的某些事件计数为列,每年的计数为行.像这样:
This seems so simple, but there is something fundamental I am not getting here. I have a dataframe with counts of some event per month as columns, per year as rows. Like this:
season sep oct nov dec jan feb
2000 2 7 47 152 259 140
2001 1 5 88 236 251 145
2002 2 14 72 263 331 147
2003 5 6 71 207 290 242
首先,我想创建一个季节的条形图.例如,对于2000季节,条形图将每个月的值显示为垂直条(可怜的是,没有足够的代表点来张贴示例图像).我猜我需要以某种方式重塑数据吗?
First, I would like to create a bar chart for one season. For example, for the 2000 season, a bar chart showing the values for each month as a vertical bar (alas, not enough rep points to post an image of an example). I'm guessing I need to reshape my data in some way?
最终,我想创建一组多面包装的图表,每个季节一个.
Ultimately I would like to create a facet wrapped set of charts, one for each season.
请注意,我发现了一个类似的帖子,但是由于问题不清晰,该帖子过于复杂.
Please note I found a similar post, but the post was over complicated due to a lack of clarity on the question.
推荐答案
library(reshape2)
library(ggplot2)
df_m <- melt(df, id.vars = "season")
为了绘制一个季节(例如2000年)
In order to plot for one season (e.g. 2000)
ggplot(subset(df_m, season == "2001"), aes(x = variable, y = value)) +
geom_bar(stat = "identity")
对于面包装的一组图表,请尝试:
And for facet wrapped set of charts try:
ggplot(df_m, aes(x = variable, y = value)) + geom_bar(stat = "identity") +
facet_wrap(~season)
这篇关于R ggplot2条形图在多列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!