[英]using ggplot2 to show multiple number columns in barplot?
Would anybody please help using ggplot2 in R, to show a barplot, where i need to show columns (first, second, third, fourth, fifth) on x axis and their values on y-axis ? 有人可以帮忙在R中使用ggplot2来显示小程序,我需要在x轴上显示列(第一,第二,第三,第四,第五),在y轴上显示其值吗? without showing the column "uname". 而不显示“ uname”列。
> head(golQ1Grades)
qname uname first second third fourth fifth
1 onlinelernen_quiz_1 xxx 100 0 0 0 0
2 onlinelernen_quiz_2 xxxx 100 0 0 0 0
3 onlinelernen_quiz_4 xxxx 42 71 0 0 0
4 onlinelernen_quiz_7 xxxx 85 100 0 0 0
5 onlinelernen_quiz_1 xxx 85 100 0 0 0
6 onlinelernen_quiz_3 xxxx 71 0 0 0 0
Thanks for the advanced help. 感谢您的高级帮助。
It is my guess that you would like to display the mean value on the Y-axis. 我猜想您想在Y轴上显示平均值。
library(ggplot2)
Data 数据
dat<-data.frame(c(100,100,42,85,85,71), c(0,0,71,100,100,0), c(0,0,0,0,0,0), c(0,0,0,0,0,0), c(0,0,0,0,0,0))
names(dat)<-NULL
Compute mean and get new data 计算均值并获取新数据
v1<-apply(dat, 2, mean)
nv1<-c("first","second","third", "fourth","fifth")
ndat<-data.frame(nv1, v1)
Plot 情节
p <- ggplot(ndat, aes(factor(nv1), v1))
p + geom_bar(stat="identity")
I think the better option is dplyr
and tidyr
.Eg (I change data.frame
a little) 我认为更好的选择是dplyr
和tidyr
例如(我稍微更改了data.frame
)
library(dplyr)
library(tidyr)
library(ggplot2)
df <- data.frame(qname = letters[1:10],
first = seq(1,10,1),
second = seq(10,100,10),
third = seq(2,20,2))
And then use gather
feature: 然后使用gather
功能:
df <- df %>%
gather(variable, value, -qname)
in your case it will be 在你的情况下
df <- golQ1Grades %>%
gather(variable,value, -qname, -uname)
Futhermore, instead of computing average value it is also extremely helpful facet_grid
: 此外,代替计算平均值,它对facet_grid
也非常有用:
ggplot(df, aes(factor(qname),value))+
geom_bar(stat = "identity")+
facet_grid(.~variable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.