结合ggplot geom_point和geom_line图所产生的冗余图例

时间:2014-09-18 14:40:04

标签: r ggplot2 legend

我试图用geom_point和errorbars来展示两组之间的差异,并通过geom_line链接它们,以便很容易地跟踪哪些组被链接。生成的图例自动包含2个面板,一个用于点,一个用于线。我搜索过“结合传说”和“冗余的传说”以及其他许多东西,并且已经接近一两次,但似乎无法解决我的问题。我希望有一个单一的图例显示点和线类型,但我决定放弃线条图例并显示点数。就代码而言,我是相当新手,所以如果你看到提高效率的方法,我很乐意学习。感谢您的帮助。

dat <- structure(list(pop = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), foundlost = structure(c(1L, 2L, 
1L, 2L, 1L, 2L), .Label = c("Found", "Lost"), class = "factor"), 
mean = c(3.939, -7.153, 3.001, -4.089, 4.247, -5.168), sd = c(4.394, 
9.08, 4.627, 6.644, 5.346, 6.592), Freq = c(118L, 65L, 109L, 
80L, 202L, 166L), error = c(0.792806386453774, 2.20738080265164, 
0.868629033901147, 1.4559039398977, 0.737227217957054, 1.00279288913615
), Edn = c(3.14619361354623, -9.36038080265164, 2.13237096609885, 
-5.5449039398977, 3.50977278204295, -6.17079288913615), Eup = c(4.73180638645377, 
-4.94561919734836, 3.86962903390115, -2.6330960601023, 4.98422721795705, 
-4.16520711086385)), .Names = c("pop", "foundlost", "mean", 
"sd", "Freq", "error", "Edn", "Eup"), class = "data.frame", row.names = c(NA, -6L))
dat

attach(dat)
require(ggplot2)
pd <- position_dodge(.2)


mainplot <- ggplot(data=dat, aes(x=foundlost, y=mean, ymin=Edn, ymax=Eup, linetype=NULL, group=pop)) +
geom_point(aes(shape=pop), size=4, position=pd)+
geom_errorbar(width=0.2, size=1.25, position=pd)+
geom_line(position=pd, size=1.25, aes(group=pop, linetype=pop))

phase2 <- mainplot +
scale_shape_discrete(name="", labels= c("ONE", "TWO", "TREE"))+
scale_x_discrete(expand=c(0.1,0.1)) +
labs(title="Mean standardized differences",
        y="Deviation from population mean",
        x="")

phase2 + 
theme(plot.title=element_text(size=rel(1.5)), 
panel.background=element_blank(),
axis.line=element_line(1.5),
panel.grid=element_blank(), 
axis.text=element_text(colour='black', size=14), 
axis.title.y=element_text(colour='black', size=14),
axis.ticks=element_line(colour='black', size=1),
legend.position=c(.2,.25),

complete=FALSE)

编辑最后一点:我从阅读中意识到这与我设置aes()或其他东西的方式有关,而且事实上它们不匹配,但我尝试过的一切都失败了到目前为止,我的查询是ggroup。

1 个答案:

答案 0 :(得分:1)

这里有几件与你的传说有关的事情。

首先,由于整个情节linetype = NULL中的aes参数,您得到了两个传说。删除它最初摆脱了两个传说。但随后使用scale_shape_discrete再次添加了第二个图例。如果您想以这种方式更改图例标签并且只有一个图例,则需要同时使用scale_shape_discretescale_linetype_discrete。在Cookbook for R website Legends 页面上有一个很好的例子。

我认为更改图例标签的最简单方法之一是在绘图之前在数据集中设置因子pop的级别。

levels(dat$pop) = c("ONE", "TWO", "THREE")

如果您使用组合图例,则可能需要更改图例的宽度以更好地显示线型,您可以使用guidesguide_legendkeywidth中执行此操作正如我在下面所示。

ggplot(data=dat, aes(x=foundlost, y=mean, ymin=Edn, ymax=Eup, group=pop)) +
    geom_point(aes(shape=pop), size=4, position=pd) +
    geom_errorbar(width=0.2, size=1.25, position=pd) +
    geom_line(position=pd, size=1, aes(group=pop, linetype=pop)) +
    scale_x_discrete(expand=c(0.1,0.1)) +
    labs(title="Mean standardized differences",
        y="Deviation from population mean",
        x="") + 
    theme(plot.title=element_text(size=rel(1.5)), 
         panel.background=element_blank(),
         axis.line=element_line(1.5),
         panel.grid=element_blank(), 
         axis.text=element_text(colour='black', size=14), 
         axis.title.y=element_text(colour='black', size=14),
         axis.ticks=element_line(colour='black', size=1),
         legend.position=c(.2,.25)) +
    guides(shape = guide_legend(keywidth = 2))

要删除linetype图例,请在添加show.legend = FALSE图层时使用geom_line

ggplot(data=dat, aes(x=foundlost, y=mean, ymin=Edn, ymax=Eup, group=pop)) +
    geom_point(aes(shape=pop), size=4, position=pd) +
    geom_errorbar(width=0.2, size=1.25, position=pd) +
    geom_line(position=pd, size=1, aes(group=pop, linetype=pop), show_guide = FALSE)
相关问题