how to create Grouped barchart with values from table()

Hi! I want to create a grouped bar chart using the values i already have on a table function.

     1    2    3    4
D   38   21    0   10
I   40    8    5   0

this values are the amount of subjects that meet D or I condition i was trying:

library(ggplot2)

ggplot(var_datosgrupal, 
       aes(fill=JPC_VI_COND, y=JPG_VG_VINCULO, x=JPG_VG_VINCULO)) + 
  geom_bar(position="dodge", stat="identity")

but i need to show the number of subjects in y

Can you clarify what you're hoping to get? e.g., is the chart basically right but you want to add numbers (geom_text), or is the chart not showing the right thing? A reprex would help too—specifically, it would also help to see your data wrangling; it looks like you're doing a pivot but it might not be coming out quite right.

1 Like

Sorry. What i'm trying to get is to show on the ylabel the amount of subjects (n) that meet both conditions (1,2,3,4 [JPG_VG_VINCULO] by D or I[JPC_VG_COND])
is this even possible?
here is the grid with the data taken from R


So i need those 38, 21, 0 and 10 to be displayed in colour red for each "vinculo"
And 40, 8, 5, 0 in colour blue for each "vinculo"

Can you post a reproducible example of the dataset?

Where is JPG_VG_VINCULO in this?

1 Like

Hi, so here are the head and tail from the dataframe i'm working with

            JPC_VI_COND JPG_VG_VINCULO
      1           D              2
      2           D              2
      3           D              2
      4           D              2
      5           D              2
      6           D              2
      7           D              2
      8           D              2


             JPC_VI_COND JPG_VG_VINCULO
     115           I              1
     116           I              1
     117           I              1
     118           I              1
     119           I              1
     120           I              1
     121           I              1
     122           I              1

Can you just put the output of dput(head(var_datosgrupal)) here?

Sure, i hope this helps. i'm sorry im new in this so i struggle a bit posting the data.

structure(list(ID = c(1, 7, 3, 6, 8, 11), CONSENTIMIENTO = c(1,
1, 1, 1, 1, 1), EDAD = c(11, 43, 11, 41, 5, 38), GENERO = c(2,
1, 2, 2, 1, 1), FECHA.NAC = c("30/05/2006", "10/03/1974", "24/05/2006",
"05/08/1976", "28/04/2012", "27/07/1979"), JPC_VI_GRUPO = c(11,
11, 33, 33, 1, 1), JPC_VI_COND = c("D", "D", "D", "D", "D", "D"
), JPC_VI_COLOR = c("N", "V", NA, NA, "V", "N"), JPC_VI_DECISION1 = c(1,
3, 4, 4, 1, 3), JPC_VI_DECISION2 = c(0, 3, 5, 4, 2, 3), JPC_VI_BELIEF1 = c(2,
2, 6, 5, 3, 3), JPC_VI_BELIEF2 = c(1, 2, 2, 0, 4, 3), grupal = c(1,
1, 1, 1, 1, 1), JPC_VI_coment = c(NA, NA, "La mamá le dijo "sos una rata" en el 1° juego",
"La mamá le dijo "sos una rata" en el 1° juego", NA, NA), JPC_VG_GRUPO = c(1,
1, 1, 1, 2, 2), JPC_VG_COND = c("D", "D", "D", "D", "D", "D"),
JPC_VG_COLOR = c("R", "R", "A", "A", "A", "A"), JPC_VG_DECISION1 = c(2,
2, 2, 2, 2, 2), JPC_VG_DECISION2 = c(1, 1, 1, 1, 2, 2), JPC_VG_BELIEF1 = c(2,
2, 2, 2, 2, 2), JPC_VG_BELIEF2 = c(0, 0, 0, 0, 2, 2), JPG_VG_VINCULO = c(2,
2, 2, 2, 2, 2), JPG_VG_coment = c("Son parte de la misma familia (ensamblada)",
"Son parte de la misma familia (ensamblada)", "Son parte de la misma familia (ensamblada)",
"Son parte de la misma familia (ensamblada)", "Son parte de la misma familia (ensamblada)",
"Son parte de la misma familia (ensamblada)"), casos = c(3,
3, 3, 3, 3, 3), DE_GRUPO = c(NA, 3, 1, 1, 2, 2), DE_VINCULO = c(NA,
"1", "1", "1", "1", "1"), DE_T1 = c(NA, "R", "V", "V", "R",
"R"), DE_T2 = c(NA, "R", "R", "V", "V", "V"), DE_T3 = c(NA,
"V", "V", "R", "R", "R"), DE_T4 = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), DE_T5 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), DE_T6 = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), DE_COMENT = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
)), row.names = c(NA, 6L), class = "data.frame")

There might be something missing in your dput(). I couldn't get it to run properly.

You might be after something like this though.

library(tidyverse)

# count data frame
df <- var_datosgrupal %>% 
  group_by(JPC_VI_COND) %>% 
  count(JPG_VG_VINCULO)


ggplot(df, aes(JPC_VI_COND, n)) +
  geom_bar(position="dodge", stat="identity")
1 Like

Thanks! this worked!

df <- var_datosgrupal %>%
group_by(JPC_VI_COND) %>%
count(JPG_VG_VINCULO)

ggplot(df, aes(fill=JPC_VI_COND, y=n, x=JPG_VG_VINCULO)) +
geom_bar(position="dodge", stat="identity")

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.