我认为我遇到了有关变量范围的问题,但无法弄清楚如何解决它。基本上我在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
非常感谢任何帮助!
答案 0 :(得分:1)
我让这个工作,但不得不做出很多改变:
aes
geom_text
的{{1}}调用中有tempDemoDF y
值,因为y
(虽然看起来像x
因为它被翻转)应该是连续的variable
y
用作文本的y
值,这没有任何意义;相反,我使用了文本variable
值的值。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))
还是按问题着色?您的配色方案建议使用后者,但您的代码会执行前者(我保留原样)。我可能还修复了其他问题,但不记得。
库( “GGPLOT2”) 库( “reshape2”) 库( “RColorBrewer”)
{{1}}