Geom_text with two labels in ggplot graph
Asked Answered
V

2

5

I'm struggling with an additional label that I would like to add to my ggplot graph.

Here is my data set:

Group                   Gaze direction  Counts   Duration
Expert Performers       game table      148      1262.122
Expert Performers       objects table   40       139.466
Expert Performers       other           94       371.191
Expert Performers       co-participant  166      387.228
Non-Performers          game table      223      1137.517
Non-Performers          objects table   111      369.26
Non-Performers          other           86       86.794
Non-Performers          co-participant  312      566.438

This is the code that I'm using:

ggplot(b, aes(x=Group, y=Gaze.direction))+
  geom_count(mapping=aes(color=Counts, size=Duration))+
  theme_bw()+
  theme(panel.grid.major = element_line(colour = "grey"))+scale_size(range = c(0, 8))+
  scale_colour_gradient(low = "black", high = "gray91")+
  scale_y_discrete(name ="Gaze direction") + 
  geom_text(aes(label=Counts,hjust=-1, vjust=-1))

Current plot:

The desired graph should contain the number of counts (it's already there) and also the duration in parenthesis (marked red in graph) for all data points.

Desired plot:

If there is someone, who has an idea how I can fix my code I would highly appreciate it.

Varese answered 1/8, 2017 at 12:28 Comment(0)
B
15

Please post Your data as dput() output!

You could try this:

library(ggplot2)
b <- data.frame(Group  = c("Expert Performers","Expert Performers","Expert Performers","Expert Performers","Non-Performers","Non-Performers","Non-Performers","Non-Performers"), 
                   Gaze.direction = c("game table","objects table","other","co-participant","game table","objects table","other","co-participant"), Counts = c(148,40,94,166,223,111,86,312), Duration =c(1262.122,139.466,371.191,387.228,1137.517,369.26,86.794,566.438))

ggplot(b, aes(x=Group, y=Gaze.direction))+
  geom_count(mapping=aes(color=Counts, size=Duration))+
  theme_bw()+
  theme(panel.grid.major = element_line(colour = "grey"))+scale_size(range = c(0, 8))+
  scale_colour_gradient(low = "black", high = "gray91")+
  scale_y_discrete(name ="Gaze direction") + 
  geom_text(aes(label=paste("(",Counts,",",Duration,")"),hjust=-1, vjust=-1))

I have used paste() function in geom_text(): label argument in which both of Your variable values (Counts & Duration) are implemented.

enter image description here

Brebner answered 1/8, 2017 at 12:31 Comment(2)
Sorry for the dput output. Wasn't on purpose. Next time, I'll pay more attention to it. Many thanks for the solution. It looks exactly the way I wanted to have it.Varese
The only thing is that I still miss is the ms after the duration time. Also, there is space between the brackets and the counts and in other places. Is there a way to remove this empty space?Varese
K
0

Expanding on @Mal_a's answer, using paste0() instead of paste() will remove spaces, and you can just add ,"ms" to add in the ms label.

So use something like:

geom_text(aes(label = paste0("(", Counts," ,", Duration, "ms", ")"), hjust = -1, vjust = -1))
Kickapoo answered 19/7 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.