R ggplot - 错误stat_bin需要连续的x变量 [英] R ggplot - Error stat_bin requires continuous x variable

查看:2311
本文介绍了R ggplot - 错误stat_bin需要连续的x变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 'data.frame':1309 obs。 12个变量:
$生存:因子w / 3等级0,1,无:1 2 2 2 1 1 1 1 2 2 ...
$ Pclass:因子w / 3等级1,2,3:3 1 3 1 3 3 1 3 3 2 ...
$名称:因子w / 1307等级Abbing,Anthony先生.. :109 191 358 277 16 559 520 629 417 581 ...
$性别:第2 1 1 1 2 2 2 2 1 1 ...
$年龄:第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 ...
$票务:因数为929关卡110152,110413,..:524 597 670 50 473 276 86 396 345 133 ...
$票价:7.25 71.28 7.92 53.1 8.05 ...
$客舱:187个等级的因子,A10,A14,..:1 83 1 57 1 1 131 1 1 1 ...
$上船:因子w / 4水平,C,Q,S:4 2 4 4 4 3 4 4 4 2 ...
$标题:因子有4级主人,小姐。 ,..:3 3 2 3 3 3 3 1 3 3 ...

我想画画一个图表来反映标题和生存之间的关系,分类d由Pclass。我使用了下面的代码:

  ggplot(data.combined [1:891,],aes(x = Title,fill = )+ 
geom_histogram(binwidth = 0.5)+
facet_wrap(〜Pclass)+
ggtitle(Pclass)+
xlab(Title)+
ylab(Total count)+
labs(fill =Survived)

然而,这会导致错误:错误:StatBin需要连续的x变量,x变量是离散的。也许你想stat =count?



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

顺便说一句,我在Mac El Capital上使用R 3.2.3。 图表:代替先生,小姐,x轴的女士显示数字值1,2,3,4



解决方案

总结上述评论的答案:
$ b $ 1 - 替换 geom_histogram(binwidth = 0.5) with geom_bar()。然而,这种方式不允许自定义binwidth。



2 - 使用 stat_count(width = 0.5)而不是 geom_bar() geom_histogram(binwidth = 0.5)会解决它。


My table is data.combined with following structure:

'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 ...

I want to draw a graph to reflect the relationship between Title and Survived, categorized by Pclass. I used the following code:

  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")

However this results in error: Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?

If I change variable Title into numeric: data.combined$Title <- as.numeric(data.combined$Title) then the code works but the label in the graph is also numeric (below). Please tell me why it happens and how to fix it. Thanks.

Btw, I use R 3.2.3 on Mac El Capital.

Graph: Instead of Mr, Miss,Mrs the x axis shows numeric values 1,2,3,4

解决方案

Sum up the answer from the comments above:

1 - Replace geom_histogram(binwidth=0.5) with geom_bar(). However this way will not allow binwidth customization.

2 - Using stat_count(width = 0.5) instead of geom_bar() or geom_histogram(binwidth = 0.5) would solve it.

这篇关于R ggplot - 错误stat_bin需要连续的x变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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