r - Plot geom_area with no filling and colouring lines by variable/ or geom_line without variables overlapping
问题描述
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:
- Using geom_area could I have no colour-filling for densities, and colour the lines selected variables based on “Case”?
- 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)
解决方案
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)
这是回答你的问题吗?
推荐阅读
- sql - Big Query 在最近日期加入
- javascript - 有没有更好的方法来(取消)选择 HighCharts LineChart 中的所有 LegendItems?
- rabbitmq - 如何从外部秘密设置兔子密码?
- c - 函数中的 Free() 函数使我的整个程序在 c 中崩溃
- sql - 空数组在 PostgreSQL 中是否等于 NULL,还是里面有 NULL 的数组?
- python - 合并具有不同键列的两个表
- spring-boot - Liquibase 无法正确启动
- jdbc - 带有 Cosmos db 的石英
- python-3.x - 从列表中删除 \n
- linux - 即使在 Linux 中没有活动客户端也可以继续执行