0

From what I can see, geom_area and geom_line plot variables differently so the area are on top of each other (they have different y-axis start) whereas geom_line they cross and overlap continuously. Do you know why is this?

I would like one of the following:

  1. Using geom_area could I have no colour-filling for densities, and colour the lines selected variables based on “Case”?
  2. Or could I use geom_line like I have done but avoiding the crossing and overlapping of variables?

Thanks

dataA <- tibble::tibble(
          value = c(10,20,30,30,20,10,5,8,10,8,7,2,9,25,28,29,15,6),
        Sample = rep(c(1:6),3),
        Variable = rep(c(rep("C1",6),rep("C2",6),rep("C3",6))),
        Case = rep(c(rep("o",6), rep("a",6),rep("o",6))))
    #This is the geom_area graph
        p1 <- ggplot(dataA, aes(x=Sample, y=value, fill=Variable)) +
        geom_area(colour="black", size=.2, alpha=.8) +
          theme_bw()
    #This is the geom_line
        p2 <- ggplot(dataA, aes(x=Sample, y=value, color=Case, group = interaction(Variable,Case))) + 
          geom_line(colour="black") +
          geom_line(data=subset(dataA, Case == "o"), colour="green4", size=1.5)

enter image description here

4

2 回答 2

1

color改为fill在_geom_area

library(ggplot2)
dataA <- tibble::tibble(
  value = c(10,20,30,30,20,10,5,8,10,8,7,2,9,25,28,29,15,6),
  Sample = rep(c(1:6),3),
  Variable = rep(c(rep("C1",6),rep("C2",6),rep("C3",6))),
  Case = rep(c(rep("o",6), rep("a",6),rep("o",6))))

ggplot(dataA, aes(x=Sample, y=value, group = interaction(Variable,Case))) +
  geom_area(aes(colour=Case), size=.2, alpha=.8, fill = NA) +
  theme_bw() +
  scale_color_manual(values = c("o" = "green4", "a" = "red"))

reprex 包于 2020-04-20 创建(v0.3.0)

这是回答你的问题吗?

于 2020-04-20T09:24:48.773 回答
1

使用geom_area:使用group而不是fill.

ggplot(dataA, aes(x=Sample, y=value, group=Variable)) +
    geom_area(colour="black", size=.2, alpha=.8, fill="white") +
    theme_bw()

使用 geom_area 绘图

您还可以使用更改线条的颜色aes(color=Case)

于 2020-04-20T09:19:39.647 回答