0
votes

I am doing several heatmaps in ggplot2 using geom_tile. They work great but what if instead of tiles (little rectangles) I want to have dots. My input is a binary matrix (converted in a table using melt function).
My x and y are discrete factors. How do I produce circles or dots instead of tiles.....any idea?

Thanks!

example:

dat=data.frame(sample = c("a","a","a","b","b","b","c","c","c"), cond=c("x","y","z","x","y","z","x","y","z"),value=c("1","4","6","2","3","7","4","6","7"),score=c(0,1,1,0,0,0,1,1,1))

if I use the following plot:

ggplot(dat, aes(x = sample, y = cond, color = value)) + 
  geom_point()

I get the wrong plot. Instead, I would like to have or not have a dot where the score is 0 or 1 and color them by value factor.

1
Hi Jordan, please make your post reproducible: include a same of your data and code you've written so far to get your desired result. It's hard to help without knowing what you've tried so far! - OTStats
The simplest solution is to use geom_point() instead of geom_tile(). - OTStats
I edited with a code. - Jordan

1 Answers

1
votes

I assume you mean to map score to your color aesthetic and not value, as written in your shared code.
Simply convert color to a factor in your initial aesthetics call:

ggplot(dat, aes(x = sample, y = cond, color = as.factor(score))) + 
  geom_point()

color points as factor

EDIT: The user indicated that he would like to filter observations where score is not equal to 1, and then color the points by value. You can do so by adding the following pipe operation:

I assume you mean to map score to your color aesthetic and not value, as written in your shared code.
Simply convert color to a factor in your initial aesthetics call:

dat %>% 
filter(score == 1) %>% 
ggplot(aes(x = sample, y = cond, color = as.factor(value))) + 
  geom_point()

Modified Plot

Note that there are only 3 levels of the factor score and we are missing level b from sample on the x-axis. Keep all levels by specifying drop = FALSE in scale_x_discrete():

dat %>% 
  filter(score == 1) %>% 
  ggplot(aes(x = sample, y = cond, color = as.factor(value))) + 
  geom_point() + 
  scale_x_discrete(drop = FALSE)

third plot with <code>drop = false</code>