首页 > 解决方案 > 使用 ggplot2 geom_point() 绘制二进制(存在/不存在)数据

问题描述

尝试使用 geom_point() ggplot 绘制存在/不存在数据。

Col1 Col2 Col3
Name Case 1
Name Case2 0
Name Case3 1
Name2 Case 1
Name2 Case2 0
Name2 Case3 1

我试过了:

library(ggplot2)
library(dplyr)

plot <- df %>%
  ggplot(aes(x=Col2, y=Col1, fill = Col3, size = 7)) +
  geom_point() +
  theme(axis.text.x = element_text(angle = 90, hjust = .1, vjust=0.5))

事实证明,所有的空格都被填满了:: 数据框中的 0 都不被尊重,所有的空格都被填满

标签: rggplot2dplyrtidyverse

解决方案


如果您只是想显示缺席或存在,那么更改 alpha 可能会起作用。

df<- read.table(header=TRUE, text="Col1 Col2 Col3
Name Case 1
Name Case2 0
Name Case3 1
Name2 Case 1
Name2 Case2 0
Name2 Case3 1")

library(ggplot2)

#need to convert col3 from a continuous value to a discrete value.
ggplot(df, aes(x=Col2, y=Col1, alpha = factor(Col3) )) +
  geom_point(size = 10) +
  theme(axis.text.x = element_text(angle = 90, hjust = .1, vjust=0.5))

另一种选择是使用颜色美学并定义“白色”和另一种颜色的自定义调色板。

在此处输入图像描述


推荐阅读