library(plyr)
library(ggplot2)
period <- c("period 0","period 1","period 1","period 1","period 1","period 1","period 2","period 2","period 2","period 2","period 2","period 2","period 2", "period 2","period 2","period 3","period 3","period 3","period 3","period 3","period 3","period 3.5","period 3.5","period 3.5","period 4","period 4", "period 4","period 4","period 4","period 4","period 4","period 5","period 5","period 5","period 5","period 5","period 5","period 5","period 6","period 7")
teacher <- c("17","14","17","20","22","23","13","15","17","19","20","22", "23","24","25","14","17","20","22","23","24","20","22","24","13","15","16","17", "20","21","22","14","15","18","19","20","22","23","18","20")
qty <- c(2,1,2,2,1,1,4,1,1,1,1,5,4,1,1,2,13,2,2,1,1,2,1,3,4,1,1,7,2,1,1,1, 6,5,2,2,3,2,1,2)
ref_by_period_df <- data.frame(Period=period,Teacher=teacher,Qty=qty)
ref_by_period_df <- ddply(ref_by_period_df, .(Period), transform, pos = cumsum(Qty) - (0.5 * Qty))
ref_by_period <- ggplot(ref_by_period_df, aes(x=Period,y=Qty))+
geom_bar(aes(fill=Teacher),stat="identity")+
geom_text(aes(label=Qty,x=Period,y=pos),size=3)+
labs(y="Referral Count",x=NULL,title="Frequency of referrals by period by teacher")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
堆栈交换的优秀人员大家好,这些数据和代码为我提供了我想要的图形。但是,我现在想将此数据与每隔6周的并排排进行比较。我想保留堆栈,但现在我将拥有更多的并排堆栈(总共6个:H1,H2,H3,H4,H5,H6)。因此,对于期间1,我希望看到6个并排的堆栈。但是我不知道如何告诉ggplot基于新的变量字段“ Hex”并排执行。当我将position =“ dodge”放到geom_bar中时,并排获得H1,H2,但是在它的右边再加上句号0-7...。结果是不可取的。任何帮助,将不胜感激。
因此,我的新数据将如下所示:
hex1 <- c("H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1" ,"H1", "H1", "H1", "H1", "H1", "H1","H1", "H1", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2")
Period1 <- c("period 0", "period 1", "period 1", "period 1", "period 1", "period 2", "period 2", "period 2", "period 2", "period 2", "period 3", "period 3", "period 3", "period 3", "period 3", "period 3.5", "period 3.5", "period 4", "period 4", "period 4", "period 4", "period 4", "period 5", "period 5", "period 5", "period 5", "period 5", "period 5", "period 7", "period 0", "period 1", "period 2", "period 2", "period 2", "period 2" , "period 2", "period 3", "period 3", "period 3", "period 3.5", "period 4", "period 4" , "period 4", "period 5", "period 5", "period 5", "period 5", "period 6")
Teacher1 <- c("17", "14", "17", "20", "23", "13", "19", "20", "22", "23", "14" ,"17", "20", "23", "24", "22", "24", "13", "16", "17", "20", "21", "14", "15", "18", "20", "22","23", "20", "17", "22", "13", "15", "17", "24", "25", "14", "17", "22", "20", "15", "17", "22", "15" ,"18", "19", "22", "18")
Qty1 <- c(1, 1, 2, 2, 1, 3, 1, 1, 5, 4, 1, 9, 2, 1, 1, 1, 3, 4, 1, 5, 2, 1, 1, 5, 3, 2, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 2, 2, 1, 2, 1, 1, 2, 2, 2, 1)
ref_by_period_df <- data.frame(Hex=hex1,Period=Period1,Teacher=Teacher1,Qty=Qty1)
ref_by_period_df <- ddply(ref_by_period_df, .(Period), transform, pos = cumsum(Qty) - (0.5 * Qty)) #Now let's calculate midpoint
ref_by_period <- ggplot(ref_by_period_df, aes(x=Period,y=Qty))+
geom_bar(aes(Hex,fill=Teacher),stat="identity",position="dodge")+
geom_text(aes(label=Qty,x=Period,y=pos),size=3)+
labs(y="Referral Count",x=NULL,title="Frequency of referrals by period by teacher")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
#This puts H1,H2 on the left, Period 0-7 on the right. Bars appear for H1,H2, but not for the Periods. The numerical values appear for the periods, but they are not correct.
刻面在这里可以很好地工作。它看起来并不完全符合您的要求,但是应该可以传达信息。
# Data
Hex <- sample(c("H1","H2","H3","H4","H5","H6"),size = 100,replace = TRUE)
Period <- sample(c("period 0", "period 1","period 2", "period 3","period 3.5", "period 4","period 5", "period 7"),size = 100, replace = TRUE)
Teacher <- sample(c("13","14","15","16","17","18","19","20","21","22","23","24","25"), size = 100, replace = TRUE)
Qty <- sample(c(1:9), size = 100, replace = TRUE)
ref_by_period_df <- data.frame(Hex=Hex,Period=Period,Teacher=Teacher,Qty=Qty)
ref_by_period_df <- ddply(ref_by_period_df, .(Period), transform, pos = cumsum(Qty) - (0.5 * Qty)) #Now let's calculate midpoint
# Plotting
ggplot(ref_by_period_df, aes(x = Hex, y = Qty, fill = Teacher)) +
geom_bar(stat="identity") +
facet_grid(.~Period) +
theme(axis.text.x = element_text(angle = 90, size = 8))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句