Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用颜色填充密度图_R_Ggplot2_Fill - Fatal编程技术网

R 用颜色填充密度图

R 用颜色填充密度图,r,ggplot2,fill,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/e

我有这个
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"))
它生成了这个图:


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

如果您只关心填充线下的区域,那么您的示例就不必要地复杂了

下面是一个使用
geom_area
和示例数据填充数据行下区域的简单示例

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


请提供您的数据样本。您需要
geom_区域
这似乎工作正常:
ggplot(data.frame(x=runif(100),g=rep(c(“a”,“b”),每个=50))+geom_密度(aes(x=x,fill=g),alpha=0.25)
。请在代码中定义所有变量,使示例中的绘图不是密度绘图。这是一个直线图(
geom\u line
),其中直线穿过每个(x,y)点(x=特征值,y=值)
geom_line
不承认填充美学,因为从geom_line的角度看,没有什么可填充的。如@zx8754所述,使用
geom_区域
填充线图。此外,如果希望所有线宽都相同,则不需要尺寸美学。只需在
geom_line
中添加
size=0.7
lwd=0.7
,非常感谢,事实上,这并不是一个每说一次的密度图(尽管它绘制密度数据,内置于python脚本中)。我用一个数据集更新了这个问题,它应该会变得更清晰。干杯酷,几乎就是我要找的。事实上,我也需要台词,同时保持同样的传奇。如果这真的不可能,那么您当前的答案非常接近。谢谢如果您也需要这些行,只需将
geom_行
添加回:
+geom_行(aes(color=variable))