ggplot:多变量(多连续变量)绘图 [英] ggplot : Multi variable (multiple continuous variable) plotting
问题描述
使用 dplyr
从大数据中推导出的数据显示了有关2013年和2014年四季度总销售额的信息。
A data that is deduced from a larger data using dplyr
, shows me information regarding the total sales of the four quarters for the years 2013 and 2014.
Quarter X2013 X2014 Total.Result
1 Qtr 1 77282.13 66421.10 143703.2
2 Qtr 2 69174.64 76480.13 145654.8
3 Qtr 3 65238.47 79081.74 144320.2
4 Qtr 4 65429.73 109738.82 175168.5
我想绘制一个通过比较酒吧上的两年和每个季度的酒吧组(如下所示),使用 ggplot
来创建以下条形图。 (从MS Excel输出)
I wish to plot a bargraph for the following using ggplot
by comparing the two years on the bars and for such bar-groups for the each of the quarters like shown below. (output from MS Excel)
我使用的ggplot语句如下所示(我可能是错的)
The ggplot statement that I used is as shown below (I may be wrong)
ggplot(qtr2, aes(x=as.factor(Quarter),fill=c(X2013,X2014))) +
geom_bar(position="dodge")
而我收到错误
Error: Aesthetics must either be length one, or the same length as the dataProblems:as.factor(Quarter)
推荐答案
您需要将数据转换为ggplot的长格式:
You need to bring your data into the long format for ggplot:
require(ggplot2)
require(reshape2)
df <- read.table(header = TRUE, text = 'R Quarter X2013 X2014 Total.Result
1 Qtr 1 77282.13 66421.10 143703.2
2 Qtr 2 69174.64 76480.13 145654.8
3 Qtr 3 65238.47 79081.74 144320.2
4 Qtr 4 65429.73 109738.82 175168.5')
df$R <- NULL
head(df)
# bring to long data (1)
dfm <- melt(df, id.vars = c('Quarter', 'Total.Result'))
dfm
ggplot(dfm, aes(x = factor(Quarter), y = value, fill = variable) ) +
geom_bar(stat="identity", position = 'dodge')
关于总体结果栏/栏:我不知道这些数据应该来自哪里(缺少?) - 只有一列,但有两个栏?这些值不适合。如果你告诉我如何生成这些图,那么绘制应该没有问题。
Regarding the Total Result column / bars: I don't know where this data should come from (missing?) - There is only one column, but two bars? And the values do not fit. If you tell me how to generate these, it should be no problem to plot.
这篇关于ggplot:多变量(多连续变量)绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!