我想我有一个问题关于可变范围,但无法弄清楚如何解决它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
任何帮助将不胜感激!
我要补充,这是指向BarValues行代码。该ggplot似乎工作,否则。我想这不是问题ggplot,但是geom_text函数是一个问题。 –
在调用'geom_text(...)'你'tempDemoDF'在'AES(...)'调用。尝试'geom_text(数据= tempDemoDF,AES(Y =变量,...))' – jlhoward
谢谢,但似乎并没有解决问题。我仍然有相同的错误信息。 –