Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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 ggplot2:动态图例标题_R_Ggplot2_Legend - Fatal编程技术网

R ggplot2:动态图例标题

R ggplot2:动态图例标题,r,ggplot2,legend,R,Ggplot2,Legend,在我的代码中,图例的标题是自动生成的(X1,X2,…)。我如何使它如此,而不是X,它说“N=#”,其中N是在我的sappy函数中给出的 library(ggplot2) library(reshape2) PlotBinom <- function(p) { x <- c(0:500) df <- sapply(seq(50,500,by=50), function(n) dbinom(x,n,p)) df <- data.frame(x,df) mel

在我的代码中,图例的标题是自动生成的(X1,X2,…)。我如何使它如此,而不是X,它说“N=#”,其中N是在我的sappy函数中给出的

library(ggplot2)
library(reshape2)

PlotBinom <- function(p) {
  x <- c(0:500)
  df <- sapply(seq(50,500,by=50), function(n) dbinom(x,n,p))
  df <- data.frame(x,df)
  melted.df <- melt(df,id.vars='x')

  plot <- ggplot(melted.df,aes(x=x,y=value,colour=variable)) + geom_line() 

  print(plot)
}

PlotBinom(0.6)
PlotBinom(0.2)
库(ggplot2)
图书馆(E2)

PlotBinom添加一个调用,使用所需标签向量对
缩放\u颜色\u离散

PlotBinom <- function(p) {
  x <- c(0:500)
  df <- sapply(seq(50,500,by=50), function(n) dbinom(x,n,p))
  df <- data.frame(x,df)
  melted.df <- melt(df,id.vars='x')

  plot <- ggplot(melted.df,aes(x=x,y=value,colour=variable)) + geom_line()+
  scale_colour_discrete( labels= paste( "N =" ,1:length( unique(melted.df$variable) ) ) )

  print(plot)
}
PlotBinom