1

I have a data frame and I would like to stack the points that have overlaps exactly on top of each other.

here is my example data:

value <- c(1.080251e-04, 1.708859e-01, 1.232473e-05, 4.519876e-03,2.914256e-01, 5.869711e-03, 2.196347e-01,4.124873e-01, 5.914052e-03, 2.305623e-03, 1.439013e-01, 5.407597e-03, 7.530298e-02, 7.746897e-03)
names = letters[1:7]
data <- data.frame(names = rep(names,), group = group, value = value, stringsAsFactors = T)
group <- c(rep("AA", 7) , rep("BB", 7))

I am using the following command:

p <- ggplot(data, aes(x = names, y = "", color = group)) + 
geom_point(aes(size = -log(value)), position = "stack") 
plot(p)

But the stacked circle outlines out of the grid. I want it close or exactly next to the bottom circle. do you have any idea how I can fix the issue?

Thanks, enter image description here

say.ff
  • 373
  • 1
  • 7
  • 21
  • Try `aes(y = 1...` and then set new y limits: `ylim(1,2)`. If this is not working, please provide a reproducible example of your dataset (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Mar 01 '20 at 01:56
  • Thanks for your response! It did not work. I have edited my question. Can you please take a look at it? – say.ff Mar 01 '20 at 02:21

1 Answers1

4

The y-axis has no numeric value, so use the group instead. And we don't need the color legend now since the group labels are shown on the y-axis.

ggplot(data, aes(x = names, y = group, color = group)) + 
  geom_point(aes(size = -log(value))) +
  guides(color=FALSE)

enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26
  • Thats a good idea too. Do you know how can I decrease the distance between vertical lines. I want the circles of two groups to be closer to each other. – say.ff Mar 01 '20 at 05:14
  • 1
    After producing the graph, reduce the height of the device manually. Or when saving to a file, tinker with the `height` argument in the `ggsave` function. For example, `ggsave("figure1.png", height=2, width=4)`. See my edited graph. – Edward Mar 01 '20 at 06:10
  • Thanks for your reply. It was really helpful. Using ggsave() reduces the hight of my figure But not as much as I want. Do you know any other way that I can play with the horizontal lines to reduce the distance between them? – say.ff Mar 07 '20 at 01:29
  • You want the dots closer together, almost touching? – Edward Mar 07 '20 at 01:57
  • 1
    Maybe you can increase the size of the points: `+ scale_size_continuous(range=c(3,10))` and/or add more discrete categories to the y-axis: `+ ylim("","AA","BB","")` – Edward Mar 07 '20 at 02:43
  • I used the ylim() which is generating pretty much what I need. Thank you. – say.ff Mar 07 '20 at 20:14