qplot facet两个直方图

时间:2013-09-09 09:23:28

标签: r ggplot2 histogram

您好我在表格中有以下列:

Person
Score1
Score2

我可以使用qplot绘制得分的历史记录:

qplot(Score1,data = dat,geom="histogram")

这很好,但是如何并排绘制两个直方图 - 一个是Score1,另一个是Score2。

我还设法使用ggplot和熔化Score1和Score2在同一图表上绘制:

m <- melt(dat[,c(2,3)])

>head(m)

    variable      value
  1 Score1        50
  2 Score2        70
  3 Score1        45
  4 Score2        30.5
  5 Score1        70
  6 Score2        40

ggplot(m,aes(value)) + geom_bar(binwidth = 1)

但是在尝试使用facet_wrap()

ggplot(m,aes(value)) + geom_bar(binwidth = 1) + facet_wrap(variable ~ Score_type) 

我明白了:

Error in layout_base(data,cols,drop = drop)
At least one layer must contain all variables used for facetting

有什么想法吗?如果我自己解决这个问题,我会发布。还可以按Score1或Score2订购x轴吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

得到了 - 我传递的facet_wrap函数参数中的小错误。它适用于:

ggplot(m,aes(value)) + geom_bar(binwidth = 1) + facet_wrap(~variable)
相关问题