Barplot (bar graph) of numerical versus categorical variable in R

Asked

Viewed 307 times

4

How to plot a bar graph on the R in which the eixo x I have a categorical variable (with 4 categories: frequent, sometimes rare, never) and in eixo y the average of a numeric variable (Y=0:13)? How do I insert the label above the bars?

Below is an example of the cross table of the categorical variable with the numerical variable. In the columns "frequent", "sometimes", "rare" and "never" are the number of individuals who answered one of these four options and the Y score (0:13).

  Y        Frequente  As vezes   Raro    Nunca
  0          86        70        111       69
  1         227       181        246      149
  2         341       254        417      197
  3         418       298        501      275
  4         396       316        541      257
  5         458       310        564      307
  6         387       273        562      246
  7         352       212        518      228
  8         247       167        401      207
  9         173       118        278      131
  10         88        54        140       61
  11         35        19         74       27
  12          9         7         31        8
  13          2         0          3        0

The information you should have in the bar graph is: what is the mean of Y for the individuals who answered: frequent, rare, sometimes and never.

I appreciate the help!

1 answer

3


I would do so:

library(tidyr)
library(dplyr)
library(ggplot2)

dados %>%
  gather(intensidade, qtd, -Y) %>%
  group_by(intensidade) %>%
  summarise(media = weighted.mean(Y, qtd)) %>%
  ggplot(aes(x = intensidade, y = media)) + geom_col()

Whereas data is a data.frame the same way you posted it. Don’t forget to install the packages before using. You can install them all together using install.packages('tidyverse').

The generated graph looks like this:

inserir a descrição da imagem aqui

  • I could not run - Error: Invalid column Specification - Could you detail what you assigned to "intensity" and "Qtd", please?

  • intensity and quantity are just names for the columns that are created... post the result of dput(data) so that we can look exactly as your base is. My kick that there is no column called Y...

  • I got it!! Thanks!

Browser other questions tagged

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