Q:

ggplot doesnt provide same plot for different sets of codes which has same variables and values using geom_bar

Hi I am trying to figure out the differences between these two set of codes which gives different plots even though both are same. Kindly help me if there is any mistake in the code 2. The code 2 is not giving the exact plot like code 1. 
CODE 1:  
por %>%  
  group_by(sex,romantic) %>% 
   summarise(n=mean(G3,na.rm=T)) %>% 
    ggplot(aes(x=sex, y=n, fill=romantic)) +    
     geom_bar(position="dodge",stat="identity") +   
      ggtitle("Romantic Relationship, Grades and Gender")

CODE 2: 
q <-summarise(por,n=mean(G3,na.rm=T)) 

ggplot(por, aes(sex, as.numeric(q), fill = romantic)) +    
  geom_bar(position="dodge",stat="identity") +
    ggtitle("Romantic Relationship,Grades and Gender")

---- here is the structure of Object: ---
str(por) 'data.frame':  649 obs. of  6 variables:   
  $ sex       : chr   "F" "F" "F" "F" ...    
  $ age       : int  18 17 15 15 16 16 16 17 15 15...   
  $ romantic  : chr  "no" "no" "no" "yes" ...    
  $ G1        : int  0 9 12 14 11 12 13 10 15 12 ...    
  $ G2        : int  11 11 13 14 13 12 12 13 16 12 ...    
  $ G3        : int  11 11 12 14 13 13 13 13 17 13 ...

A:

In your first code:
summarising is by grouping of "sex" and "romantic", which gives you different observation of n
In your second code:
summarising is being done for all values of "G3", leaving you with one single value of n
Try this:
q <-por %>% group_by(sex,romantic) %>% 
  summarise(n=mean(G3,na.rm=T))

Further it needs to be combined with por
por<-merge(por,q,by=c("sex","romantic")

