我有三个数据框,包含相同变量的数据(x
和y
,按变量case
分组)但每个数据框包含来自不同来源的数据({{1} },test
和sim
)。 model
和case
的{{1}}级别相同,但test
的级别不同。对于model
的每个值,我希望来自不同来源但具有相同sim
的所有xy曲线具有相同的颜色。我需要有一个清晰标识数据源的图例,但我也想为不同的数据源使用不同的geom。这就是我能够做到的:
case
这里的问题是难以辨别case
和rm(list=ls())
gc()
graphics.off()
library(ggplot2)
# build the dataframes
nx <- 10
x1 <- seq(0, 1, len = nx)
x2 <- x1+ 0.1
x3 <- x2+ 0.1
x4 <- x3+ 0.1
x <- c(x1, x2, x3, x4)
y1 <- 1 - x1
y2 <- 1.1 * y1
y3 <- 1.1 * y2
y4 <- 1.1 * y3
y <- c(y1, y2, y3, y4)
z1 <- (y1 + y2)/2
z2 <- (y2 + y3)/2
z3 <- (y3 + y4)/2
z4 <- (y4 + 1.1 * y4)/2
z <- c(z1, z2, z3, z4)
w <- y*1.01
case_y <- c("I-26_1", "I00", "I20_5", "I40_9")
case_z <- c("I-23_6", "I00", "I22_4", "I42_3")
case_y <- rep(case_y, each = nx)
case_z <- rep(case_z, each = nx)
foo <- data.frame(x = x, y = z, case = case_z, type = "test")
bar <- data.frame(x = x, y = y, case = case_y, type = "sim")
mod <- data.frame(x = x, y = w, case = case_z, type = "model")
# different data frames have different factor levels: to avoid this,
# I bind all dataframes and I reorder the levels of case
foobar <- rbind(foo, bar, mod)
case_levels <- c("I-26_1", "I-23_6", "I00", "I20_5", "I22_4", "I40_9", "I42_3")
foobar$case <- factor(foobar$case, levels = case_levels)
# now I can plot the resulting dataframe
p <- ggplot(data = foobar, aes(x = x, y = y, color = case)) +
geom_line(aes(linetype = type), size = 1)
p
。为了使图表更具可读性,我为sim
数据切换到model
:
geom_point
但是,现在我在图例中没有model
标签。如何确保foobar <- rbind(foo, bar)
case_levels <- c("I-26_1", "I-23_6", "I00", "I20_5", "I22_4", "I40_9", "I42_3")
foobar$case <- factor(foobar$case, levels = case_levels)
mod$case <- factor(mod$case, levels = case_levels)
# now I can plot the resulting dataframe
p <- ggplot(data = foobar, aes(x = x, y = y, color = case)) +
geom_line(aes(linetype = type), size = 1) +
geom_point(data = mod)
曲线在图例中清晰标记,但也可以通过model
和model
曲线直观地识别它们?
编辑 Procrastinatus Maximus建议编辑Pierre Lafortune的代码,该代码应该消除sim
标签和test
图例之间的空格,但显然它消除了而是model
和type
图例之间的空格:
model
结果是
答案 0 :(得分:2)
这会让您更接近目标。我将看看是否可以缩小两个传说之间的差距。
ggplot(data = foobar, aes(x = x, y = y, color = case)) +
geom_line(aes(linetype = type), size = 1) +
geom_point(data = mod, aes(shape=type)) +
scale_shape_discrete(name="") +
guides(colour = guide_legend(override.aes = list(linetype=c(1),
shape=c(NA))))
修改强>
#@ProcrastinatusMaximus
ggplot(data = foobar, aes(x = x, y = y, color = case)) +
geom_line(aes(linetype = type), size = 1) +
geom_point(data = mod, aes(shape = type)) +
guides(color = guide_legend(override.aes = list(linetype = c(1), shape = c(NA)), order = 1),
linetype = guide_legend(order = 2),
shape = guide_legend(title = NULL, order = 3))+
theme(legend.margin = margin(0,0,0,0), legend.spacing = unit(0, 'lines'))
答案 1 :(得分:0)