如何将图例添加到R中具有两个图层的图中

时间:2013-06-28 17:15:00

标签: r ggplot2 scale legend

这是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 个答案:

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

然后根据需要添加控制颜色,文本等的选项。