R和ggplot2:如何创建包含三个元素的图形?

R和ggplot2:如何创建包含三个元素的图形?,r,ggplot2,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,

我正在尝试创建一个类似的图形。经过一番思考,我创建了一个虚拟数据框,我认为它可能与我需要的有点类似:

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"))
这就产生了不正确的结果


这里出了什么问题?我不明白为什么图中的pan和核心元素没有显示出来。它们是否被
geom\u bar
命令否定?

您必须将
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"))


数据点必须分组,以便它知道要连接哪些点。(关于线图)

您必须将
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"))


数据点必须分组,以便它知道要连接哪些点。(关于线图)

谢谢,这很完美。谢谢,这很完美。您好@E.O。如果解决方案可行,请您将其标记为这样好吗?谢谢。您好@E.O。如果解决方案是好的,请您将其标记为好吗?谢谢。