我目前正在使用ggplot2绘制条形图,其中填充的级别太多。结果,我无法判断一个栏结束而另一个开始。
这是我现在正在做的示例数据代码。
variable<-c("X1","X1","X1","X1","X1","X1","X1","X1","X1","X2","X2","X2","X2","X2","X2","X2","X2","X2","X3","X3","X3","X3","X3","X3","X3","X3","X3")
Length.1<-c(4.24,0.81,0.81,NA,NA,NA,NA,NA,NA,4.24,0.81,0.81,NA,NA,NA,NA,NA,NA,4.24,0.72,0.72,0.16,NA,NA,NA,NA,NA)
data<-data.frame(variable=as.factor(variable),value=as.factor(1:length(variable)),Length.1=Length.1)
## Plots a stacked bar chat in ggplot2 with text labels. Credit to MYaseen208
library(ggplot2)
p <- qplot(variable, Length.1, data = data, geom = "bar", fill = value, theme_set(theme_bw()))
p + geom_text(aes(label = Length.1), size = 3, hjust = 0.5, vjust = 3, position = "stack")
目前,情节看起来不错。然而,在我的实际数据集中,length(data$value)
要高得多,我无法清楚地看到不同条形图之间的差异。因此,我希望将条形图的颜色更改为白色,并在每个条形图周围绘制黑色边框。有谁知道我怎么能这样做?
答案 0 :(得分:9)
我会使用ggplot
函数而不是qplot
。然后您可以手动设置颜色并填充:
ggplot(data,aes(x=variable, y=Length.1,group=value))+
geom_bar(fill="white",color="black")+
geom_text(aes(label = Length.1), size = 3, hjust = 0.5, vjust = 3, position ="stack") +
theme_bw()
答案 1 :(得分:0)
p <- ggplot(aes(variable, Length.1), data = data)+ geom_bar(stat="identity",colour="black", fill="white")+geom_text(...)