ggplot error in if (node$tag == "span") after trying to split a faceted plot over multiple pages - r

My dataset includes the variable "annual_TRW" measured over different "survey_year", with 5 "Sample_Core_ID" within each "block" nested within each "site".
The following code was working to plot all samples for a plot subset.
pdf("plots.pdf")
library(ggplot2)
dataset %>%
group_by(site, block, Sample_Core_ID ) %>%
ggplot(., aes(x = survey_year, y = annual_TRW, colour = Sample_Core_ID)) +
geom_line() +
facet_grid(Sample_Core_ID ~ .)
dev.off()
Then, I was trying to split a faceted plot over multiple pages, with each page including the faceted plots (1 column, 5 rows) corresponding to the samples of each block. I was testing using different different functions ggplus::facet_multiple, gridExtra::marrangeGrob, and ggforce::facet_wrap_paginate, but without success i.e. pdf were created empty.
The problem is that, after trying different options, now I get the following error every time that I try to use any ggplot on the same dataset. Now, I dont manage to plot the code that worked before without giving this error.
Error in if (node$tag == "span") { : argument is of length zero
Question 1: How can I solve this error every time I try to use this ggplot?
Question 2: Once this works again, how can I split the faceted plot over multiple pages as explained before?
Thanks a lot in advance!
I tried re-running the entire code, and re-calling the packages, but it doesnt work.

Regarding Problem 1: the error was solved after updating the R version and reinstalling only the necessary packages that worked. The issue might have come from installing the packages ggplus as described here https://github.com/guiastrennec/ggplus

Related

R: $ cannot be used for atomic vectors (box plot)

I would love some advice on how to fix the error shown in the screenshot. I only started learning R yesterday so I'm not very familiar with it. I tried using % but this produced a different type of error (unexpected input). Are there any problems with how I've defined time_spen and species earlier on in the code?
Do you want to make a boxplot of the time_spen variable for each species? The ggplot2 package can help.
Since I don't have your data, here is an example from the penguins dataset in the palmerpenguins package.
library(palmerpenguins)
library(ggplot2)
ggplot(penguins) +
geom_boxplot(aes(x = species, y = body_mass_g))

ggplot graphs appearing empty when using Rmarkdown

I am having problems using ggplot with Rmarkdown.
I have a code that is supposed to return a ggplot graph.When I execute the code in a R session it works but the same code returns an empty plot when using R markdown.This is the code I am using in the R session
m<-ggplot(data=t,aes_string(x=v,y =t$cnt_pct))
m+geom_bar(stat = "identity",fill="#FF0000",color='black')+geom_text(aes(label = cnt_pct),vjust=-0.2)+ggtitle(paste(v,paste("graph")))+theme(plot.title = element_text(hjust = 0.5))+ylab("percent")+geom_line(aes_string(x=v,y =t$keshel_pct),group=1,size=2)
This is the code I am using in the Rmarkdown
m<-ggplot(data=t,aes_string(x=v,y =t$cnt_pct))
m+geom_bar(stat = "identity",fill="#FF0000",color='black')+geom_text(aes(label = cnt_pct),vjust=-0.2)+ggtitle(paste(v,paste("graph")))+theme(plot.title = element_text(hjust = 0.5))+ylab("percent")+geom_line(aes_string(x=v,y =t$keshel_pct),group=1,size=2)
plot(m)
The only diffrence between the two is that with the Rmarkwond a plot(m) line is added.
For some reason the plot command makes the graph empty, when I remove it and run the Rmarkdown no graphs are returned.
This is the graph empty
This is the graph working in a session of R
Anybody has any idea what is my mistake?
Any help will be appriciated
You have a few issues here:
1) your plot syntax has issues. aes_string takes strings referring to column names, but you're passing a variable v for x and direct column data t$cnt_pnt for y. Check some ggplot examples to get the syntax right.
2) The plot(m) statement shouldn't work for two reasons: first, you print ggplot objects, not plot them. Second, you are not assigning the m + geom_bar(...) statement to anything, so even if your plot statement did work, you'd get the plot without the geom_bar(...) stuff. Try e.g. y = m+ geom_bar(...); print(y) instead.

How to put 2 boxplot in one graph in R without additional libraries?

I have this kind of dataset
Defect.found Treatment Program
1 Testing Counter
1 Testing Correlation
0 Inspection Counter
3 Testing Correlation
2 Inspection Counter
I would like to create two boxplotes, one boxplot of detected defects per program and one boxplot of detected defects per technique but in one graph.
Meaning having:
boxplot(exp$Defect.found ~ exp$Treatment)
boxplot(exp$Defect.found ~ exp$Program)
In a joined graph.
Searching on Stackoverflow I was able to create it but with lattice library typing:
bwplot(exp$Treatment + exp$Program ~ exp$Defects.detected)
but i would like to know if its possible to create the graph without additional libraries like ggplot and lattice
Prepare the plot window to receive two plots in one row and two columns (default is obviously one row and one column):
par(mfrow = c(1, 2))
My suggestion is to avoid using the word exp, because it is already used for the exponential function. Use for instance mydata.
Defects found against treatment (frame = F suppresses the external box):
with(mydata, plot(Defect.found ~ Treatment, frame = F))
Defects found against program (ylab = NA suppresses the y label because it is already shown in the previous plot):
with(mydata, plot(Defect.found ~ Program, frame = F, ylab = NA))

Plot histograms or pie charts in a scatter plot

I need to repeat the thing done in:
tiny pie charts to represent each point in an scatterplot using ggplot2 but I stumbled into the problem that the package ggsubplot is not available for 3.3.1 R version.
Essentially I need a histogram or a pie chart in predefined points on the scatterplot. Here is the same code that is used in the cited post:
foo <- data.frame(X=runif(30),Y=runif(30),A=runif(30),B=runif(30),C=runif(30))
foo.m <- melt(foo, id.vars=c("X","Y"))
ggplot(foo.m, aes(X,Y))+geom_point()
ggplot(foo.m) +
geom_subplot2d(aes(x = X, y = Y, subplot = geom_bar(aes(variable,
value, fill = variable), stat = "identity")), width = rel(.5), ref = NULL)
The code used libraries reshape2, ggplot2 and ggsubplot.
The image that I want to see is in the post cited above
UPD: I downloaded the older versions of R (3.0.2 and 3.0.3) and checkpoint package, and used:
checkpoint("2014-09-18")
as was described in the comment bellow. But I get an error:
Using binwidth 0.0946
Using binwidth 0.0554
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
Which I can't get around, because when I try to include facet, the following error comes up:
Error: ggsubplots do not support facetting
It doesn't look like ggsubplot is going to fix itself any time soon. One option would be to use the checkpoint package, and essentially "reset" your copy of R to a time when the package was compatible. This post suggests using a time point of 2014-09-18.

Stacked bar in R

I have a table exported in csv from PostgreSQL and I'd like to create a stacked bar graph in R. It's my first project in R.
Here's my data and what I want to do:
It the quality of the feeder bus service for a certain provider in the area. For each user of the train, we assign a service quality based of synchronization between the bus and the train at the train stations and calculate the percentage of user that have a ideal or very good service, a correct service, a deficient service or no service at all (linked to that question in gis.stackexchange)
So, It's like to use my first column as my x-axis labels and my headers as my categories. The data is already normalized to 100% for each row.
In Excel, it's a couple of clicks and I wouldn't mind typing a couple of line of codes since it's the final result of an already quite long plpgsql script... I'd prefer to continue to code instead of moving to Excel (I also have dozens of those to do).
So, I tried to create a stacked bar using the examples in Nathan Yau's "Visualize This" and the book "R in Action" and wasn't quite successful. Normally, their examples use data that they aggregate with R and use that. Mine is already aggregated.
So, I've finally come up with something that works in R:
but I had to transform my data quite a bit:
I had to transpose my table and remove my now-row (ex-column) identifier.
Here's my code:
# load libraries
library(ggplot2)
library(reshape2)
# load data
stl <- read.csv("D:/TEMP/rabat/_stl_rabattement_stats_mtl.csv", sep=";", header=TRUE)
# reshape for plotting
stl_matrix <- as.matrix(stl)
# make a quick plot
barplot(stl_matrix, border=NA, space=0.1, ylim=c(0, 100), xlab="Trains", ylab="%",
main="Qualité du rabattement, STL", las = 3)
Is there any way that I could use my original csv and have the same result?
I'm a little lost here...
Thanks!!!!
Try the ggplot2 and reshape library. You should be able to get the chart you want with
stl$train_order <- as.numeric(rownames(stl))
stl.r <- melt(stl, id.vars = c("train_no", "train_order"))
stl.r$train_no <- factor(
stl.r$train_no,
levels = stl$train_no[order(stl$train_order)])
ggplot(stl.r, aes(x = factor(train_no), y = value, fill = variable)) + geom_bar(stat = 'identity')
It appears that you transposed the matrix manually. This can be done in R with the t() function.
Add the following line after the as.matrix(stl) line:
stl_matrix <- t(stl_matrix)

Resources