2014-01-10 24 views
1

我想我有一个问题关于可变范围,但无法弄清楚如何解决它ggplot。本质上,我在R函数中创建一个数据框,然后使用ggplot从该数据框中调用变量。我不断收到一个错误,指出找不到对象数据帧。的R - 创造一个R函数内的数据帧,并使用该数据帧

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 

任何帮助将不胜感激!

+0

我要补充,这是指向BarValues行代码。该ggplot似乎工作,否则。我想这不是问题ggplot,但是geom_text函数是一个问题。 –

+0

在调用'geom_text(...)'你'tempDemoDF'在'AES(...)'调用。尝试'geom_text(数据= tempDemoDF,AES(Y =变量,...))' – jlhoward

+0

谢谢,但似乎并没有解决问题。我仍然有相同的错误信息。 –

回答

1

我得到这个工作,但不得不做出很多变化:

  1. 许多指出的那样,你有tempDemoDF的aes调用内部为geom_text
  2. 另外,你想要绘制为你的问题y价值不因为y工作(虽然看起来很像x这里,因为它的翻转)应该是连续
  3. 你试图使用variable作为文本y值,这没有任何意义;相反,我使用了文本的y值。
  4. 是否试着通过variable或问题的颜色?你的配色方案建议后者,但你的代码做前者(我保持原样)。

有可能是我固定的很好,但不记得其他的问题。

enter image description here

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

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)) 
+0

我真的很喜欢同一个情节中的男性和女性分组在一起,除了标签 –

+0

啊,我想我已经解决了问题。我改变了我的原代码Barvalues读取 'BarValues < - geom_text(data = tempDemoDF,aes(label = value),position = position_dodge(width = 1))' –

+0

@DevinHunt,查看更新回答 – BrodieG