Overlay graphics in R with ggplot

Asked

Viewed 817 times

5

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 feedback of the questions

 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 graphs 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,a idea was to overlay these two graphics. In such a way that in the graph of the number of students who reported the items appeared the feedback of the question in red.

I have tried in various ways the solutions put forward here and here, but without success.

I can even superimpose, but then I’d have to make one graph 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.

2 answers

6


I made some changes to your code.

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

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

Then I joined long and long2 for ITENS and what you called turma (so I changed the names of the variables into 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)

Imgur

3

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. Instead of 'class' is more appropriate 'question''.

Wow. It’s practically a copy of what Rafael did. But when I started doing it, he hadn’t published his answer.

Browser other questions tagged

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