LLES说,我需要用相同单元绘制两个数据集(“类型”),其中每种类型都有一个组A和组B。我希望组(a,b)与颜色相对应,但是我希望类型(a,b)与GEOM相对应(例如,A和B点的路径)。我该如何建立这样的情节?
library(ggplot2)
library(dplyr)
set.seed(22)
# Example data
df <- data.frame(
x = rep(1:10, 2),
y = c(runif(10, 1, 3), runif(10, 3, 5)),
pVar = rep(c("A", "B"), each = 10),
cVar = rep(c("a", "b"), 10)
) %>%
mutate(grp = paste(pVar, cVar, sep = "_"))
alpha
美学来渲染不可见的外观,然后在
scale_alpha_identity()
中构造适当的传奇。
library(ggplot2)
library(dplyr)
set.seed(22)
# Example data
df <- data.frame(
x = rep(1:10, 2),
y = c(runif(10, 1, 3), runif(10, 3, 5)),
pVar = rep(c("A", "B"), each = 10),
cVar = rep(c("a", "b"), 10)
) %>%
mutate(grp = paste(pVar, cVar, sep = "_"))
# set point size
sz <- 4
# Base plot
ggplot(df, aes(x, y, group = grp)) +
# Path for group A (cVar) with color mapping for group (cVar)
geom_path(aes(color = cVar
, alpha = as.numeric(pVar == "A")
, group = grp)
, linewidth = 1) +
# Points for group B (pVar) with fill based on cVar
geom_point(aes(fill = cVar
, alpha = as.numeric(pVar == "B")
, group = grp
)
, shape = 21
, size = sz
, color = scales::alpha("white", alpha = 0)
) +
# set up a guide for the plotting type, using the alpha scale that renders
# outside type invisible
scale_alpha_identity(breaks = c(0,1)
, labels = c("A", "B")
, guide = guide_legend(title = "type"
, override.aes = list(
linewidth = c(0.8, 0)
, shape = c(15, 21)
, linetype = c("solid", "blank")
, fill = c(NA, "grey")
, size = c(0, sz)
, alpha = c(1)
)
)
) +
theme_classic()