ggplot传奇顺序不匹配

时间:2016-08-15 20:40:12

标签: r ggplot2 legend

我是R / ggplot的新手,我调查了Stack {0}的similar problems,但无济于事。对于家里的ggplot专家来说,这可能是一个微不足道的问题,如果是这样,我期待快速回答!

所以这里是:我试图从存储在数据帧中的变量绘制4条曲线,使用颜色和线型进行播放。不幸的是,图例标签与曲线不匹配,这违背了传奇的整个目的。

这是图: 4 curves with mismatched labels

以下是生成它的代码:

# declare variables
alpha = 0.5
m = 500
x = seq(m)
fdr_thresh = x/m*alpha
lvl_thresh = rep(alpha,m)
pvals = sin(2*pi*x/20)
pvalsA = exp(-x/100)*cos(2*pi*x/20)

# plot
df <- data.frame(pvals=pvals, pvalsA = pvalsA, FDR = fdr_thresh, level = lvl_thresh, x=x)
p4 <- ggplot(data = df) + geom_line(aes(x=x, y=pvals,color="Pure Sinusoid",linetype="Pure Sinusoid"))
p4 <- p4 + geom_line(aes(x=x, y=pvalsA,color="Damped Sinusoid",linetype="Damped Sinusoid"))
p4 <- p4 + geom_line(aes(x=x, y=FDR,color = 'FDR', linetype='FDR')) 
p4 <- p4 + geom_line(aes(x=x, y=level,color='alpha',linetype='alpha'))  
p4 = p4+ scale_linetype_manual(name = "Significance", values=c("Pure Sinusoid"= 1,"Damped Sinusoid" = 1,"FDR" = 2,"alpha" = 3),labels=c("Pure Sinusoid", "Damped Sinusoid", "Sloping line",bquote(alpha == .(alpha))))
p4 = p4 + scale_color_manual(name = "Significance",values=c("Pure Sinusoid"= "Chocolate1","Damped Sinusoid" = 'Chartreuse4',"FDR" = "black","alpha" = "black") , labels=c("Pure Sinusoid", "Damped Sinusoid", "Sloping line",bquote(alpha == .(alpha))))
p4 <- p4 +  theme(legend.position = c(0.7, 0.8),legend.title=element_blank(),legend.key = element_rect(fill = "transparent",colour = "transparent"),legend.background = element_rect(fill=alpha('white', 0.8)))
show(p4)

我非常感谢有关如何做得更好的提示。 Matlab或Python中的相同情节是正确的,花了15分钟,所以我必须在某处做出错误的决定。 (不,选择R不是那个决定:这是我学习它的努力的一部分)

2 个答案:

答案 0 :(得分:4)

&#34;正确的方式&#34;要做到这一点,请将您的数据集融合到一个长数据框中,然后让ggplot负责为您绘制多行,而不是发出单独的geom_line()次调用...

# plot
df <- data.frame(pvals, pvalsA, FDR = fdr_thresh, level = lvl_thresh, x)

mm <- reshape2::melt(df,id.var="x")
my.labs <- c("Pure Sinusoid","Damped Sinusoid","Sloping line",
           bquote(alpha==.(alpha)))
p4 <- ggplot(data = mm, aes(x,value,colour=variable,linetype=variable)) +
    geom_line()
p4 <- p4 + scale_linetype_manual(name="Significance",values=c(1,1,2,3),
          labels=my.labs)
p4 <- p4 + scale_color_manual(name = "Significance",
                  values=c("Chocolate1",'Chartreuse4',"black","black"),
                  labels=my.labs)
p4 <- p4 +  theme(legend.position = c(0.7, 0.8),
                  legend.title=element_blank(),
                  legend.key = element_rect(fill = "transparent",
                                            colour = "transparent"),
          legend.background = element_rect(fill=alpha('white', 0.8)))
show(p4)

enter image description here

(我已经在我正在工作的R会话中设置theme_bw(),这就是为什么灰色背景/网格线丢失了......)

答案 1 :(得分:1)

问题是您在映射变量标签时添加了错误的顺序。

dp4 <- ggplot(data = df) + geom_line(aes(x=x, y=pvals,color="Pure Sinusoid",linetype="Pure Sinusoid"))
p4 <- p4 + geom_line(aes(x=x, y=pvalsA,color="Damped Sinusoid",linetype="Damped Sinusoid"))
p4 <- p4 + geom_line(aes(x=x, y=FDR,color = 'FDR', linetype='FDR')) 
p4 <- p4 + geom_line(aes(x=x, y=level,color='alpha',linetype='alpha'))  

p4 <- p4+ scale_linetype_manual(name = "Significance", values=c("Pure Sinusoid"= 1,"Damped Sinusoid" = 1,"FDR" = 2,"alpha" = 3),labels=c(bquote(alpha == .(alpha)), "Damped Sinusoid", "Sloping line","Pure Sinusoid"))

p4 <- p4 + scale_color_manual(name = "Significance",values=c("Pure Sinusoid"= "Chocolate1","Damped Sinusoid" = 'Chartreuse4',"FDR" = "black","alpha" = "black") , labels=c(bquote(alpha == .(alpha)), "Damped Sinusoid", "Sloping line","Pure Sinusoid"))

p4 <- p4 +  theme(legend.position = c(0.7, 0.8),legend.title=element_blank(),legend.key = element_rect(fill = "transparent",colour = "transparent"),legend.background = element_rect(fill=alpha('white', 0.8)))
show(p4)

enter image description here