Paschalis
Paschalis

Reputation: 12321

geom_text positions per group

I am using geom_line, geom_point, and geom_text to plot something like the picture below:

enter image description here

I am grouping, and coloring my data frame, but I want the geom_text not to be so close to each other. I want to put the one text on top, and the other on bottom. Or at least, hide the one of the two. Is there any way I can do this?

Upvotes: 1

Views: 4850

Answers (1)

user2034412
user2034412

Reputation: 4282

You can specify custom aesthetics in different geom_text() calls. You can include only a subset of the data (such as just one group) in each call, and give each geom_text() a custom hjust or vjust value for each subset.

ggplot(dat, aes(x, y, group=mygroups, color=mygroups, label=mylabel)) +
geom_point() + 
geom_line() + 
geom_text(data=dat[dat$mygroups=='group1',], aes(vjust=1)) + 
geom_text(data=dat[dat$mygroups=='group2',], aes(vjust=-1))

Upvotes: 6

Related Questions