用颜色填充密度图

Rod*_*phe -1 r fill ggplot2

我有这个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"))
Run Code Online (Sandbox Code Playgroud)

这产生了这个情节:

在此输入图像描述

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

MrF*_*ick 5

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

这是一个简单的示例,使用geom_area您的样本数据填充数据行下的区域.

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

返回

在此输入图像描述

  • 如果你想要这些行,只需添加`geom_line`:`+ geom_line(aes(color = variable))` (3认同)