Katie S
Katie S

Reputation: 169

Ploting bianary (presence/absence) data with ggplot2 geom_point()

Trying to plot presence/absence data with geom_point() ggplot.

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

I tried:

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))

And it just turns out that all of the spaces are filled in:: None of the 0 in the dataframe are respected and all of the spaces are filled

Upvotes: 2

Views: 1796

Answers (3)

Axeman
Axeman

Reputation: 35382

fill is not a property of geom_point, unless you specifically set a point shape that respects it.

ggplot(df, aes(x=Col2, y=Col1, fill = factor(Col3))) +
  geom_point(size = 7, shape = 21)

enter image description here

(You need to set size in geom_point, outside the aes.)

Alternatively, you could change the shape by Col3:

ggplot(df, aes(x=Col2, y=Col1, shape = factor(Col3))) +
  geom_point(size = 7) +
  scale_shape_manual(values = c(1, 16))

enter image description here

Upvotes: 1

Dave2e
Dave2e

Reputation: 24139

If you are looking just to show the absence or presence then maybe changing the alpha will work.

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))

Another option is to use the color aesthetic and define a custom color palette of "White" and another color(s).

enter image description here

Upvotes: 1

JAQuent
JAQuent

Reputation: 1224

What about using colour? ggplot(aes(x=Col2, y=Col1, fill = Col3, colour = Col3, size = 7))

Upvotes: 1

Related Questions