【问题标题】:error: Invalid argument to unary operator错误:一元运算符的参数无效
【发布时间】:2023-04-04 01:16:01
【问题描述】:

我见过this。 我有这样的数据:

female=c("hw","hw","uw","uw","w","w")
male=c("w","hw","uw","w")

我需要为男性和女性制作直方图,所以我制作了 2 个不同的 data.frame:

male<-data.frame(class=male)
female<-data.frame(class=female)

然后绘制hist:

library(ggplot2)

ggplot(data=male,col="green",aes(x=class))
+ geom_histogram(data=female,col="red")

但它抱怨:

一元运算符的参数无效

我需要用绿色制作男性情节,用蓝色制作女性情节。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    如果你运行male$class,它会给出NULL,所以我不确定你想在那里做什么。也许是这样的:

    df=data.frame(gender=as.factor(c(rep("female",6),rep("male",4))),
      outcome=as.character(c("hw","hw","uw","uw","w","w","w","hw","uw","w")))
    
    library(ggplot2)
    
    ggplot(data=df,aes(x=outcome,fill=gender)) + 
      geom_histogram(stat="count",position="dodge") + scale_fill_manual(values=c("blue","green"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多