Overlay graphics in R with ggplot

4

Hello, consider two data frames:

df = the number of students who answered items A, B, C, D and E of a 6-question test

 ITENS <-c("A","B","C","D","E")
 Q.1 <-c(10,20,10,40,10)
 Q.2 <-c(5,25,0,50,10)
 Q.3 <-c(15,20,5,40,10)
 Q.4 <-c(15,30,5,30,5)
 Q.5 <-c(20,25,5,20,15)
 Q.6 <-c(10,20,10,40,10)
 df <- data.frame(ITENS,Q.1,Q.2,Q.3,Q.4,Q.5,Q.6)

df2 = is the question template

 Q.01 <-c(0,20,0,0,0)
 Q.02 <-c(5,0,0,0,0)
 Q.03 <-c(0,0,5,0,0)
 Q.04 <-c(0,0,0,30,0)
 Q.05 <-c(20,0,0,0,0)
 Q.06 <-c(0,0,10,0,0)
 df2 <- data.frame(ITENS,Q.01,Q.02,Q.03,Q.04,Q.05,Q.06)

So I create two dataframes: long and long2

 library(tidyr)
 long <- df %>% gather(turma, quantidade, Q.1:Q.6)
 long2 <- df2 %>% gather(turma, quantidade, Q.01:Q.06)

and plot the graphics of each data frame:

library(ggplot2)
ggplot(long)+geom_bar(aes(x = ITENS, y = quantidade, fill ="red"),stat = 
"identity")+facet_wrap(~turma)+guides(fill=FALSE)

ggplot(long2)+geom_bar(aes(x = ITENS, y = quantidade),stat = "identity")+          
facet_wrap(~turma) +guides(fill=FALSE)

The graphics are on separate pages, the idea was to overlap those two graphs . In such a way that in the graph of the amount of students that reponderated the items appeared the template of the question in red.

I have tried various ways of presenting the solutions presented here here and < a href="https://en.stackoverflow.com/questions/85844/sobrepor-gr%C3%A1fico-no-r"> here , but without success.

I can even overlap, but then I would have to do one chart at a time:

ggplot(data = data.frame(a = Q.1, b = Q.01, x = df$ITENS)) + geom_bar(aes(x = 
x, y = Q.1 , fill = "red"),stat = "identity") + geom_bar(aes(x = x, y = Q.01, 
fill= "blue"),stat = "identity")

Att.

    
asked by anonymous 10.05.2018 / 16:01

2 answers

5

I've made some changes to your code.

I changed the name of the variables from df2 to Q.1 , Q.2 , Q.3 , Q.4 and Q.5 .

In long2 I called the gabarito variable because it represents the number of students who answered their questions, right?

Then I joined long and long2 by ITENS and what you called turma (that's why I modified the variable names in df2 ).

Follow code and result:

ITENS <-c("A","B","C","D","E")
Q.1 <-c(10,20,10,40,10); Q.2 <-c(5,25,0,50,10); Q.3 <-c(15,20,5,40,10)
Q.4 <-c(15,30,5,30,5); Q.5 <-c(20,25,5,20,15); Q.6 <-c(10,20,10,40,10)
df <- data.frame(ITENS,Q.1,Q.2,Q.3,Q.4,Q.5,Q.6)

Q.1 <-c(0,20,0,0,0); Q.2 <-c(5,0,0,0,0); Q.3 <-c(0,0,5,0,0)
Q.4 <-c(0,0,0,30,0); Q.5 <-c(20,0,0,0,0); Q.6 <-c(0,0,10,0,0)
df2 <- data.frame(ITENS,Q.1,Q.2,Q.3,Q.4,Q.5,Q.6)

library(tidyr)
long <- df %>% gather(turma, quantidade, Q.1:Q.6)
long2 <- df2 %>% gather(turma, gabarito, Q.1:Q.6)

library(dplyr)
l <- left_join(long, long2, by = c("ITENS", "turma"))
ggplot(l, aes(x = ITENS)) +
  geom_bar(aes(y = quantidade, fill = "red"), stat = "identity", position = "identity") +
  guides(fill = F) +
  geom_bar(aes(y = gabarito, fill = "blue"), stat = "identity", position = "identity") +
  facet_wrap(~turma)

    
10.05.2018 / 17:45
2

I think I understand what you want to do.

# Vou mudar o seu df2.
df2 <- data.frame(turma = c("Q.1", "Q.2", "Q.3", "Q.4", "Q.5", "Q.6"), 
                             gabarito = factor(c("B", "A", "C", "D", "A", "C"), 
                             levels = c("A", "B", "C", "D", "E"))

# unir long e df2
library(dplyr)
long <- left_join(long, df2)

# construir o gabarito 
long <- long %>% transform(gabarito = (gabarito == ITENS))

# o grafico
library(ggplot2)
ggplot(long) + geom_bar(aes(x = ITENS, y = quantidade, fill = gabarito), stat = "identity") + facet_wrap(~turma)

I think I induced you to an inappropriate name. In place of 'class' is more appropriate 'question'.

Wow. It's almost a copy of what Rafael did. But when I started doing it, he had not posted his answer.

    
10.05.2018 / 18:11