包括来自不同数据帧的数据,组合不同的geom以及标识数据源的图例

时间:2016-12-01 14:39:22

标签: r ggplot2

我有三个数据框,包含相同变量的数据(xy,按变量case分组)但每个数据框包含来自不同来源的数据({{1} },testsim)。 modelcase的{​​{1}}级别相同,但test的级别不同。对于model的每个值,我希望来自不同来源但具有相同sim的所有xy曲线具有相同的颜色。我需要有一个清晰标识数据源的图例,但我也想为不同的数据源使用不同的geom。这就是我能够做到的:

case

enter image description here

这里的问题是难以辨别caserm(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

enter image description here

但是,现在我在图例中没有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) 曲线在图例中清晰标记,但也可以通过modelmodel曲线直观地识别它们?

编辑 Procrastinatus Maximus建议编辑Pierre Lafortune的代码,该代码应该消除sim标签和test图例之间的空格,但显然它消除了而是modeltype图例之间的空格:

model

结果是

enter image description here

2 个答案:

答案 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'))

enter image description here

答案 1 :(得分:0)

就个人而言,我认为您需要做的就是改变类型的顺序,以便实线位于中间。如果你让背景变白并且线条颜色更亮一点,我认为你的数字很清楚:

(p <- ggplot(data = foobar, aes(x = x, y = y, color = case)) + 
  geom_line(aes(linetype = rev(type)), size = 1) +
  scale_color_manual(values = c("black","green","blue","purple","pink","red","brown"))+
  theme_bw())

enter image description here