我正试图在ggplot中创建一个图表:
...其中框的颜色由一个变量表示,框的轮廓由另一个变量表示。
假设数据的结构如下:
df<-data.frame(index=1:50,date=sample(seq(as.Date('1999/01/01'), as.Date('1999/06/01'), by="day"), 50,replace=T),
V1=sample(c("Indigenous","Import-related","Imported","Unknown"), 50,replace=T),
V2=sample(c(NA,"Zombie","Pulmonary Hemorrhage"), 50,replace=T))
我能想到的就是这样:
require(ggplot2)
#draw the histogram with fill determined by V1
p<-ggplot(data=df)+geom_histogram(aes(x=date,group=V1,fill=V1),binwidth=7,color="black",alpha=0.9)
#draw the individual boxes for each case
p1<-p+scale_fill_discrete()+geom_histogram(aes(x=date,group=index),binwidth=7,color="black",alpha=0)
#attempt to draw green boxes for one value of V2
p2<-p1+geom_histogram(aes(x=date,group=V2=="Zombie"),binwidth=7,color="green",alpha=0,size=1.2)
#attempt to draw orange boxes for the other value of V2
p3<-p2+geom_histogram(aes(x=date,group=V2=="Pulmonary Hemorrhage"),binwidth=7,color="orange",alpha=0,size=1.2)
然而,这不起作用,因为它在任何地方划定边界,我无法使用这种方法隔离个别案例,如您所见。
是否有ggplot解决方案?如果我不能做彩色框,我可以通过适当的方框上的某种文字注释来指示V2,但是然后必须为每个标签找出x和y,这样就可以让我适合好。
答案 0 :(得分:0)
我们可以这样尝试
df<-data.table(index=1:50,date=sample(seq(as.Date('1999/01/01'), as.Date('1999/06/01'), by="day"), 50,replace=T),
V1=sample(c("Indigenous","Import-related","Imported","Unknown"), 50,replace=T),
V2=sample(c(NA,"Zombie","Pulmonary Hemorrhage"), 50,replace=T))
df <- df[,.(count=.N), by = .(date,V1,V2)]
windows()
ggplot(data = df, aes(x = date, y = count, color = V2, fill = V1)) +
geom_bar(stat = "identity", position = "stack", width = 2, size = 1) +
scale_fill_manual(values=c("red4", "blue4", "green4", "blue")) +
scale_color_manual(values=c("orange", "green", "black")) +
scale_y_continuous(breaks = seq(1,3,1))