R - 在R函数内创建数据帧并在该数据帧上使用ggplot

时间:2014-01-10 16:18:43

标签: r ggplot2

我认为我遇到了有关变量范围的问题,但无法弄清楚如何解决它。基本上我在R函数中创建一个数据帧,然后使用ggplot从该数据帧中调用变量。我一直收到一条错误,指出找不到对象数据帧。

library("ggplot2")
library("reshape2")
library("RColorBrewer")


singleColor <- brewer.pal(8, "Dark2")[1]
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
"#F0E442", "#0072B2", "#D55E00", "#CC79A7")

set.seed(42) ## for the differents sample call
posRespt <- data.frame(Section = rep(1, 11),
                      Order = 1:11,
                      Question = LETTERS[1:11],
                      Response = rep('Very Important', 11),
                      Males = sample(1:40, 11, replace = TRUE),
                      Females = sample(1:40, 11, replace = TRUE))
posRespt$Total <- with(posRespt, Males + Females)



BannerDemoPlots <- function(titleText, fname, yLabel, xLabel, DemColumns){
  temp <- subset(posRespt, select=c(1:3, DemColumns))  
  tempDemoDF <- melt(temp, id=c("Section","Order", "Question"))
  tempDemoDF <- tempDemoDF[order(tempDemoDF$Order, tempDemoDF$variable),]
  #  print(tempDemoDF)

  DemoPlots <- ggplot(data=tempDemoDF, aes(Question, value, group=variable, fill=cbPalette)) + geom_bar(stat="identity", aes(fill=variable), position="dodge") + coord_flip() + ylim(0, 100) 
  DemoTheme <- labs(title= titleText, x=xLabel, y=yLabel)
  AxisColors <- theme(axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"))
  BarValues <- geom_text(aes(data=tempDemoDF, y=variable, label = value), position = position_dodge(width=1))
  Colors <- scale_fill_manual(values=cbPalette)

  DemoPlot <- DemoPlots + DemoTheme + AxisColors + Colors + BarValues
  DemoPlot
  ggsave(filename=fname, plot=(DemoPlot), width=6.5, height=8.5, units='in', dpi=300)
  return(tempDemoDF)
}

BannerDemoPlots(titleText="Gender", xLabel='', yLabel="Percent Responding 'Very Important'", fname="/home/huntdj/Army STARRS/Programs/Banner Data Charts/EnlistmentGender.eps", DemColumns=c(6:7))

我得到的错误状态:

Error in eval(expr, envir, enclos) : object 'tempDemoDF' not found

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我让这个工作,但不得不做出很多改变:

  1. 正如许多人所指出的,aes geom_text的{​​{1}}调用中有tempDemoDF
  2. 另外,您试图将您的问题绘制为y值,因为y(虽然看起来像x因为它被翻转)应该是连续的variable
  3. 您尝试将y用作文本的y值,这没有任何意义;相反,我使用了文本variable值的值。
  4. 您是尝试按singleColor <- brewer.pal(8, "Dark2")[1] cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "red", "green", "blue") set.seed(42) ## for the differents sample call posRespt <- data.frame(Section = rep(1, 11), Order = 1:11, Question = LETTERS[1:11], Response = rep('Very Important', 11), Males = sample(1:40, 11, replace = TRUE), Females = sample(1:40, 11, replace = TRUE)) posRespt$Total <- with(posRespt, Males + Females) BannerDemoPlots <- function( titleText, fname, yLabel, xLabel, DemColumns){ temp <- subset(posRespt, select=c(1:3, DemColumns)) tempDemoDF <- melt(temp, id=c("Section","Order", "Question")) tempDemoDF <- tempDemoDF[order(tempDemoDF$Order, tempDemoDF$variable),] DemoPlots <- ggplot( data=tempDemoDF, aes(x=Question, y=value, group=variable)) + geom_bar(stat="identity", aes(fill=variable), position="dodge") + coord_flip() + ylim(0, 100) DemoTheme <- labs(title= titleText, x=xLabel, y=yLabel) AxisColors <- theme(axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black")) BarValues <- geom_text( data=tempDemoDF, aes(label = value, y=value), position = position_dodge(width=1) ) Colors <- scale_fill_manual(values=cbPalette) DemoPlot <- DemoPlots + DemoTheme + AxisColors + Colors + BarValues #ggsave(filename=fname, plot=(DemoPlot), width=6.5, height=8.5, units='in', dpi=300) print(DemoPlot) return(tempDemoDF) } BannerDemoPlots( titleText="Gender", xLabel='', yLabel="Percent Responding 'Very Important'", fname="test.eps", DemColumns=c(6:7)) 还是按问题着色?您的配色方案建议使用后者,但您的代码会执行前者(我保留原样)。
  5. 我可能还修复了其他问题,但不记得。

    enter image description here

    库( “GGPLOT2”) 库( “reshape2”) 库( “RColorBrewer”)

    {{1}}