错误:使用因子将连续值提供给离散比例

时间:2012-11-21 17:46:49

标签: r ggplot2 r-factor

我得到了这个data.frame

'data.frame':   935 obs. of  17 variables:
 $ IQ        : num  93 119 108 96 74 116 91 114 111 95 ...
 $ KWW       : num  35 41 46 32 27 43 24 50 37 44 ...
 $ educ      : num  12 18 14 12 11 16 10 18 15 12 ...
 $ exper     : num  11 11 11 13 14 14 13 8 13 16 ...
 $ tenure    : num  2 16 9 7 5 2 0 14 1 16 ...
 $ age       : num  31 37 33 32 34 35 30 38 36 36 ...
 $ married   : Factor w/ 2 levels "married","non-married": 1 1 1 1 1 1 2 1 1 1 ...
 $ black     : Factor w/ 2 levels "black","non-black": 2 2 2 2 2 1 2 2 2 2 ...
 $ south     : Factor w/ 2 levels "non-south","south": 1 1 1 1 1 1 1 1 1 1 ...
 $ urban     : Factor w/ 2 levels "rural","urban": 2 2 2 2 2 2 2 2 1 2 ...
 $ sibs      : num  1 1 1 4 10 1 1 2 2 1 ...
 $ brthord   : num  2 NA 2 3 6 2 2 3 3 1 ...
 $ meduc     : num  8 14 14 12 6 8 8 8 14 12 ...
 $ feduc     : num  8 14 14 12 11 NA 8 NA 5 11 ...
 $ lwage     : num  6.65 6.69 6.72 6.48 6.33 ...
 $ experclass: Factor w/ 5 levels "(0,5]","(5,10]",..: 3 3 3 3 3 3 3 2 3 4 ...
 $ iqlevel   : Factor w/ 4 levels "50","75","100",..: 2 3 3 2 1 3 2 3 3 2 ...

绘制像这样的ggplot:

   p<-ggplot(data = wage)
  p+ geom_jitter(aes(x = educ, y = lwage, shape=married,
                col = black, size=IQ, alpha=0.1))

但不喜欢这个

   p<-ggplot(data = wage)
  p+ geom_jitter(aes(x = educ, y = lwage, shape=married,
                col = black, size=iqlevel, alpha=0.1))

错误发生在geom_smooth中,这是完整的代码

  load("C:/tmp/session08.rda")

  wage["iqlevel"]<-cut(wage$IQ, breaks=c(0,75,100,125,Inf), labels=c(50,75,100,125))

  # geom_jitter where col, size, etc define different representations/ axes
  p<-ggplot(data = wage)

  p+ geom_jitter(aes(x = educ, y = lwage, shape=married,
                    col = black, size=iqlevel, alpha=0.1)) +
  # a grid
  facet_grid(urban ~ experclass)+
  # setting the axis labels
  xlab("Education (Years)") + ylab("Log Wage") +
  scale_fill_discrete(name="Experimental\nCondition") 
  # setting the labels for the legend
  labs(shape="Martial Status", col="Ethnicity", size ="IQ") +
  # removing alpha from the legend and setting dots as the symbol of the size legend
  guides(alpha=FALSE, size = guide_legend(override.aes = list(shape = 20))) +
  # trying to draw a regression, without the confidence interval
  geom_smooth(se=F, method ="lm", color="black", fullrange=T, na.rm=T,
              aes(lwd=1, x = educ, y = lwage)) 

修改

包含工资的会话文件: session08.rda

PS: 我需要设置因子水平,因为IQ水平应该是自定义的

1 个答案:

答案 0 :(得分:3)

lwd不是有效的geom_smooth美学参数。在美学功能之外设置size=1,就像使用color="black"一样。

以下是文档link