Saltar al contenido

usando geom_text en el ejemplo de código ggplot2

Luego de de esta larga selección de datos dimos con la solución este enigma que tienen ciertos de nuestros usuarios. Te dejamos la solución y nuestro objetivo es resultarte de gran ayuda.

Ejemplo 1: reordenar ggplot2 geom_text

library(dplyr)
library(ggplot2)# some toy data
data <- tibble(Category = c("Baseball","Basketball","Football","Hockey"),
               n = c(45,10,25,20))# fill is provided to the initial ggplot call - labels are in correct order
ggplot(data, aes (x="", y = n, fill = Category))+
  geom_bar(width =1, stat ="identity")+
  geom_text(aes(label = paste(n,"%")),
            position = position_stack(vjust =0.5))

Ejemplo 2: reordenar ggplot2 geom_text

library(dplyr)
library(ggplot2)# some toy data
data <- tibble(Category = c("Baseball","Basketball","Football","Hockey"),
               n = c(45,10,25,20))# fill is provided to the initial ggplot call - labels are in correct order
  ggplot(data, aes (x="", y = n, fill = Category))+
    geom_bar(width =1, stat ="identity")+
    geom_text(aes(label = paste(n,"%")),
            position = position_stack(vjust =0.5))# fill is provided to geom_bar - labels are in reverse order
  ggplot(data, aes (x="", y = n))+
    geom_bar(aes(fill = Category), width =1, stat ="identity")+
    geom_text(aes(label = paste(n,"%")),
              position = position_stack(vjust =0.5))# fill is provided to aes in geom_text - yields warning, but labels are in correct order
  ggplot(data, aes (x="", y = n))+
  geom_bar(aes(fill = Category), width =1, stat ="identity")+
  geom_text(aes(fill = Category, label = paste(n,"%")),
          position = position_stack(vjust =0.5))#> Warning: Ignoring unknown aesthetics: fill# fill variable is provided to group in geom_text - labels are in correct order
  ggplot(data, aes (x="", y = n))+
    geom_bar(aes(fill = Category), width =1, stat ="identity")+
    geom_text(aes(label = paste(n,"%"), group = Category),
              position = position_stack(vjust =0.5))

Si para ti ha resultado de utilidad este post, sería de mucha ayuda si lo compartes con el resto seniors y nos ayudes a extender esta información.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)


Tags : /

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *