ggplot2:删除颜色并在条形图中绘制边框 [英] ggplot2: remove colour and draw borders in bar plot

查看:1909
本文介绍了ggplot2:删除颜色并在条形图中绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用ggplot2来绘制一个条形图,其中填充的层次太多。结果,我无法确定酒吧何时结束而另一个开始。



以下是我正在执行的操作的示例数据代码。

$ b$ p
$ $ p $ 变量< -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,
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) b data <-data.frame(variable = as.factor(variable),value = as.factor(1:length(variable)),Length.1 = Length.1)

## Plots ggplot2中带有文本标签的堆叠酒吧聊天。 Credit to MYaseen208
library(ggplot2)
p < - qplot(变量,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)要高得多,我不能清楚地看到不同栏之间的差异。所以,我希望将酒吧的颜色更改为白色,并在每个酒吧周围绘制黑色边框。有人知道我该怎么做吗?

解决方案

我会使用 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()


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()

这篇关于ggplot2:删除颜色并在条形图中绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆