I am currently using ggplot2 to plot a barchart with too many levels in fill. As a result, I am unable tell when a bar ends and the other begin.
Here's my sample data code of what I am doing right now.
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")
Currently, the plot looks fine. However in my actual data set, length(data$value)
is much higher and I can't see the differences between different bars as clearly. So, I wish to change the colours of the bars to white and draw a black border around each one. Does anyone know how I can do that?
I would use the ggplot
function instead of qplot
. Then you can set the color and fill manually:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With