用颜色填充密度图

时间:2015-05-13 18:07:55

标签: r ggplot2 fill

我有这个ggplot2脚本:

require(reshape2)
library(ggplot2)
library(RColorBrewer)
library(extrafont)
font_import(pattern = 'Arch') 
font_import(pattern = 'Akk') 

fileName = paste("/ex_spectra.csv", sep = "") # data: https://www.dropbox.com/s/gs1hwv3xjnlhb7z/ex_spectra.csv?dl=0
mydata = read.csv(fileName,sep=",", header=TRUE, check.names=FALSE)
dataM = melt(mydata,c("x"))

my_palette = c(brewer.pal(5, "Set1")[c(1,2,3,4,5)])

ggplot(data=dataM, aes(x= x, y=value, colour=variable, fill = variable, size = variable)) +
geom_line(alpha = .75) +

scale_colour_manual(breaks=c("A", "B", "C", "D", "E"), values=my_palette) +
scale_size_manual(breaks=c("A", "B", "C", "D", "E"), values=c(0.7,0.7,0.7,0.7,0.7)) +

theme(plot.background = element_blank(), panel.grid.minor = element_blank(), #panel.grid.major = element_blank(),
axis.line = element_blank(),
legend.key = element_blank(), legend.title = element_blank()) +
scale_y_continuous("y", expand=c(0,0)) + 
scale_x_continuous("x", expand=c(0,0)) +

theme(axis.title.x = element_text(vjust=-0.3, face="bold", size=12, colour = "grey50", family="AkkuratPro-Regular")) + 

theme(axis.title.y = element_text(vjust=1.5, face="bold", size=12, colour = "grey50", family="AkkuratPro-Regular")) +

theme(legend.position = "right", axis.ticks = element_blank(),
  axis.text.x = element_text(size = 9, angle = 0, vjust = 0.25 , hjust = 1, colour = "grey50", family="AkkuratLightPro-Regular")
  ,axis.text.y = element_text(size = 9, angle = 0, hjust = 1, colour = "grey50", family="AkkuratLightPro-Regular"))

产生这个情节:

enter image description here

现在,我想用相应的颜色(例如,25%alpha)填充密度曲线。不是fill = variable通常的做法吗?

1 个答案:

答案 0 :(得分:5)

如果你只是担心填充线下的区域,那么你的例子就不必要了。

以下是使用geom_area和您的示例数据填充数据行下方区域的简单示例。

ggplot(data=dataM, aes(x= x, y=value, fill = variable)) +
  geom_area(alpha = .75, position="identity")

返回

enter image description here