绘制geom_area,不使用变量填充和/或着色线/或geom_line,不重叠变量

问题描述 投票:0回答:3

据我所知,geom_area和geom_line绘制变量的方式不同,因此区域彼此重叠(它们的y轴起点不同),而geom_line则交叉且重叠。你知道为什么吗?

我想要以下之一:

  1. 使用geom_area,我可以不填充颜色吗?根据“案例”为选定的变量上色行?
  2. 或者我可以像以前一样使用geom_line但要避免变量的交叉和重叠吗?

谢谢

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

r ggplot2 geom-area
3个回答
0
投票

我发现您可以使用position = stack绘制geom_line,但是,当按变量添加颜色时,它会增加一条额外的线吗?我有3个变量,但显示4个?

ggplot(dataA, aes(x=Sample, y=value, color=Case, group = interaction(Variable,Case))) + 
  geom_line(colour="black", position = "stack") +
  geom_line(data=subset(dataA, Case == "o"), colour="green4", size=1.5)

enter image description here


0
投票

强调文本使用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()

plot using geom_area

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


0
投票

使用color代替fill中的geom_area

© www.soinside.com 2019 - 2024. All rights reserved.