Label reversed Stacked Bar R

Asked

Viewed 94 times

2

The feminine/masculine colors pros generos appear correctly on the chart, however, when I add "Labels" the amount that is gen F appears below and gen M appears in the middle

p4 <- ggplot() + geom_bar(aes(y = QTD, x = SINTOMA, fill = GENERO), data = charts.data,
                           stat="identity") + theme(plot.subtitle = element_text(vjust = 1), 
    plot.caption = element_text(vjust = 1), 
    legend.title = element_text(face = "italic"), 
    panel.background = element_rect(fill = "gray91"), 
    plot.background = element_rect(fill = "gainsboro"), 
    legend.key = element_rect(fill = "gray75"), 
    legend.background = element_rect(fill = "gray72")) + theme(plot.subtitle = element_text(family = "mono", 
    size = 11, face = "bold.italic", colour = "gray2", 
    hjust = 0.5), plot.caption = element_text(size = 10, 
    hjust = 0)) +labs(subtitle = "Frequencia de Sintomas por Genero")

I believe that the ERROR is in the following section :

p4 <- p4 + geom_text(data=charts.data, aes(x = SINTOMA, y = QTD,
                     label = paste0(QTD,"")), size=4)

inserir a descrição da imagem aqui

Someone can help me, pfv?

  • 1
    1. Lay the foundation charts.data, and the mapping = aes(etc) in the call to ggplot() and remove from geom_bar. 2) Whether it already has the basis charts.data, and the mapping = aes(etc), do not repeat in geom_text. 3) geom_text(size = 4, position = position_stack(vjust = 0.5)).
  • 1

    Note: a frequent error in ggplot is to repeat the data and aes(). If they are common to geom_* always put on the original call to ggplot, argument inherit.aes = TRUE is the defect, which greatly simplifies the code and makes the results more consistent with the data. Establishes right from the beginning the elements used in the various layers of the chart.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.