Issue when knitting with rmarkdown and latex distribution - r

[WARNING] Deprecated: --self-contained. use --embed-resources --standalone
! Missing $ inserted.
<inserted text>
$
l.100 ![](methods_
files/figure-latex/plot-1.pdf)<!-- -->
Try to find the following text in methods.Rmd:
![](methods_
You may need to add $ $ around a certain inline R expression `r ` in methods.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile methods.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See methods.log for more info.
Execution halted
I get this error message when I try to knit my methods section document. Everything seems to work fine if I delete the following code chunk:
#Overlaying both groups to see which questions show most differences in performance between them
ggplot(mapping = aes(x, y)) +
geom_bar(data = nonrankedplayers, aes(x= questions , y=scoreNR), width = 0.8, stat = 'identity', fill= 'darkgrey') +
geom_bar(data = rankedplayers, aes(x= questions , y=scoreR), width = 0.4, stat = 'identity') +
theme_classic() +
ggtitle("Percentage of participants knowing the answer per group")+
theme(plot.title = element_text(hjust = 0.5))+ #centre the title
scale_x_discrete("Question", breaks = seq(0,26, by = 1) )+
scale_y_continuous("Percentage correct", breaks = seq(0,1, by = 0.1))
I tried adding $ between the code chunk, but then R does not recognise it.

Related

ggplot2 geom_rug error Can't recycle `x` (size 496) to match `PANEL` (size 5456)

I am trying to plot graphs with geom_rug on bottom. The geom_rug command below worked well when I last ran the code (August 2022). However, when I attempted to re-plot, I get an error message that I cannot understand.
Below is my code:
p <- ggplot(Pred.resp.univar, aes(z, est, ymin = est - 1.96*se, ymax = est + 1.96*se)) +
geom_smooth(stat = "identity") +
facet_wrap(~ variable, scales = "free_x") +
ylab("h(z)") +
geom_rug(data=data_gdm, mapping=aes(x=Z), sides="b", alpha = 0.2, inherit.aes = F, colour="black") +
theme_bw()
p
Below is the error message that I get. Z has 496 rows, 11 columns, which should make a panel size of 5456, so I don't understand why I have this error message. Before geom_rug command update by the end of last year, I did not get this error message with the same command:
Below is the data structure fyi:
I looked up recent geom_rug command examples and my R error message, but nothing was helpful.

Correct syntax for manually scaling the width of lines in ggplot

I plot a melted dataframe using the following code (just a minimal fragment shown for clarity):
ggplot(df_melt, aes(x = Date, y= value)) +
geom_line(aes(color = Variable, size = Variable)) +
scale_color_manual(values = c("dark green", "azure3", "goldenrod4")) +
scale_size_manual(values = c(.6, .8, .6)) +
.
.
.
but get the following error message:
Warning message:
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
But if I change size = Variable to linewidth = Variable, I get a different warning:
Warning messages:
1: Using linewidth for a discrete variable is not advised.
If I further change scale_size_manual to scale_linewidth_manual, I get an error message:
Error in scale_linewidth_manual(values = c(0.6, 0.7, 0.6, 0.3)) :
could not find function "scale_linewidth_manual"
What is the correct syntax here? Unfortunately, ggplot2's help got me nowhere , so I would be very appreciative for some guidance.
Sincerely and with many thanks in advance
Thomas Philips
The current preferred option seems to be to use scale_discrete_manual(), see also https://github.com/tidyverse/ggplot2/issues/5050. To demonstrate:
library(ggplot2)
ggplot(economics_long, aes(date, value01)) +
geom_line(
aes(colour = variable, linewidth = variable)
) +
scale_discrete_manual("linewidth", values = seq(0.1, 3, length.out = 5))
Created on 2022-12-10 by the reprex package (v2.0.1)

Rerun the name of the plot cannot show but shows Error in check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (8): label

I have a weird problem. I am using ggplot2 to generate 12 biplot figures for 12 RDA (Redundancy analysis). Please see the below code for one figure.
Here I attach the data and full code in the google drive https://drive.google.com/drive/folders/1ohO62-2jmMzVjJo2JLZOo8ffabzBx-8S?usp=sharing
plot1 <-ggplot() +
geom_point(data = sp,aes(RDA1,RDA2),shape=21,size=4,fill="tomato2")+
scale_fill_brewer(palette = 'Set2')+
geom_text_repel(data = sp,aes(RDA1,RDA2,label=sp2$Group),color="tomato1",size=4.5)+
geom_point(data = yz,aes(RDA1,RDA2),size=3,shape=24,fill="dodgerblue3")+
scale_shape_manual(values = c(22:23))+
geom_text_repel(data = yz,aes(RDA1,RDA2,label=yz2$label),color="dodgerblue2",size=4.5)+
labs(x=paste("RDA 1 (", format(100 *ii$cont[[1]][2,1], digits=3), "%)", sep=""),
y=paste("RDA 2 (", format(100 *ii$cont[[1]][2,2], digits=3), "%)", sep=""))+
geom_hline(yintercept=0,linetype=3,size=1,color="gray50") +
geom_vline(xintercept=0,linetype=3,size=1,color="gray50")+
guides(shape=guide_legend(title=NULL,color="black"),
fill=guide_legend(title=NULL))+
theme_bw()+theme(panel.grid=element_blank())+
theme(legend.position = "none") +
theme(legend.text = element_text(colour="black", size = 13)) +
theme(axis.title.x = element_text(colour="black",size=14),
axis.text.x = element_text(colour="black",size=14)) +
theme(axis.title.y = element_text(colour="black",size=14),
axis.text.y = element_text(colour="black",size=14))
plot1
The problem is that when I finished running the code for the first figure (plot1), I could see the first figure by running plot1. However, when after I finished running the code for the other figures (plot3 or 4 or 5..., not exact one, very randomly), I rerun plot1, it came out an error said Error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (8): label
Run rlang::last_error() to see where the error occurred. Also, this kind of error comes out quite randomly. For plot1, sometimes there is no error. Sometimes this error shows.

Error when graphing with ggplot. geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

When I use this code, it will create a graph just fine, but in rmarkdown it will show the error at the top. It shows the error with or without the aes(group = 1) in the main ggplot and/or the geom_line(). Any suggestions? None of the other posts I find about this question tell me what to do if the error persists after adding the group = 1.
ggplot(monthly_leads_dealer, mapping = aes(x = as.factor(month), y = n, group = 1))+geom_point()+
geom_line(aes(group = 1))+
geom_label(aes(label = n))+
facet_wrap(~Dealer)+
labs(title = "2021 Total Monthly Leads by Dealer", caption = "Source: Lead Sheet", x = "Month", y = "Totals")
EDIT: the warning will appear in and out of rmarkdown. Putting warning = FALSE in the markdown chunk header doesn't get rid of the warning in markdown.
EDIT: error = FALSE and echo = FALSE in the markdown code chunk didn't solve this particular issue, but message = FALSE did solve it.

Embed ggplot2 Graph in Knitr Document: Unable to find function ggplot

I am working on a document and am using both knitr and ggplot2. I am new to knitr and TeX itself and therefore not overly familiar with all that I am doing.
When I open RStudio to do the work, I first run the following two commands:
require("knitr")
require("ggplot2")
I then click on the Compile PDF. I have the following code that throws an error:
<<histogram, echo=FALSE, fig.align='center'>>=
summary(los$hosp_svc)
summary(los$Pt_Age)
binsize = diff(range(los$Pt_Age)/30)
ggplot(los, aes(x = Pt_Age)) +
geom_histogram(binwidth = binsize, fill = "red",
alpha = 0.315, colour = 'black') +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
xlab("Patient Age in Years") +
ylab("Frequency/Count") +
ggtitle("Histogram of Patient Age")
#
The error that I am getting is that the ggplot function could not be found, which is odd because if I just run the above code in the console the graph produces just find, so I know the package is loaded and available for use.
Any thoughts?
Thank you,
When working with .Rnw files (or .Rmd files), be sure to include any library calls in your script (see below). When you press the "Compile PDF" button, the R code in your script is submitted to a new instance of R to prevent anything in your current environment from mucking up the results. This might seem a little strange, but is good for reproducibility. So objects that aren't created explicitly via your script and packages that aren't called explicitly in your script will be forgotten as soon as you hit "Compile PDF"
<<histogram, echo=FALSE, fig.align='center'>>=
library(ggplot2)
summary(los$hosp_svc)
summary(los$Pt_Age)
binsize = diff(range(los$Pt_Age)/30)
ggplot(los, aes(x = Pt_Age)) +
geom_histogram(binwidth = binsize,
fill = "red",
alpha = 0.315,
colour = 'black') +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
xlab("Patient Age in Years") +
ylab("Frequency/Count") +
ggtitle("Histogram of Patient Age")
#

Resources