Geom_text writing all data on all faces

I used ggplot with facet_grid, and I would like to indicate on each face the number of observations in each face. I follow the examples presented on many sites, but when I get it to write something, it records all four observation numbers on top of each other on all four graphs.

Here's the geom_text command: geom_text (data = ldata, aes (x = xpos, y = ypos, label = lab, size = 1), group = NULL, hjust = 0, parse = FALSE)

and ldata is a data frame that lists the coordinates (xpos, ypos) on each graph and the number of observations (laboratory). It prints the numbers in the correct position on the graph, but all four are written on top of each other on all four graphs. I cannot understand what I am doing wrong.

ldata:

xpos ypos lab

1 10 1.35 378

2 10 1.35 2

3 10 1.35 50

4 10 1.35 26

+7
overwrite r ggplot2 facets
source share
2 answers

You almost have it. You just need one more column in the ldata data frame that you will give facet_grid . (I changed Ypos to Inf)

Note the role of the splitter column in ldata below and how it is used in facet_grid

 xpos <- c(10,10,10) ypos <- c(Inf,Inf,Inf) lab <- c(378,2,50) splitter <- c(1:3) ldata <- data.frame(xpos, ypos, lab, splitter) ggplot(mtcars) + geom_bar(aes(x=cyl)) + facet_grid(~splitter) + geom_text(data=ldata, aes(x=xpos, y=ypos, label=lab, size=1), vjust=2, parse=FALSE) 

What produces:

enter image description here

+10
source share

The question could not be posted, but have an idea why the splitter function will not work if the label is a character or a number? I tried to get around, but I can’t decide ...

ex.

xpos <- c (16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22)

ypos <- 13.5

Laboratory <- c ("a", "a", "b", "b", "bc", "bc", "bcd", "c", "d", "e", "e", e "," e "," f "," f "," fg "," fg "," fgh "," fgh "," h "," gh "),

splitter <- c (1: 3)

ldata <- data.frame (xpos, ypos, lab, splitter)

0
source share

All Articles