Trying to print indvidual plots outside loop in R - r

I am breaking my data frames into 3 other data frames. I am iterating through each of them and plot histogram based on it. I am storing these plots into separate ones. However, outside the loop, I am able to print only the plot from the last iteration and not the first two. I am able to print all three plots inside the loop though. Here is my code:
catCust1 <- myData[(myData$meanVal > 0 & myData$meanVal <= 20),]
catCust2 <- myData[(myData$meanVal > 20 & myData$meanVal <= 40),]
catCust3 <- myData[(myData$meanVal > 40 & myData$meanVal <= 60),]
for(i in 1:3) {
if(i == 1) {
catCust <- catCust1
} else if(i == 2) {
catCust <- catCust2
} else if(i == 3) {
catCust <- catCust3
}
catCust <- na.omit(catCust)
numOrdersCatCust <- ddply(catCust, .(ORDERDATE), nrow)
numOrdersCatCust$numDay <- 1:nrow(numOrdersCatCust)
catCust$orderCount <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 2]
catCust$numDay <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 3]
setDT(catCust)[, uniqueCounter := .GRP, by = CUSTOMERID]
apply(catCust, 2, function(x)length(unique(x)))
threshold25catOne <- catCust[(catCust$uniqueCounter == as.integer(0.25 * max(uniqueCounter))), ]
threshold25catOneVal <- threshold25catOne$numDay
catCustPlot <- ggplot(data=catCust, aes(catCust$numDay)) +
geom_histogram(binwidth = 1, col="black", fill="white", alpha=0.1) +
labs(x="Day Number (Since 01-09-2016)", y="Orders") +
ggtitle("GRAPH TITLE") +
theme(plot.title = element_text(hjust = 0.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
geom_vline(xintercept = threshold25catOneVal[1], color="purple") +
annotate("text", x = threshold25catOneVal[1]-7, y = max(catCust$orderCount) + 1000, angle = 0, label = threshold25catOneVal[1], vjust = 1.2, parse = TRUE)
# ABLE TO PRINT DIFFERENT PLOTS HERE
if(i == 1) {
catCustPlot1 <- catCustPlot
print(catCustPlot1)
} else if(i == 2) {
catCustPlot2 <- catCustPlot
print(catCustPlot2)
} else if(i == 3) {
catCustPlot3 <- catCustPlot
print(catCustPlot3)
}
}
# PRINTS ONLY catCustPlot3
print(catCustPlot1)
print(catCustPlot2)
print(catCustPlot3)
The other two plots gives me an error:
Error: Aesthetics must be either length 1 or the same as the data
UPDATE: head(myData)
meanVal sumVal countCat
75.98 75.98000 (60,80]
36.37 80.55727 (80,100]
50.96 52.67500 (40,60]
15.33 15.33000 (0,20]
17.48 27.65000 (20,40]
51.35 101.64900 (100,1e+04]

I just simplified you loop and stored ggplot objects to resPlots (no printing out). When loop finishes you can access/print them.
myData <- na.omit(myData)
resPlots <- list()
for(i in 1:3) {
if (i == 1) {
catCust <- myData[(myData$meanVal > 0 & myData$meanVal <= 20), ]
} else if (i == 2) {
catCust <- myData[(myData$meanVal > 20 & myData$meanVal <= 40), ]
} else if (i == 3) {
catCust <- myData[(myData$meanVal > 40 & myData$meanVal <= 60), ]
}
numOrdersCatCust <- ddply(catCust, .(ORDERDATE), nrow)
numOrdersCatCust$numDay <- 1:nrow(numOrdersCatCust)
catCust$orderCount <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 2]
catCust$numDay <- numOrdersCatCust[match(catCust$ORDERDATE, numOrdersCatCust$ORDERDATE), 3]
setDT(catCust)[, uniqueCounter := .GRP, by = CUSTOMERID]
apply(catCust, 2, function(x)length(unique(x)))
threshold25catOne <- catCust[(catCust$uniqueCounter == as.integer(0.25 * max(uniqueCounter))), ]
threshold25catOneVal <- threshold25catOne$numDay
resPlots[[i]] <- ggplot(catCust, aes(catCust$numDay)) +
geom_histogram(binwidth = 1, col = "black", fill = "white", alpha = 0.1) +
labs(x="Day Number (Since 01-09-2016)", y="Orders") +
ggtitle("GRAPH TITLE") +
theme(plot.title = element_text(hjust = 0.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
geom_vline(xintercept = threshold25catOneVal[1], color="purple") +
annotate("text", x = threshold25catOneVal[1]-7, y = max(catCust$orderCount) + 1000, angle = 0, label = threshold25catOneVal[1], vjust = 1.2, parse = TRUE)
}
# First plot
resPlots[[1]]

Related

How to position a common label for multiple plots using gtable in ggplot in R?

I have been attempting to solve this issue for a considerable amount of time with no success. I am creating multiple partial dependence plots (PDPs) and utilising a package called zenplots to lay them out. However, the issue I am having is I cannot figure out a way to have a common legend for the multiple plots. I have tried placing them on a grid and plotting and tried changing the positioning of the grobs... but I cant figure it out. For example:
In the above plot, all PDPs are on the same scale and I would like a single legend. Currently, when I produce the image, it plots a legend for each individual plot. Whereas, what I want is something like the image below (which I made in photoshop):
The code Im providing to produce the plots is somewhat long ( which I hope won't deter people)... but essentially it's only the ggplot part of the code that I need to manipulate. That is, Im creating the actual ggplot on lines 103-105 and more generally between lines 103-125, where I use ggtable to build the plots. For example, changing the color argument on line 115 to: guides(fill = FALSE, color = "colour bar") will create the legend for each plot... setting color = FALSE will remove the legends.
below is the code used to make the plots and it's application on the air quality data:
library(randomForest)
library(ggplot2)
library(dplyr)
pdpLayout <- function(data,
fit,
response,
pal = rev(RColorBrewer::brewer.pal(11, "RdYlBu")),
gridSize = 10,
nmax = 500,
class = 1,
rug = TRUE,
...) {
data <- na.omit(data)
# if (is.numeric(nmax) && nmax < nrow(data)) {
# data <- data[sample(nrow(data), nmax), , drop = FALSE]
# }
gridSize <- min(gridSize, nmax)
predData <- predict(fit, data)
vars <- names(data)
vars <- vars[-match(response, vars)]
datap <- data[,vars]
zpath <- 1:length(vars)
zdata <- datap
zpairs <- t(sapply(1:(length(zpath)-1), function(i){
z <- zpath[i:(i+1)]
if (i %% 2 == 0) rev(z) else z
}))
zpairs <- cbind(vars[zpairs[, 1]], vars[zpairs[, 2]])
# loop through vars and create a list of pdps for each pair
pdplist <- vector("list", nrow(zpairs))
for (i in 1:nrow(zpairs)) {
ind <- zpairs[i, ]
if (!is.na(ind[1])) {
px <- pdp_data(data, ind, gridsize = gridSize)
px$.pid <- i
pdplist[[i]] <- px
} else {
pdplist[[i]] <- NULL
}
}
pdplist <- bind_rows(pdplist)
pdplist$fit <- predict(fit, pdplist)
pdplist <- split(pdplist, pdplist$.pid)
pdplist0 <- vector("list", nrow(zpairs))
j <- 1
for (i in 1:nrow(zpairs)) {
ind <- zpairs[i, ]
if (!is.na(ind[1])) {
pdplist0[[i]] <- pdplist[[j]] %>%
group_by(.data[[ind[1]]], .data[[ind[2]]]) %>%
summarise(fit = mean(fit))
j <- j + 1
} else {
pdplist0[[i]] <- NULL
}
}
pdplist <- pdplist0
pdplist0 <- NULL
names(pdplist) <- paste(zpairs[, 2], zpairs[, 1], sep = "pp")
message("Finished ice/pdp")
# Set limits for pairs
pdplist0 <- pdplist[!sapply(pdplist, is.null)]
r <- range(sapply(pdplist0, function(x) range(x$fit)))
limits <- range(labeling::rpretty(r[1], r[2]))
# Zenplot graphing function
data$pred <- predData
z2index <- 0
pdpnn <- function(zargs) {
z2index <<- z2index + 1
vars <- zpairs[z2index, ]
pdp <- pdplist[[z2index]]
if (!is.null(pdp)) {
if (is.factor(pdp[[vars[1]]]) + is.factor(pdp[[vars[2]]]) == 1) {
if (is.factor(pdp[[vars[1]]])) vars <- rev(vars)
p <- ggplot(data = pdp, aes(x = .data[[vars[1]]], y = fit, color = .data[[vars[2]]])) +
geom_line() +
geom_rug(data = data, sides = "b", aes(y = .data[["pred"]]))
} else {
if (is.factor(pdp[[vars[1]]])) posx <- "jitter" else posx <- "identity"
if (is.factor(pdp[[vars[2]]])) posy <- "jitter" else posy <- "identity"
p <- ggplot(data = pdp, aes(x = .data[[vars[1]]], y = .data[[vars[2]]])) +
geom_tile(aes(fill = fit)) +
scale_fill_gradientn(name = "y-hat", colors = pal, limits = limits, oob = scales::squish)
if (rug) {
p <- p +
geom_rug(data = data, sides = "b", position = posx, aes(color = .data[["pred"]])) +
geom_rug(data = data, sides = "l", position = posy, aes(color = .data[["pred"]])) +
scale_color_gradientn(name = "y-hat", colors = pal, limits = limits, oob = scales::squish)
}
}
p <- p +
guides(fill = FALSE, color = FALSE) +
theme_bw() +
theme(
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_rect(colour = "gray", fill = NA, size = 1.5)
)
} else {
p <- ggplot() +
theme(panel.background = element_blank())
}
ggplot_gtable(ggplot_build(p))
}
suppressMessages({
zenplots::zenplot(zdata,
pkg = "grid", labs = list(group = NULL),
plot2d = pdpnn, ...
)
})
}
pdp_data <- function(d, var, gridsize = 30) {
if (length(var) == 1) {
pdpvar <- d[[var]]
if (is.factor(pdpvar)) {
gridvals <- levels(pdpvar)
} else {
gridvals <- seq(min(pdpvar, na.rm = T), max(pdpvar, na.rm = T), length.out = gridsize)
}
dnew <- do.call(rbind, lapply(gridvals, function(i) {
d1 <- d
d1[[var]] <- i
d1
}))
if (is.factor(pdpvar)) dnew[[var]] <- factor(dnew[[var]], levels = levels(pdpvar), ordered = is.ordered(pdpvar))
}
else {
pdpvar1 <- d[[var[1]]]
pdpvar2 <- d[[var[2]]]
if (is.factor(pdpvar1)) {
gridvals1 <- levels(pdpvar1)
} else {
gridvals1 <- seq(min(pdpvar1, na.rm = T), max(pdpvar1, na.rm = T), length.out = gridsize)
}
if (is.factor(pdpvar2)) {
gridvals2 <- levels(pdpvar2)
} else {
gridvals2 <- seq(min(pdpvar2, na.rm = T), max(pdpvar2, na.rm = T), length.out = gridsize)
}
gridvals <- expand.grid(gridvals1, gridvals2)
dnew <- do.call(rbind, lapply(1:nrow(gridvals), function(i) {
d1 <- d
d1[[var[1]]] <- gridvals[i, 1]
d1[[var[2]]] <- gridvals[i, 2]
d1
}))
if (is.factor(pdpvar1)) dnew[[var[1]]] <- factor(dnew[[var[1]]], levels = levels(pdpvar1), ordered = is.ordered(pdpvar1))
if (is.factor(pdpvar2)) dnew[[var[2]]] <- factor(dnew[[var[2]]], levels = levels(pdpvar2), ordered = is.ordered(pdpvar2))
}
dnew$.id <- 1:nrow(d)
rownames(dnew) <- NULL
dnew
}
Now use some data to create the plots:
aq <- na.omit(airquality)
rf <- randomForest(Ozone~., data = aq)
pdpLayout(aq, rf, "Ozone")
Any help or suggestions is greatly appreciated.

Mark start point and end point in ggplot

I'm making a plot of 2D random walk using R and ggplot2 library. It works, but I would like to show where the starting point and ending point are in my random walk plot.
I tried to create another geom_point and append it to the existing ggplot but it did not work. Any suggestions? Thanks!
x = 0
y = 0
vec1 <- vector()
xcor <- vector()
ycor <- vector()
number = 1000
list_num = c(1,2,3,4)
move = sample(list_num, size = number, replace = TRUE)
for (i in 1:number) {
if (move[i] == 1) {
x = x + 1
}
else if (move[i] == 2) {
x = x - 1
}
else if (move[i] == 3) {
y = y + 1
}
else if (move[i] == 4) {
y = y - 1
}
vec1 <- c(vec1, i)
xcor <- c(xcor, x)
ycor <- c(ycor, y)
}
df_randomwalk = data.frame(vec1, xcor, ycor)
ggplot(df_randomwalk, aes(x = xcor, y = ycor)) +
geom_point(alpha = 0.1, size = 0.3) + geom_path() +
theme_minimal()
This should do it.
start <- df_randomwalk %>% filter(vec1 == min(df_randomwalk$vec1))
end <- df_randomwalk %>% filter(vec1 == max(df_randomwalk$vec1))
ggplot(df_randomwalk, aes(x = xcor, y = ycor)) +
geom_point(alpha = 0.1, size = 0.3) + geom_path() +
geom_point(alpha = 0.1, size = 0.3) +
theme_minimal() +
geom_point(start, mapping=aes(x=xcor,y=ycor), colour="red", size=1) +
geom_point(end, mapping=aes(x=xcor,y=ycor), colour="blue", size=1)

ggplot2 making a line graph that records constantly changing variables

I have a graph and code like the following:
library(igraph)
g <- make_empty_graph (2) %>%
set_vertex_attr("a", value = 1) %>%
add_vertices(2, color = 2, "a" = 2) %>%
add_vertices(2, color = 4, "a" = 3) %>%
add_edges(c(1,2, 2,1, 1,5, 5,1, 1,4 ,4,1)) %>%
set_vertex_attr("xyz", value = 3)
i <- 1
repeat {
prev_value <- mean(V(g)$a == 1)
print(V(g)$a)
print(mean(V(g)$a == 1))
print(i)
V(g)$xyz = sapply(V(g), function(x) {
NeighborList = neighbors(g, x)
length(NeighborList[NeighborList$a == 2])
})
V(g)$a[V(g)$xyz == 1] = 2
i <- i + 1
aa <- mean(V(g)$a == 1)
if (aa == prev_value) {
break
}
}
df <- data.frame(time=i, prop=aa)
library(ggplot2)
ggplot(data=df, aes(x=time, y=prop, group=1)) +
geom_line() +
geom_point()
Whenever I try to run this however, it doesn't work. Ideally I would like an output where ggplot2 would plot a line graph that depicts the change in the proportion of nodes with an attribute "aa" value of 1 as the time variable "i" changes.
I am not sure what exactly you want. But if you want to depict the difference in aa for each iteration then you can add the ggplot inside the repeat.
repeat {
prev_value <- mean(V(g)$a == 1)
print(V(g)$a)
print(mean(V(g)$a == 1))
print(i)
V(g)$xyz = sapply(V(g), function(x) {
NeighborList = neighbors(g, x)
length(NeighborList[NeighborList$a == 2])
})
V(g)$a[V(g)$xyz == 1] = 2
i <- i + 1
aa <- mean(V(g)$a == 1)
if(i == 1){
df <- data.frame(time=i, prop=aa)
print( ggplot(data=df, aes(x=time, y=prop, group=1)) +
geom_line() +
geom_point() )}else{
df <- rbind(df,data.frame(time=i, prop=aa))
print( ggplot(data=df, aes(x=time, y=prop, group=1)) +
geom_line() +
geom_point() )}
if (aa == prev_value) {
break
}
}

What the solution for this ggplot2 error on gigINEXT function:" Error in levels<-..."?

I'm running some examples from iNEXT package on Rstudio and having trouble with the graphical example. To help possible suggestions, below are some initial steps that run without any major problems, followed by the command line that generates the error:
#packages and dependencies
>install.packages("iNEXT")
>install.packages('devtools')
>library(devtools)
>install_github('JohnsonHsieh/iNEXT')
>library(iNEXT)
>library(ggplot2)
#example dataset
>data(spider)
#last line that runs properly before the error
>out <- iNEXT(spider, q=c(0, 1, 2), datatype="abundance", endpoint=500)
#here comes to the problematic line and its error
>ggiNEXT(out, type=1, facet.var="site")
Error in `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, : factor level [2] is duplicated
Any idea how to solve this?
I solved it, adding the unique function, inside the ggiNEXT.iNEXT function:
Below are the original command line and the same line altered:
Original command line:
z$lty <- factor(z$method, c("interpolated", "observed", "extrapolated"),
c("interpolation", "interpolation", "extrapolation"))
Changed command line:
z$lty <- factor(z$method, levels=unique(c("interpolated", "observed", "extrapolated"),
c("interpolation", "interpolation", "extrapolation")))
After the change, I could make the graph exactly as the example from https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html
Just for note, I used traceback() to find that the problem was inside ggiNEXT.iNEXT. Then I found the command indicated by the error message, and use the unique function based on a similar problem from this post: ggplot: order of factors with duplicate levels
To run properly and construct the graphs, past all the altered function on R console:
ggiNEXT.iNEXT<-function (x, type = 1, se = TRUE, facet.var = "none", color.var = "site",
grey = FALSE)
{
TYPE <- c(1, 2, 3)
SPLIT <- c("none", "order", "site", "both")
if (is.na(pmatch(type, TYPE)) | pmatch(type, TYPE) == -1)
stop("invalid plot type")
if (is.na(pmatch(facet.var, SPLIT)) | pmatch(facet.var, SPLIT) ==
-1)
stop("invalid facet variable")
if (is.na(pmatch(color.var, SPLIT)) | pmatch(color.var, SPLIT) ==
-1)
stop("invalid color variable")
type <- pmatch(type, 1:3)
facet.var <- match.arg(facet.var, SPLIT)
color.var <- match.arg(color.var, SPLIT)
if (facet.var == "order")
color.var <- "site"
if (facet.var == "site")
color.var <- "order"
options(warn = -1)
z <- fortify(x, type = type)
options(warn = 0)
if (ncol(z) == 7) {
se <- FALSE
}
datatype <- unique(z$datatype)
if (color.var == "none") {
if (levels(factor(z$order)) > 1 & "site" %in% names(z)) {
warning("invalid color.var setting, the iNEXT object consists multiple sites and orders, change setting as both")
color.var <- "both"
z$col <- z$shape <- paste(z$site, z$order, sep = "-")
}
else if ("site" %in% names(z)) {
warning("invalid color.var setting, the iNEXT object consists multiple orders, change setting as order")
color.var <- "site"
z$col <- z$shape <- z$site
}
else if (levels(factor(z$order)) > 1) {
warning("invalid color.var setting, the iNEXT object consists multiple sites, change setting as site")
color.var <- "order"
z$col <- z$shape <- factor(z$order)
}
else {
z$col <- z$shape <- rep(1, nrow(z))
}
}
else if (color.var == "order") {
z$col <- z$shape <- factor(z$order)
}
else if (color.var == "site") {
if (!"site" %in% names(z)) {
warning("invalid color.var setting, the iNEXT object do not consist multiple sites, change setting as order")
z$col <- z$shape <- factor(z$order)
}
z$col <- z$shape <- z$site
}
else if (color.var == "both") {
if (!"site" %in% names(z)) {
warning("invalid color.var setting, the iNEXT object do not consist multiple sites, change setting as order")
z$col <- z$shape <- factor(z$order)
}
z$col <- z$shape <- paste(z$site, z$order, sep = "-")
}
z$lty <- factor(z$method, levels=unique(c("interpolated", "observed", "extrapolated"),
c("interpolation", "interpolation", "extrapolation")))
z$col <- factor(z$col)
data.sub <- z[which(z$method == "observed"), ]
g <- ggplot(z, aes_string(x = "x", y = "y", colour = "col")) +
geom_point(aes_string(shape = "shape"), size = 5, data = data.sub)
g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) +
guides(linetype = guide_legend(title = "Method"), colour = guide_legend(title = "Guides"),
fill = guide_legend(title = "Guides"), shape = guide_legend(title = "Guides")) +
theme(legend.position = "bottom", legend.title = element_blank(),
text = element_text(size = 18))
if (type == 2L) {
g <- g + labs(x = "Number of sampling units", y = "Sample coverage")
if (datatype == "abundance")
g <- g + labs(x = "Number of individuals", y = "Sample coverage")
}
else if (type == 3L) {
g <- g + labs(x = "Sample coverage", y = "Species diversity")
}
else {
g <- g + labs(x = "Number of sampling units", y = "Species diversity")
if (datatype == "abundance")
g <- g + labs(x = "Number of individuals", y = "Species diversity")
}
if (se)
g <- g + geom_ribbon(aes_string(ymin = "y.lwr", ymax = "y.upr",
fill = "factor(col)", colour = "NULL"), alpha = 0.2)
if (facet.var == "order") {
if (length(levels(factor(z$order))) == 1 & type != 2) {
warning("invalid facet.var setting, the iNEXT object do not consist multiple orders.")
}
else {
g <- g + facet_wrap(~order, nrow = 1)
if (color.var == "both") {
g <- g + guides(colour = guide_legend(title = "Guides",
ncol = length(levels(factor(z$order))), byrow = TRUE),
fill = guide_legend(title = "Guides"))
}
}
}
if (facet.var == "site") {
if (!"site" %in% names(z)) {
warning("invalid facet.var setting, the iNEXT object do not consist multiple sites.")
}
else {
g <- g + facet_wrap(~site, nrow = 1)
if (color.var == "both") {
g <- g + guides(colour = guide_legend(title = "Guides",
nrow = length(levels(factor(z$order)))), fill = guide_legend(title = "Guides"))
}
}
}
if (facet.var == "both") {
if (length(levels(factor(z$order))) == 1 | !"site" %in%
names(z)) {
warning("invalid facet.var setting, the iNEXT object do not consist multiple sites or orders.")
}
else {
g <- g + facet_wrap(site ~ order)
if (color.var == "both") {
g <- g + guides(colour = guide_legend(title = "Guides",
nrow = length(levels(factor(z$site))), byrow = TRUE),
fill = guide_legend(title = "Guides"))
}
}
}
if (grey) {
g <- g + theme_bw(base_size = 18) + scale_fill_grey(start = 0,
end = 0.4) + scale_colour_grey(start = 0.2, end = 0.2) +
guides(linetype = guide_legend(title = "Method"),
colour = guide_legend(title = "Guides"), fill = guide_legend(title = "Guides"),
shape = guide_legend(title = "Guides")) + theme(legend.position = "bottom",
legend.title = element_blank())
}
g <- g + theme(legend.box = "vertical")
return(g)
}

R Knitr not printing model results to finle

I have an R script that is generating a number of plots and well as assessing a few linear models. For some reason when I try to print out the plots and linear model statistics in a loop they don't end up in the file but when I remove the loop they get printed to file.
R/Knitr markup that doesn't work (desired lines to print with # I want this to print):
library('RODBC')
library('ggplot2')
library('dplyr')
library('reshape2')
con <- odbcConnect('yield_model')
sql <- "SELECT DISTINCT [grouping],[group],regionId,class,finalPk,avgArea,layer,runTime,random,name FROM dbo.pk p LEFT OUTER JOIN dred.dbo.yasMap y ON p.class = y.id WHERE random=1"
values <- sqlQuery(con,sql,stringsAsFactors = FALSE)
values$model = "NM"
groupings <- unique(select(values,regionId,class,layer))
groupings <- groupings[order(groupings$layer,groupings$regionId,groupings$class),]
for(i in 1:nrow(groupings)) {
data <- subset(values,regionId == groupings$regionId[i] & class == groupings$class[i] & layer == groupings$layer[i])
layer <- unique(select(data,layer))
region <- unique(select(data,regionId))
defectNo <- unique(select(data,class))
defectName <- unique(select(data,name))
count <- length(unique(data$avgArea))
average <- mean(data$finalPk)
myPlot <- ggplot(data=data,aes(x=avgArea,y=finalPk)) +
geom_point(size=4,color="red") +
ggtitle(paste("Defect=",defectName$name,"(", defectNo$class,"), Region=",region$regionId, ", Layer=", layer$layer,sep="")) +
geom_abline(intercept=average,slop=0,size=1,aes(color="mean"))
if(count > 1) {
myPlot <- myPlot + stat_smooth(method ="lm",formula = y ~ x, se = FALSE,size=1,aes(color="linear"))
}
if (count > 3) {
myPlot <- myPlot + stat_smooth(method = "lm", formula = y ~ log(x), se = FALSE, size=1, aes(color="log"))
myPlot <- myPlot + stat_smooth(method = "lm", formula = y ~ poly(x,2), se = FALSE, size=1, aes(color="poly"))
}
if(count > 1 & count < 4) {
myPlot <- myPlot + scale_colour_manual(name="",values=c("mean" = "orange","linear" = "green"))
} else if(count > 3) {
myPlot <- myPlot + scale_colour_manual(name="",values=c("mean" = "orange","linear" = "green","log" = "red","poly" = "blue"))
} else if (count == 2 | count == 3) {
}
plot(myPlot)
paste("Average Pk=",average,sep="") # I want this to print
if(count > 1) {
linMod <- lm(data$finalPk ~ data$avgArea)
coef(linMod) # I want this to print
summary(linMod) # I want this to print
}
if (count > 3) {
linModLog <- lm(data$finalPk ~ log(data$avgArea))
coef(linModLog) # I want this to print
summary(linModLog) # I want this to print
linModPoly <- lm(data$finalPk ~ poly(data$avgArea,2))
coef(linModPoly) # I want this to print
summary(linModPoly) # I want this to print
}
}
R/Knitr that does print:
library('RODBC')
library('ggplot2')
library('dplyr')
library('reshape2')
con <- odbcConnect('yield_model')
sql <- "SELECT DISTINCT [grouping],[group],regionId,class,finalPk,avgArea,layer,runTime,random,name FROM dbo.pk p LEFT OUTER JOIN dred.dbo.yasMap y ON p.class = y.id WHERE random=1"
values <- sqlQuery(con,sql,stringsAsFactors = FALSE)
values$model = "NM"
groupings <- unique(select(values,regionId,class,layer))
groupings <- groupings[order(groupings$layer,groupings$regionId,groupings$class),]
#for(i in 1:nrow(groupings)) {
i <- 1
data <- subset(values,regionId == groupings$regionId[i] & class == groupings$class[i] & layer == groupings$layer[i])
layer <- unique(select(data,layer))
region <- unique(select(data,regionId))
defectNo <- unique(select(data,class))
defectName <- unique(select(data,name))
count <- length(unique(data$avgArea))
average <- mean(data$finalPk)
myPlot <- ggplot(data=data,aes(x=avgArea,y=finalPk)) +
geom_point(size=4,color="red") +
ggtitle(paste("Defect=",defectName$name,"(", defectNo$class,"), Region=",region$regionId, ", Layer=", layer$layer,sep="")) +
geom_abline(intercept=average,slop=0,size=1,aes(color="mean"))
if(count > 1) {
myPlot <- myPlot + stat_smooth(method ="lm",formula = y ~ x, se = FALSE,size=1,aes(color="linear"))
}
if (count > 3) {
myPlot <- myPlot + stat_smooth(method = "lm", formula = y ~ log(x), se = FALSE, size=1, aes(color="log"))
myPlot <- myPlot + stat_smooth(method = "lm", formula = y ~ poly(x,2), se = FALSE, size=1, aes(color="poly"))
}
if(count > 1 & count < 4) {
myPlot <- myPlot + scale_colour_manual(name="",values=c("mean" = "orange","linear" = "green"))
} else if(count > 3) {
myPlot <- myPlot + scale_colour_manual(name="",values=c("mean" = "orange","linear" = "green","log" = "red","poly" = "blue"))
} else if (count == 2 | count == 3) {
}
plot(myPlot)
paste("Average Pk=",average,sep="") # I want this to print
if(count > 1) {
linMod <- lm(data$finalPk ~ data$avgArea)
coef(linMod) # I want this to print
summary(linMod) # I want this to print
}
if (count > 3) {
linModLog <- lm(data$finalPk ~ log(data$avgArea))
coef(linModLog) # I want this to print
summary(linModLog) # I want this to print
linModPoly <- lm(data$finalPk ~ poly(data$avgArea,2))
coef(linModPoly) # I want this to print
summary(linModPoly) # I want this to print
}
#}
My header values (I also tried with no results portion at all):
{r echo=FALSE,results='asis'}
Hopefully somebody has some advice for me. Appreciate the help!
Given that it's everything after the plot(myPlot) statement which is giving you problems in the loop, I'm thinking the answer here might apply to you.
Try adding a plot.new call after the plot(myPlot) statement

Resources