【问题标题】:How to create a ggplot when the answers are FALSE or TRUE?当答案为 FALSE 或 TRUE 时如何创建 ggplot?
【发布时间】:2021-03-14 15:29:57
【问题描述】:

当我的答案是 TRUE 或 FALSE 时,如何使用 ggplot 创建绘图?

这是我的代码:

t.obese<-master1%>%
  filter(Income>0,obese==TRUE)%>%
  select(Income,obese)

> head(t.obese)
  Income obese
1  21600    TRUE
2   4000    TRUE
3  12720    TRUE
4  26772    TRUE

当我尝试创建绘图时,r 告诉我“不知道如何为 Have_labelled/vctrs_vctr/double 类型的对象自动选择比例。默认为连续。 Fehler:stat_count() 只能具有 x 或 y 美学。”

谢谢!

> dput(t.obese[1:10, ])
structure(list(Income = structure(c(1944, 4000, 16000, 19200, 
22800, 21600, 18000, 18000, 2000, 18000), label = "Wages,Salary from                    main job", format.stata = "%42.0g", labels = c(`[-5] in Fragebogenversion    nicht enthalten` = -5, 
 `[-2] trifft nicht zu` = -2), class = c("haven_labelled",      "vctrs_vctr", 
 "double")), obese = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE)), row.names = c(NA, 10L), class = "data.frame")

【问题讨论】:

  • 显示你的情节代码?
  • 你想在x轴上放什么?你想在y轴上放什么?什么类型的图(条形图、线形图、点图...)?你想用颜色还是别的什么?
  • 因为我是一个非常初学者,我尝试过p&lt;-ggplot(t.obese, aes(x=obese, y=Income) + geom_bar() 我想要一个箱线图,y=收入和x=肥胖
  • 但如果它是一条线、点或直方图,任何都可以。我的主要问题是 TRUE 和 FALSE 的问题
  • @GregorThomas 我编辑了它!

标签: r ggplot2 r-haven


【解决方案1】:

使用您共享的数据,这是最小的,尝试了这个:

library(ggplot2)
#Code1
ggplot(as.data.frame(t.obese), aes(x=factor(obese), y=Income)) +
  geom_bar(stat='identity')+
  xlab('Obese')+
  scale_y_continuous(labels = scales::comma)

输出:

还有这个:

#Code 2
ggplot(as.data.frame(t.obese), aes(x=factor(obese), y=Income)) +
  geom_point()+
  geom_jitter()+
  geom_boxplot()+
  xlab('Obese')

输出:

【讨论】:

    【解决方案2】:

    如果你想比较肥胖的收入分布,那么你需要 obese = TRUE 和 obese = FALSE,所以你可以进行比较

    我随机创建了一个non_obese 数据集只是为了进行比较。 另外,我删除了Incomehaven_labelled 类,因为它在reprex 渲染中引起了一些问题[使用haven::zap_labels()

    无论如何,希望以下内容能帮助您入门

    library(dplyr)
    library(ggplot2)
    library(haven)
    
    obese <- 
    structure(list(Income = structure(c(1944, 4000, 16000, 19200, 
                                        22800, 21600, 18000, 18000, 2000, 18000), 
                                      label = "Wages,Salary from main job", 
                                      format.stata = "%42.0g", 
                                      labels = c(`[-5] in Fragebogenversion nicht enthalten` = -5,
                                                 `[-2] trifft nicht zu` = -2), 
                                      class = c("haven_labelled", "vctrs_vctr","double")), 
                   obese = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,TRUE, TRUE, TRUE)), 
              row.names = c(NA, 10L), class = "data.frame"
              )
    
    
    # remove the haven/labelled class of the income variable
    obese <- 
      obese %>% 
      haven::zap_labels() 
    
    non_obese <- 
      obese %>% 
      mutate(
        Income = Income - rnorm(1, mean = 1000, sd = 50),
        obese  = !obese
      )
    
    
    
    full_data <- 
      bind_rows(obese, non_obese)
    
    
    # Box plot 
    full_data %>% 
      ggplot(
        aes(obese, Income)
      )+
      geom_boxplot(width = 0.5)+
      geom_point(position = position_jitter(width  = 0.05))
    

    # Density plot
    full_data %>% 
      ggplot(
        aes(Income,fill = obese)
      )+
      geom_density(alpha = 0.5)
    

    reprex package (v0.3.0) 于 2020 年 12 月 3 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多