R ggplot - 错误stat_bin需要连续的x变量

Kha*_*yen 13 r ggplot2

我的表是data.combined,结构如下:

'data.frame':   1309 obs. of  12 variables:
 $ Survived: Factor w/ 3 levels "0","1","None": 1 2 2 2 1 1 1 1 2 2 ...
 $ Pclass  : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 3 1 3 3 2 ...
 $ Name    : Factor w/ 1307 levels "Abbing, Mr. Anthony",..: 109 191 358 277 16 559 520 629 417 581 ...
 $ Sex     : num  2 1 1 1 2 2 2 2 1 1 ...
 $ Age     : num  22 38 26 35 35 NA 54 2 27 14 ...
 $ SibSp   : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch   : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Ticket  : Factor w/ 929 levels "110152","110413",..: 524 597 670 50 473 276 86 396 345 133 ...
 $ Fare    : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Cabin   : Factor w/ 187 levels "","A10","A14",..: 1 83 1 57 1 1 131 1 1 1 ...
 $ Embarked: Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ...
 $ Title   : Factor w/ 4 levels "Master.","Miss.",..: 3 3 2 3 3 3 3 1 3 3 ...
Run Code Online (Sandbox Code Playgroud)

我想绘制一个图表来反映Title和Survived之间的关系,按Pclass分类.我使用了以下代码:

  ggplot(data.combined[1:891,], aes(x=Title, fill = Survived)) +
  geom_histogram(binwidth = 0.5) +
  facet_wrap(~Pclass) +
  ggtitle ("Pclass") +
  xlab("Title") +
  ylab("Total count") +
  labs(fill = "Survived")
Run Code Online (Sandbox Code Playgroud)

但是这会导致错误: Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?

如果我将变量Title更改为numeric:data.combined$Title <- as.numeric(data.combined$Title)那么代码可以工作,但图中的标签也是数字(下图).请告诉我它为什么会发生以及如何解决它.谢谢.

顺便说一句,我在Mac El Capital上使用R 3.2.3.

图:x轴没有先生,而是x轴显示数字值1,2,3,4

在此输入图像描述

Kha*_*yen 17

总结上述评论的答案:

1 - 替换geom_histogram(binwidth=0.5)geom_bar().但是这种方式不允许进行binwidth定制.

2 - 使用stat_count(width = 0.5)代替geom_bar()geom_histogram(binwidth = 0.5)将解决它.