简体   繁体   English

ggplot2:删除颜色并在条形图中绘制边框

[英]ggplot2: remove colour and draw borders in bar plot

I am currently using ggplot2 to plot a barchart with too many levels in fill. 我目前正在使用ggplot2绘制填充过多级别的条形图。 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. 但是,在我的实际数据集中, length(data$value)高得多,而且我看不出不同条形之间的差异。 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 . 我将使用ggplot函数而不是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()
p <- ggplot(aes(variable, Length.1), data = data)+ geom_bar(stat="identity",colour="black", fill="white")+geom_text(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM