2

I want to separately plot data in a bubble plot like the image right (I make this in PowerPoint just to visualize). enter image description here At the moment I can only create a plot that looks like in the left where the bubble are overlapping. How can I do this in R?

b <- ggplot(df, aes(x = Year, y = Type))

b + geom_point(aes(color = Spp, size = value), alpha = 0.6) +
  scale_color_manual(values = c("#0000FF", "#DAA520",  "#228B22","#E7B888")) +
  scale_size(range = c(0.5, 12)) 
Dendrobium
  • 323
  • 5
  • 13

1 Answers1

4

You can have the use of position_dodge() argument in your geom_point. If you apply it directly on your code, it will position points in an horizontal manner, so the idea is to switch your x and y variables and use coord_flip to get it in the right way:

library(ggplot2)
ggplot(df, aes(y = as.factor(Year), x = Type))+ 
  geom_point(aes(color = Group, size = Value), alpha = 0.6, position = position_dodge(0.9)) +
  scale_color_manual(values = c("#0000FF", "#DAA520",  "#228B22","#E7B888")) +
  scale_size(range = c(1, 15)) +
  coord_flip()

enter image description here

Does it look what you are trying to achieve ?


EDIT: Adding text in the middle of each points

To add labeling into each point, you can use geom_text and set the same position_dodge2 argument than for geom_point.

NB: I use position_dodge2 instead of position_dodge and slightly change values of width because I found position_dodge2 more adapted to this case.

library(ggplot2)
ggplot(df, aes(y = as.factor(Year), x = Type))+ 
  geom_point(aes(color = Group, size = Value), alpha = 0.6, 
             position = position_dodge2(width  = 1)) +
  scale_color_manual(values = c("#0000FF", "#DAA520",  "#228B22","#E7B888")) +
  scale_size(range = c(3, 15)) +
  coord_flip()+
  geom_text(aes(label = Value, group = Group), 
            position = position_dodge2(width = 1))

enter image description here

Reproducible example

As you did not provide a reproducible example, I made one that is maybe not fully representative of your original dataset. If my answer is not working for you, you should consider providing a reproducible example (see here: How to make a great R reproducible example)

Group <- c(LETTERS[1:3],"A",LETTERS[1:2],LETTERS[1:3])
Year <- c(rep(1918,4),rep(2018,5))
Type <- c(rep("PP",3),"QQ","PP","PP","QQ","QQ","QQ")
Value <- sample(1:50,9)
df <- data.frame(Group, Year, Value, Type)
df$Type <- factor(df$Type, levels = c("PP","QQ"))
dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thank you very much @dc37 I really appreciate your prompt reply – Dendrobium Feb 28 '20 at 00:47
  • Hi @dc37, Just a quick favor, how could I denote each value on the top of its' bubble? Thank you in advance. – Dendrobium Feb 28 '20 at 11:08
  • What to do you mean ? Adding the value number of each number on top of each bubble ? or in the middle of each bubbles ? – dc37 Feb 28 '20 at 11:41
  • Yes, in the middle of each bubble how to label it’s value. – Dendrobium Feb 28 '20 at 14:35
  • I edited my answer to show you how to do that. Let me know if it is working – dc37 Feb 28 '20 at 16:50
  • Wow, great. Thanks a heap @dc37 . This is exactly what I want. Struggled whole day to do this :) it’s a very big df and everything was going jammed. Thank you again. Have a great weekend – Dendrobium Feb 28 '20 at 21:33