1

I would like to make a x-y-plot that includes the labels inside the plot symbols.

My first attempt is this:

dx <- data.frame(x=c(1,2,3),y=c(3,5,8),z=c(1,10,20))
ggplot(data=dx, aes(x=x,y=y,label=as.factor(z)))+
geom_point(color="black",shape=1,size=6) + 
geom_text(hjust = 1, nudge_x = 0.04)

The result is almost ok, but the labels (z) are not centered in the plot symbol. This probably has to do with the parameters within geom_text(). What values do I have to choose so that the numbers (z) are centered in the circles?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Aki
  • 409
  • 2
  • 6
  • 15

1 Answers1

2

We can adjust using hjust and vjust (here is a good post on how to use):

ggplot(data = dx, aes(x = x, y = y, label = as.factor(z))) +
  geom_point(color = "black", shape = 1, size = 6) + 
  geom_text(hjust = 0.5, vjust = 0.5)

Or we can use geom_label (ggplot2_2.1.0), which will give us rectangles with rounded edges with labels inside.

ggplot(data = dx, aes(x = x, y = y, label = as.factor(z))) +
  geom_label()
Community
  • 1
  • 1
zx8754
  • 52,746
  • 12
  • 114
  • 209