【问题标题】:Error: Continuous value supplied to discrete scale with factor错误:用因子提供给离散比例的连续值
【发布时间】:2012-11-21 17:46:48
【问题描述】:

我得到了这个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: 我需要设置因子水平,因为智商水平应该是自定义的

【问题讨论】:

  • 如果您能提供此问题的小型、可重现的版本,帮助会容易得多。
  • 好的,添加了包含工资数据的 rda 文件,现在整个代码应该可以工作了(除了最后的 geom_smooth 部分)

标签: r ggplot2 r-factor


【解决方案1】:

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

这是文档link

【讨论】:

  • 哈哈哈,我不敢相信会造成这个令人讨厌的错误!非常感谢!
猜你喜欢
  • 2014-11-14
  • 1970-01-01
  • 2023-03-06
  • 2015-11-09
  • 2018-06-20
  • 2014-05-27
  • 1970-01-01
  • 2019-12-18
  • 2015-01-08
相关资源
最近更新 更多