ggplot 不为具有相同变量和值的不同代码集提供相同的图 geom_bar
ggplot doesnt provide same plot for different sets of codes which has same variables and values using geom_bar
您好,我想找出这两组代码之间的差异,即使它们相同,它们也会给出不同的图。如果代码 2 中有任何错误,请帮助我。代码 2 没有像代码 1 那样给出确切的情节。
代码 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")
代码 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")
---- 这是对象的结构:---
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 ...
在您的第一个代码中:
总结是通过 "sex" 和 "romantic" 的分组,这给了你对 n
的不同观察
在你的第二个代码中:
正在对 "G3" 的所有值进行汇总,只剩下一个 n
值
试试这个:
q <-por %>% group_by(sex,romantic) %>%
summarise(n=mean(G3,na.rm=T))
进一步需要结合por
por<-merge(por,q,by=c("sex","romantic")
您好,我想找出这两组代码之间的差异,即使它们相同,它们也会给出不同的图。如果代码 2 中有任何错误,请帮助我。代码 2 没有像代码 1 那样给出确切的情节。
代码 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")
代码 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")
---- 这是对象的结构:---
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 ...
在您的第一个代码中: 总结是通过 "sex" 和 "romantic" 的分组,这给了你对 n
的不同观察在你的第二个代码中: 正在对 "G3" 的所有值进行汇总,只剩下一个 n
值试试这个:
q <-por %>% group_by(sex,romantic) %>%
summarise(n=mean(G3,na.rm=T))
进一步需要结合por
por<-merge(por,q,by=c("sex","romantic")