绘制不同类型的条形图 ggplot

3
我正在尝试绘制这个数据的柱形图。
到目前为止,我编写的R脚本如下:
library(ggplot2)
f<-read.table("Coverage_test", sep="\t", header=TRUE)
f$Coverage <- factor(f$Coverage, levels=unique(as.character(f$Coverage)))
g = ggplot(data=f, aes(x=Coverage, y=Variable_counts, group=Form, fill=Type)) 
+ geom_bar(position="dodge", stat="identity", colour="black") 
+ facet_grid( ~ Sample_name, scales="free") + opts(title = "Coverage", axis.text.x = theme_text(angle = 90, hjust = 1, size = 8, colour = "grey50")) 
+ ylab("Number of variables") + scale_fill_hue() + scale_y_continuous(formatter="comma")
ggsave("Figure_test_coverage.pdf")

这段代码的输出结果如下:

enter image description here

我的问题是: 是否有一种方式可以显示基于图形的两个变量之间的行为差异。每个x轴变量都有四个柱形条。我已经选择按“类型”填充颜色,这显示了数据中不同的“类型”(一个变量)的行为差异。但我还想显示变量“表格”在我的数据中的行为。我已经在我的代码中将它们分组为“group=Form”,但无法在实际图表中区分它(视觉上)。这可以通过在线图中完成,方法是使用不同的颜色表示一个变量,使用不同的线型(实线和虚线)表示另一个变量。类似下面这样: enter image description here
我想知道是否可以通过不同的颜色显示“表格”变量,或者至少可以在各自的柱形下方命名它们,或者任何可能的东西? 非常感谢您的帮助。
1个回答

7
我认为你想要的是这样的内容:

我想您需要类似于这样的东西:

ggplot(data=dat, aes(x=Coverage, 
                        y=Variable_counts, 
                         group=interaction(Form,Type), 
                         fill=interaction(Form,Type))) +
 geom_bar(position="dodge", stat="identity", colour="black")

输入图像描述

编辑

这里我使用lattice包来绘制条形图和智能公式符号。为了有趣,我使用latticeExtra包来创建类似ggplot的主题。

library(latticeExtra)
barchart(Variable_counts~Coverage|Sample_name,
         groups=interaction(Type,Form),
         data=dat,stack=F,auto.key=list(columns = 4),
         par.settings = ggplot2like(),
         axis = axis.grid,
         between=list(x=2))

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接