2017-05-04 69 views
1

我想创建一个看起来像this的图。经过一番思考,我创建了一个虚拟数据帧,我认为可能是有点类似于我需要什么:R和ggplot2:如何创建一个包含三个元素的图形?

dum = structure(list(pan = c(4000, 5000, 6000, 7000, 8000, 9000), core = c(1000, 2000, 3000, 2500, 2600, 2700), Group = c("Bac1", "Bac2", "Bac3", "Bac4", "Bac5", "Bac6"), Strain = c(1000L, 50L, 25L, 10L, 25L, 10L)), .Names = c("pan", "core", "Group", "Strain"), row.names = c(NA, -6L), class = "data.frame") 

我一直在使用这种代码:

ggplot(dum, aes(x=Group,y = Strain)) + 
    geom_bar(stat = "identity") + 
    geom_line(aes(y = pan, colour = "pan")) + 
    geom_line(aes(y = core, colour = "core")) 

将会产生这种不正确的graph

这里怎么回事?我不明白为什么图表的核心元素没有显示出来。他们是否被geom_bar命令否定?

+0

喂@ E.O。如果解决方案是好的,你能标记它吗?谢谢。 – Umberto

回答

4

您必须将group=1添加到aes中。

ggplot(dum, aes(x=Group,y = Strain, group = 1)) + 
    geom_bar(stat = "identity") + 
    geom_line(aes(y = pan, colour = "pan")) + 
    geom_line(aes(y = core, colour = "core")) 

result

的数据点必须进行分组,所以它知道这点连接。(关于线图)

+0

谢谢,这很完美。 –

+0

非常感谢。 :) –

相关问题