2013-06-28 45 views
0

这里是代码R. 当我运行下面的代码时,它没有图例。你能帮我弄清楚如何添加一个显示蓝色点和光滑的图例是阿尔茨海默症和粉红色是其他吗?谢谢。如何将图例添加到R中有两层的图中

sct1 <- ggplot(human, aes(age1)) + 
    geom_point(aes(y = mean_alzheimer), colour = "deepskyblue") + 
    geom_smooth(aes(y=mean_alzheimer), method="loess", , colour ="blue4", fill="gray") + 
    geom_point(aes(y = mean_other), colour = 'deeppink3') + 
    geom_smooth(aes(y=mean_other), method="loess", colour="red", fill="gray") 

sct2 <- sct1 + scale_x_continuous("AGE", breaks=c(38, 56, 72, 122, 270), labels=c("fetal", "infant", "child", "teen", "adult")) + 
    coord_cartesian(xlim=c (0, 270), ylim = c(-0.52,0.35))+ 
    scale_y_continuous("Expression ") + 
    scale_fill_manual(values=c("gray","black")) + 
    annotate("text", x=11, y=10, label="") + 
    theme_bw() 

optns <- theme (
    plot.title = element_text(face="bold", size=14), 
    axis.title.x = element_text(face="bold", size=12), 
    axis.title.y = element_text(face="bold", size=12, angle=90), 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    legend.position = c(0.8,0.3), 
    legend.title = element_blank(), 
    legend.text = element_text(size=12), 
    legend.key.size = unit(1.5, "lines"), 
    legend.key = element_blank() 
) 

sct2 + ggtitle ("Alzheimer") + optns 

如果我在aes()中添加颜色,情节很奇怪。颜色并不完全是我定义的。

sct1 <- ggplot(human, aes(age1)) + 
    geom_point(aes(y = mean_alzheimer, colour = "deepskyblue")) + 
    geom_smooth(aes(y=mean_alzheimer, colour ="blue4"), method="loess", fill="gray") + 
    geom_point(aes(y = mean_other, colour = 'deeppink3')) + 
    geom_smooth(aes(y=mean_other, , colour="red"), method="loess", fill="gray") 

sct2 <- sct1 + scale_x_continuous("AGE", breaks=c(38, 56, 72, 122, 270), labels=c("fetal", "infant", "child", "teen", "adult")) + 
    coord_cartesian(xlim=c (0, 270), ylim = c(-0.52,0.35))+ 
    scale_y_continuous("Expression ") + 
    scale_fill_manual(values=c("gray","black")) + 
    annotate("text", x=11, y=10, label="") + 
    theme_bw() 

optns <- theme (
    plot.title = element_text(face="bold", size=14), 
    axis.title.x = element_text(face="bold", size=12), 
    axis.title.y = element_text(face="bold", size=12, angle=90), 
    #panel.grid.major = element_blank(), 
    #panel.grid.minor = element_blank(), 
    #legend.position = c(0.5,0.5), 
    legend.title = element_blank(), 
    legend.text = element_text(size=12), 
    legend.key.size = unit(1.5, "lines"), 
    legend.key = element_blank() 
) 

sct2 + ggtitle ("Alzheimer") + optns 



dput(head(human)) 
structure(list(mean_alzheimer = c(-0.0553750613, -0.0496918235, 
-0.0532426455, -0.0560890633, -0.0403871812, -0.0445302045), 
    stder_alzheimer = c(0.0069845669, 0.0081224212, 0.0071107644, 
    0.0065500585, 0.0055839854, 0.0066005107), Sample_size_alzheimer = c(16940L, 
    16940L, 16940L, 16940L, 16940L, 16940L), mean_other = c(-0.355721986, 
    -0.3934466529, -0.3391313067, -0.2490772834, -0.2482841254, 
    -0.3119203366), Stder_other = c(0.0594056892, 0.0696467566, 
    0.0618351559, 0.0538088139, 0.0498136972, 0.0585087673), 
    sample_size_other = c(256L, 256L, 256L, 256L, 256L, 256L), 
    P_value = c(1.17158668837528e-07, 2.47600854952653e-07, 1.13229142817565e-06, 
    0.0008543643, 0.0001136282, 1.82385100638758e-07), age1 = 1:6), .Names = c("mean_alzheimer", 
"stder_alzheimer", "Sample_size_alzheimer", "mean_other", "Stder_other", 
"sample_size_other", "P_value", "age1"), row.names = c(NA, 6L 
), class = "data.frame") 
+1

你能提供一个最小可重现的例子吗?它看起来没有传说出现,因为你在'aes'之外设置了颜色。如果没有看到数据格式 –

+1

,您很难给出确切的答案,您还没有使用'aes()'映射颜色,所以ggplot2不会创建图例。这个想法是将数据融合成长格式,将颜色映射到变量,并且只有两层(一个点,一个平滑)。 – baptiste

+0

我尝试上传一张图片,但我没有足够的声望去做。抱歉。 –

回答

0
library(ggplot2) 
library(reshape) 

melted <- melt(human[c("age1", "mean_alzheimer", "mean_other")], id.vars = "age1") 

p <- ggplot(data = melted, aes(x = age1, y = value, colour = variable)) + geom_point() + geom_smooth(method = "loess") 

然后将选项添加到需要控制色彩,文字等。

相关问题