[R] geom_text in ggplot (position)

Kristina Wolf kmwolf at ucdavis.edu
Tue May 12 21:15:37 CEST 2015


This may or may not be helpful, but you can create a dataframe with the
geom_text exactly where you want it to be (although it will probably be a
lot of extra work and will take time fiddling with it to see if you like
how it looks - and I think it looks fine the way you have it already).

Example for just one of the values, you would have to do this for each:

text75 <- data.frame(cO = factor("B", levels = c("A","B")), cat = "0",
recuento = 50, variables = factor("PE_35", levels = c("PE_35", "PE_49")))

                   ggplot(melted, aes(x = cat, y =
recuento,ymax=max(recuento)*1.05, fill =
                                        cO)) +
                     geom_bar(stat = 'identity', position = 'stack',
col="black") +
                     facet_grid(~ variables)+
                     geom_text(data = text75, label = "75", size = 10, face
= "bold")

*​~ Kristina*

​​
Kristina Wolf
​
​
Ph.D. Candidate, Graduate Group in Ecology
M.S. Soil Science
​,
​
B.S. Animal Science​
​
KristinaMWolf.com
Restoration Ecology Lab
​
Department of Plant Sciences
​
University of California, Davis​
​
(530) 750-9771

"We have to remember that what we observe is not nature herself, but nature
exposed to our method of questioning." ~ Werner Heisenberg


On Tue, May 12, 2015 at 9:50 AM, AURORA GONZALEZ VIDAL <
aurora.gonzalez2 at um.es> wrote:

> Hello everybody.
>
> I have an "esthetic" question. I have managed to create a stacked and
> grouped bar plot but I don't manage with putting the text in the middle of
> the bar plots. Do you know how to write the numbers in that position?
>
> Thank you so much.
>
> Example code:
>
> test  <- data.frame(variables =  c("PE_35", "PE_49"),
>                     value1=c(13,3),
>                     value2=c(75,31),
>                     value3=c(7,17),
>                     value4 =c(5,49))
>
> library(reshape2) # for melt
>
> melted <- melt(test, "variables")
> melted$cO <- c("A","A","B","B","A","A","B","B")
>
> melted$cat <- ''
> melted[melted$variable == 'value1' | melted$variable == 'value2',]$cat <-
> "0"
> melted[melted$variable == 'value3' | melted$variable == 'value4',]$cat <-
> "1"
>
> names(melted)[3] <- "recuento"
>
> library(ggplot2)
>
> ggplot(melted, aes(x = cat, y = recuento,ymax=max(recuento)*1.05, fill =
> cO)) +
>   geom_bar(stat = 'identity', position = 'stack', col="black") +
> facet_grid(~ variables)+
>   geom_text(aes(label = recuento), size = 5, hjust = 0.5, vjust = 1,
> position ="stack")
>
>
> ------
> Aurora González Vidal
>
> Sección Apoyo Estadístico.
> Servicio de Apoyo a la Investigación (SAI).
> Vicerrectorado de Investigación.
> Universidad de Murcia
> Edif. SACE . Campus de Espinardo.
> 30100 Murcia
>
> @. aurora.gonzalez2 at um.es
> T. 868 88 7315
> F. 868 88 7302
> www.um.es/sai
> www.um.es/ae
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list