【问题标题】:How do I selectively visualize data in a categorical column in R?如何选择性地可视化 R 中分类列中的数据?
【发布时间】:2022-08-20 00:14:38
【问题描述】:

我有一个以“状态”作为列之一的数据集。在 status 变量中,数据是“Employed”或“Left.”

我只想可视化 \"Status\" 列的 \"Left\" 条目。

目前,我只能使用以下代码可视化 \"Employed\" 和 \"Left\":

ggplot(data=employee_data) + geom_point(aes(x=satisfaction, y=last_evaluation, color=status))

如果我只想隔离“左”类别中的那些,我该怎么做?

  • data=filter(employee_data, status == \'Left\')?
  • data=employee_data[employee_data$Status==\'Left\']?
  • 嗨@jdobres,感谢您的回复。试过这个,但它不起作用。 ggplot(data=filter(employee_data, status == \"Left\") + geom_point(aes(x=satisfaction, y=last_evaluation, color=status))
  • 请添加一个最小示例:stackoverflow.com/questions/5963269/…
  • @NoelJosephPadilla,如果您确实在使用filter(employee_data, status == \"Left\") 并使用ggplot(..., aes(color=status)) 绘制它,您是说您仍然看到status == \"Employed\" 内绘制的点?真的,请在您的问题的代码块中包含示例数据,例如dput(head(employee_data,20))。谢谢!

标签: r ggplot2 data-visualization categorical-data


【解决方案1】:

这是通过一个简单的过滤器完成的。

ggplot(data = filter(employee_data, status == "left")) + 
geom_point(aes(x = satisfaction, y = last_evaluation, color = status))

使用 iris 数据集,可以看到输出

ggplot(data = filter(iris, Species == "setosa")) + 
geom_point(aes(x = Petal.Width, y = Petal.Length, color = Species))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2015-07-13
    相关资源
    最近更新 更多