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

 双木子婷_893 发布于 2023-01-30 17:28

我目前正在使用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)要高得多,我无法清楚地看到不同条形图之间的差异.因此,我希望将条形图的颜色更改为白色,并在每个条形图周围绘制黑色边框.有谁知道我怎么做到这一点?

1 个回答
  • 我会使用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()
    

    2023-01-30 17:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有