Reputation: 1073
I am running a code in R that has several iterations. The result of each one is stored in a table called accatable
. As you can see, in this example below there is only the result for the row S2*
> accatable
1 2 3 4 5 6 7
S1 NA NA NA NA NA NA NA
S2 NA NA NA NA NA NA NA
S1_S2 NA NA NA NA NA NA NA
S2* 0.737714 0.7083141 0.767515 0.8060774 0.7800401 0.8015116 0.815209
S1_S2* NA NA NA NA NA NA NA
What I want to create is a graph using ggplot2
showing the evolution. For example, you run the first iteration and get the value for row S2*
column 1
. Then in the second iteration you get the value for row S2*
column 2
, etc.
The objective is that after each iteration you plot a graph that will be updated each time to show the evolution.
So far, I have manage to create that graph but only when all the table is completed. Here is the test I have tried. I first create the df
and convert it from wide to long format. Then I used ggplot
to crate the output
testdf <- replicate(7, sample(0:10,5,rep=TRUE))
colnames(testdf) <- as.character(seq(1,7))
rownames(testdf) <- c("S1", "S2", "S1_S2", "S2*", "S1_S2*")
test <- melt(testdf, id.vars=testdf[[1]])
colnames(test) <- c("Input", "Images", "Acca")
test
test$IMAGES <- as.numeric(as.vector(test$Images))
ggplot (data = test, aes(x=Images, y=Acca, group=Input, colour=Input)) +
geom_line(aes(linetype=Input)) +
geom_point() +
scale_colour_manual(name="Scenario",
values = c("black","black","blue","blue","red","red",
"darkgreen","darkgreen")) +
scale_linetype_manual(name="Scenario",
values=c("solid","dashed","solid","dashed","solid", "dashed",
"solid","dashed","solid","dashed", "solid","dashed")) +
theme_minimal() +
labs(x="Images", y="Acca",title="test") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous("Images", c(1,2,3,4,5,6,7), c(1,2,3,4,5,6,7))
Any idea how I could adapt the ggplot
code to plot the table each time a new value is added?
Upvotes: 0
Views: 67
Reputation: 29095
Here's a tidyverse
solution.
In order to illustrate this, I've created a blank data frame the same size as testdf
, to be updated iteratively:
testdf <- as.data.frame(testdf)
accatable <- data.frame(`1` = rep(NA, 5), `2` = rep(NA, 5),
`3` = rep(NA, 5), `4` = rep(NA, 5),
`5` = rep(NA, 5), `6` = rep(NA, 5),
`7` = rep(NA, 5),
row.names = rownames(testdf))
> accatable
X1 X2 X3 X4 X5 X6 X7
S1 NA NA NA NA NA NA NA
S2 NA NA NA NA NA NA NA
S1_S2 NA NA NA NA NA NA NA
S2* NA NA NA NA NA NA NA
S1_S2* NA NA NA NA NA NA NA
Assuming for run i
in the for-loop, column i
of the data frame becomes updated:
library(dplyr)
p.list <- vector("list", ncol(accatable))
for(i in seq_along(accatable)){
accatable[, i] <- testdf[, i] # replace with your actual updating code
p <- ggplot(accatable[, seq(1, i), drop = FALSE] %>% # keep only first 1-i columns
tibble::rownames_to_column(var = "Scenario") %>% # add row name as a column
tidyr::gather(iteration, value, -Scenario), # convert to long format
aes(x = iteration, y = value, group = Scenario,
color = Scenario, linetype = Scenario)) +
geom_line() +
geom_point() +
labs(x = "Images", y = "ACCA", title = paste("Iteration:", i)) +
theme_minimal()
print(p) # if you just want to SEE the result from each iteration
p.list[[i]] <- ggplotGrob(p) # if you want to SAVE the result from each iteration
}
gridExtra::grid.arrange(grobs = p.list, ncol = 1)
The result looks something like this:
(I've omitted the scale_XX()
specifications from the sample code, as I don't think they are essential to the solution. You can adjust the look & feel as needed.)
Upvotes: 1