Unable to create plots in Rpres (RStudio's HTML presentation format) - r

I am trying to create a HTML5 presentation with ggplot2 plots in it. I am using Rstudio's Rpres format. However I see no plot in the output presentation. For the example below, I get a textbox with a message like this:
<img src="PT terms for each age
Strata.Rnw-figure/unnamed-chunk-1-1.png" title="plot of chunk
unnamed-chunk-1" alt="plot of chunk unnamed-chunk-1" style="display:
block; margin: auto;" />
For other chunks I see no plot at all. I see the figures generated in a subdirectory, but they are not included in the presentation.
This could be due to the fact that I use setwd to change the current directory inside one of the chunks.
How do I make sure the plots are added to the presentation?
```{r, echo=FALSE,fig.width=8, fig.height=4, warning=FALSE, eval=TRUE, message=FALSE, tidy=TRUE, fig.align='center',fig=TRUE}
PT.term.table.combo.df <- structure(list(term = structure(c(3L, 6L, 10L, 9L, 5L, 8L, 2L,
7L, 1L, 4L, 11L, 16L, 20L, 13L, 18L, 19L, 15L, 14L, 17L, 12L), .Label = c("Erythema",
"Injection site erythema", "Injection site pain", "Injection site swelling",
"Pain", "Pain in extremity", "Paraesthesia", "Pruritus", "Rash",
"Urticaria", "Dizziness", "Fatigue", "Headache", "Unknown",
"Loss of consciousness", "Nausea", "Pallor", "Pyrexia", "Syncope",
"Vomiting", "Blood pressure decreased", "Condition aggravated",
"Convulsion", "Fall", "Grand mal convulsion", "Head injury",
"Immediate post-injection reaction", "Condition8",
"Condition2", "Condition3", "Condition4",
"Condition1", "Menstruation delayed", "Menstruation irregular",
"Condition5", "Condition12", "Unevaluable event"
), class = "factor"), normalized.count = structure(c(0.758666519304954,
0.509556068608868, 0.498746392459638, 0.426484861272957, 0.41955098519173,
0.333070361160926, 0.306233446655841, 0.303395720748491, 0.281332387076534,
0.275858307359097, 2.05157281092953, 1.55514068644281, 0.761792303294041,
0.730331039886107, 0.553772087835693, 0.545722098808532, 0.426814370578148,
0.422207780194755, 0.401335815218956, 0.325021057176447), .Names = c("Injection site pain",
"Pain in extremity", "Urticaria", "Rash", "Pain", "Pruritus",
"Injection site erythema", "Paraesthesia", "Erythema", "Injection site swelling",
"Dizziness", "Nausea", "Vomiting", "Headache", "Pyrexia", "Syncope",
"Loss of consciousness", "Hyperhidrosis", "Pallor", "Fatigue"
)), source = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2", "3", "4"), class = "factor")), .Names = c("term", "normalized.count",
"source"), row.names = c("Injection site pain", "Pain in extremity",
"Urticaria", "Rash", "Pain", "Pruritus", "Injection site erythema",
"Paraesthesia", "Erythema", "Injection site swelling", "Dizziness",
"Nausea", "Vomiting", "Headache", "Pyrexia", "Syncope", "Loss of consciousness",
"Hyperhidrosis", "Pallor", "Fatigue"), class = "data.frame")
library(ggplot2)
library(gdata)
#PT.term.table.combo.df <- combine(lapply( PT.term.tables$communities , FUN = function(x) { data.frame (term = names(x), normalized.count = x)}),names = 1:4)
PT.term.table.combo.df <- do.call(what=combine,args=lapply( PT.term.tables$communities , FUN = function(x) { data.frame (term = names(x), normalized.count = x)}))
levels(PT.term.table.combo.df$source)<- 1:4
#PT.term.table <- PT.term.tables$communities[[1]]
#term.df <- data.frame (term=names(PT.term.table), normalized.count = PT.term.table)
PT.plot<-ggplot(data=PT.term.table.combo.df, aes(x=term, y=normalized.count )) +
geom_bar(stat='identity') + coord_flip()+facet_wrap(~source)
print(PT.plot)
```

Ok, I was able to fix my problem by renaming my Rpres file, so that it does not have any spaces. So instead of "PT terms for each age Strata.Rnw.Rpres" I chose"PT_terms_plots.Rpres" as the filename.
If you believe this might be a bug, comment to let me know so that I can contact Rstudio devs.

Related

Make output of two rows into columns R

I am currently working with behavioural data in R from video analyses in BORIS. Every observation is 15 seconds and during this observation I noted the subject, its behaviour but also some background information such as the date, time of day, temperature, etc. However, the program has put this background information under the column "Behaviour" (so one of the behaviours is now "date") and its output under the column "Modifier" (which now says "15-10-2020" for example).
What I want is make more columns of date, time etc (from the column "Behaviour") and put its output (from the column "Modifier") in these columns, so that every behaviour has a subject, date, time, temperature, and so forth. I have however no idea how to do this.
I thought about using the function aggregate, but this gives me lots of extra rows with mainly NA's. I also looked into the package "tibble" but can't really make that work either.
Any suggestions would be greatly appreciated!
Some example rows (from dput()):
structure(list(Subject = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 7L), .Label = c("fallow deer female", "fallow deer female + calf",
"red deer female + calf", "roe deer male", "wild boar + young",
"wild boar male", "wild boar unknown sex"), class = "factor"),
Behavior = structure(c(1L, 2L, 8L, 7L, 12L, 3L, 5L, 10L,
6L, 4L), .Label = c("auditory vigilant", "date", "day/night",
"foraging", "nr. of individuals", "running", "temperature",
"time of day", "unknown behaviour", "walking", "walking while vigilant",
"weather"), class = "factor"), Behavioral.category = structure(c(4L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 3L), .Label = c("", "Background information",
"Non-vigilant", "Vigilant"), class = "factor"), Modifiers = structure(c(1L,
4L, 21L, 27L, 35L, 36L, 32L, 1L, 1L, 1L), .Label = c("",
"0346", "0347", "07172020", "07182020", "07212020", "07242020",
"07262020", "07272020", "08032020", "08052020", "1", "12",
"1307", "1327", "1342", "1343", "1430", "1528", "16", "1604",
"17", "1744", "21", "2119", "2120", "22", "23", "25", "26",
"3", "4", "7", "Clear", "Cloudy", "Day", "Night"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")
The output that I'd like to have would give as column names: Subject; Behavior; Date; Time of Day; Temperature. The modifier output would be the values of the columns "Date", "Time of Day", "Temperature". When this works, I could delete the column Modifiers (since all its values are already in assigned columns).
Split up the dataframe in actual behaviours and background information. Perform this code on the background information:
tidyr::pivot_wider(your_data, names_from = Behavior, values_from = Modifiers)
Merge the dataframes!

Error in as.data.frame.character(x, stringsAsFactors = FALSE, ...) when using papaja and tableone in RMarkdown

I am using papaja to create an APA formatted document in RMarkdown, however the apa_tables() function is throwing an error when I try to create my document [sidenote: apa_tables() builds on kable()]
## reprex dataframe
head<-structure(list(age = c(30, 30, 24, 30, 29, 58),
education = structure(c(5L, 2L, 2L, 2L, 2L, 3L), .Label = c("Less than high school", "High school diploma or GED", "Some college", "Associate's degree", "Bachelor's degree", "Professional or Masters degree", "Doctorate"), class = "factor"),
gender = structure(c(2L, 2L, 2L, 2L, 2L, 1L), .Label = c("Male", "Female", "Non-binary", "Agender", "Androgynous", "Demigirl", "Transgender (preferred gender not stated)","NA"), class = "factor"),
household_income = structure(c(5L, 3L, 6L, 1L, 2L, 2L), .Label = c("Less than $10,000", "$10,000 - $19,999","$20,000 - $29,999", "$30,000 - $39,999", "$40,000 - $49,999", "$50,000 - $59,999", "$60,000 - $69,999", "$70,000 - $79,999", "$80,000 - $89,999", "$90,000 - $99,999", "$100,000 - $149,999", "More than $150,000", "Prefer not to say"), class = "factor"),
ideology_general = c(8L, 3L, 5L, 8L, 9L, 8L),
ideology_economic = c(9L, 6L, 8L, 4L, 9L, 8L),
ideology_social = c(7L, 4L, 3L, 6L, 9L, 8L),
study = c("Climate Change", "Climate Change", "Flu vaccination", "Climate Change", "Climate Change", "Flu vaccination")),
row.names = c(NA,-6L), class = c("tbl_df", "tbl", "data.frame"))
## Use tableone to create a demographics table
tab<-print(tableone::CreateTableOne(vars = c("age",
"education",
"gender",
"household_income",
"ideology_general",
"ideology_economic",
"ideology_social"),
data = head, strata = "study", test = FALSE), printToggle = FALSE)
## print table using papaja
papaja::apa_table(tab)
This code throws the error
Error in as.data.frame.character(x, stringsAsFactors = FALSE, ...) : formal argument "stringsAsFactors" matched by multiple actual arguments
on the line with papaja::apa_table(tab)
This does run as expected if I create the table with knitr::kable(tab) instead of papaja::apa_table(tab) however that leaves me without other desired format options.
SOLVED: Upgrade to new version of papaja solved this issue.

Reorder bars within ggplot2 dodged barplot, levels are correct

I'm making a series of barplots, all fairly similar to this one that I use the following code to generate.
I've seen many posts about this, but I have tried to change the order of the variable of interest and that isn't working. Any tips?
library(ggplot2)
trtslope<-structure(list(Geno = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1a", "1b", "1c", "1d", "1e", "1f", "2co", "2h", "4f", "5t", "pin3", "pin3pin7"), class ="factor"), Light = structure(c(2L,2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Constant","Cycle"),class = "factor"), Trmt = structure(c(6L, 3L, 5L, 2L, 7L, 4L, 1L), .Label = c("None","10^-11Kin", "10^-10Kin", "10^-9Kin", "10^-11IAA", "10^-10IAA",
"10^-9IAA"), class = "factor"), mean = c(-1.54504597523189,
-1.53362395751867, -1.57385562758997, -1.54322151503139,
-1.66574978029235, -1.32095137998064, -1.36520900266343),
sd = c(0.46315259286543, 0.458985115845406, 0.482009142703553,
0.641786961061545, 0.590265055416619, 0.378034730883596,
0.400241364404397), ste = c(0.0545830565232129, 0.0564971622467328,
0.0607274438691926, 0.0789985139281606, 0.0647900070055787,
0.0417469522407694, 0.0292685472783953), Conc = c("10^-10",
"10^-10", "10^-11", "10^-11", "10^-9", "10^-9", "None"),
Group = c("IAA", "Kinetin", "IAA", "Kinetin", "IAA", "Kinetin",
"None")), .Names = c("Geno", "Light", "Trmt", "mean", "sd", "ste", "Conc", "Group"), row.names = c(13L, 14L, 17L, 18L, 31L, 32L, 37L), class = "data.frame")
trtslopeplot<-ggplot(trtslope, aes(x=Group, y=mean, fill=Conc))+ geom_bar(stat="identity", position = position_dodge())+geom_errorbar(aes(ymin=mean-ste, ymax=mean+ste),position=position_dodge(.9),width=.2)
#I've tried to reorder the factors as I've done in the past, but that doesn't seem to work to change the plot
trtslope$Trmt <- factor( as.character(trtslope$Trmt), levels= c("None","10^-11Kin","10^-10Kin","10^-9Kin","10^-11IAA","10^-10IAA","10^-9IAA"))
trtslope <- trtslope[order(trtslope$Trmt),]

GGPlot geom_text coloring with facets

Hopefully someone here will be able to help me with a problem that I'm having with a ggplot script I'm trying to get right. The script will be used many times with different data, so it needs to be relatively flexible. I've got it almost where I want it, but I've come across a problem I haven't been able to solve.
The script is for a line graph with labels for each line in the right hand margin. Sometimes the graph is faceted, other times it is not.
The piece I'm having trouble with is that I would like to color code the labels in the right margin as black if there was no significant change over time, green if there was positive change, and red if there was negative change. I've got a script that works to carry this out when I only have a single facet, but as soon as I have multiple facets in the graph, the color coding of the labels gives the following error
Error: Incompatible lengths for set aesthetics:
Below is the script with data with multiple facets. The problem seems to be in the way that I'm specifying color in the geom_text line. If I delete the color call in the geom_text line in the script, then I get the attributes printed in the correct place, just not colored. I'm really at a loss on this one. This is my first post here, so let me know if I've done anything wrong with my post.
WITH MULTIPLE FACETS (DOES NOT WORK)
require(ggplot2)
require(grid)
require(zoo)
require(reshape)
require(reshape2)
require(directlabels)
time.data<-structure(list(Attribute = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("Taste 1", "Taste 2", "Taste 3",
"Use 1", "Use 2", "Use 3"), class = "factor"), Attribute.Category = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Nutritional/Usage",
"Taste/Quality"), class = "factor"), Attribute.Order = c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), Category.Order = c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), Color = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("#084594",
"#2171B5", "#4292C6", "#6A51A3", "#807DBA", "#9E9AC8"), class = "factor"),
value = c(75L, 78L, 90L, 95L, 82L, 80L, 43L, 40L, 25L, 31L,
84L, 84L), Date2 = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L), .Label = c("1/1/2013", "9/1/2012"), class = "factor")), .Names = c("Attribute",
"Attribute.Category", "Attribute.Order", "Category.Order", "Color",
"value", "Date2"), class = "data.frame", row.names = c(NA, -12L
))
label.data<-structure(list(7:12, Attribute = structure(1:6, .Label = c("Taste 1",
"Taste 2", "Taste 3", "Use 1", "Use 2", "Use 3"), class = "factor"),
Attribute.Category = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Nutritional/Usage",
"Taste/Quality"), class = "factor"), Attribute.Order = 1:6,
Category.Order = c(1L, 1L, 1L, 2L, 2L, 2L), Color = structure(1:6, .Label = c("#084594",
"#2171B5", "#4292C6", "#6A51A3", "#807DBA", "#9E9AC8"), class = "factor"),
Significance = structure(c(2L, 3L, 1L, 1L, 3L, 2L), .Label = c("neg",
"neu", "pos"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = "1/1/2013", class = "factor"),
value = c(78L, 95L, 80L, 40L, 31L, 84L), Date2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = "2013-01-01", class = "factor"),
label.color = structure(c(1L, 2L, 3L, 3L, 2L, 1L), .Label = c("black",
"forestgreen", "red"), class = "factor")), .Names = c("",
"Attribute", "Attribute.Category", "Attribute.Order", "Category.Order",
"Color", "Significance", "variable", "value", "Date2", "label.color"
), class = "data.frame", row.names = c(NA, -6L))
color.palette<-as.character(unique(time.data$Color))
time.data$Date2<-as.Date(time.data$Date2,format="%m/%d/%Y")
plot<-ggplot()+
geom_line(data=time.data,aes(as.numeric(time.data$Date2),time.data$value,group=time.data$Attribute,color=time.data$Color),size=1)+
geom_text(data=label.data,aes(x=Inf, y=label.data$value, label=paste(" ",label.data$Attribute)),
color=label.data$label.color,
size=4,vjust=0, hjust=0,na.rm=T)+
facet_grid(Attribute.Category~.,space="free")+
theme_bw()+
scale_x_continuous(breaks=as.numeric(unique(time.data$Date2)),labels=format(unique(time.data$Date2),format = "%b %Y"))+
theme(strip.background=element_blank(),
strip.text.y=element_blank(),
legend.text=element_blank(),
legend.title=element_blank(),
plot.margin=unit(c(1,5,1,1),"cm"),
legend.position="none")+
scale_colour_manual(values=color.palette)
gt3 <- ggplot_gtable(ggplot_build(plot))
gt3$layout$clip[gt3$layout$name == "panel"] <- "off"
grid.draw(gt3)
Some problems:
Inside your aesthetic declarations, you should not be referencing the data columns as time.data$Date2, but just as Date2. The data argument specifies where to look for that information (which needs to all be in the same data.frame for a given layer, but, as you take advantage of, can vary layer to layer).
In the geom_text call, color was not inside the aes call; if you are mapping it to data which is in the data.frame, you have to have it inside the aes call. This would throw a different error after fixing the first part because then it would not be able to find label.color anywhere because it would not know to look inside label.data.
Fixing those, then the scale_colour_manual complains that there are 9 colors and you have only supplied 6. That is because there are 6 colors from the lines and 3 from the text. Since you specified these as actual color names, you can just use scale_colour_identity.
Putting this all together:
plot <- ggplot()+
geom_line(data=time.data, aes(as.numeric(Date2), value,
group=Attribute, color=Color),
size=1)+
geom_text(data=label.data, aes(x=Inf, y=value,
label=paste(" ",Attribute),
color=label.color),
size=4,vjust=0, hjust=0)+
facet_grid(Attribute.Category~.,space="free") +
scale_x_continuous(breaks=as.numeric(unique(time.data$Date2)),
labels=format(unique(time.data$Date2),format = "%b %Y")) +
scale_colour_identity() +
theme_bw()+
theme(strip.background=element_blank(),
strip.text.y=element_blank(),
legend.text=element_blank(),
legend.title=element_blank(),
plot.margin=unit(c(1,5,1,1),"cm"),
legend.position="none")
gt3 <- ggplot_gtable(ggplot_build(plot))
gt3$layout$clip[gt3$layout$name == "panel"] <- "off"
grid.draw(gt3)
To get an idea how much you can strip down your example, this is much closer to minimal:
time.data <-
structure(list(Attribute = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L), .Label = c("Taste 1", "Taste 2", "Use 1", "Use 2"), class = "factor"),
Attribute.Category = structure(c(2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("Nutritional/Usage", "Taste/Quality"), class = "factor"),
Color = c("#084594", "#084594", "#2171B5", "#2171B5", "#6A51A3",
"#6A51A3", "#807DBA", "#807DBA"), value = c(75L, 78L, 90L,
95L, 43L, 40L, 25L, 31L), Date2 = structure(c(15584, 15706,
15584, 15706, 15584, 15706, 15584, 15706), class = "Date")), .Names = c("Attribute",
"Attribute.Category", "Color", "value", "Date2"), row.names = c(NA,
-8L), class = "data.frame")
label.data <-
structure(list(value = c(78L, 95L, 40L, 31L), Attribute = structure(1:4, .Label = c("Taste 1",
"Taste 2", "Use 1", "Use 2"), class = "factor"), label.color = c("black",
"forestgreen", "red", "forestgreen"), Attribute.Category = structure(c(2L,
2L, 1L, 1L), .Label = c("Nutritional/Usage", "Taste/Quality"), class = "factor"),
Date2 = structure(c(15706, 15706, 15706, 15706), class = "Date")), .Names = c("value",
"Attribute", "label.color", "Attribute.Category", "Date2"), row.names = c(NA,
-4L), class = "data.frame")
ggplot() +
geom_line(data = time.data,
aes(x=Date2, y=value, group=Attribute, colour=Color)) +
geom_text(data = label.data,
aes(x=Date2, y=value, label=Attribute, colour=label.color),
hjust = 1) +
facet_grid(Attribute.Category~.) +
scale_colour_identity()
The theme stuff (and the making the labels visible outside the plot) isn't relevant to the question, nor is the x-axis conversions from Date to numeric to handle having Inf. I also trimmed the data to just the needed columns, and reduced categorical variable to only two categories.

change border from around legend from a scatterplot

This should be simple, but I can't figure out how to remove the border from around my legend. I would also like to place the legend within the graph and remove the inner grid lines and the top and left side border. I am using the scatterplot function and this is the code I've written thus far:
scatterplot(Comp1~ln1wr|Season, moose,
xlab = "Risk", ylab = "Principal component 1",
labels= row.names(moose), by.groups=T, smooth=F, boxplots=F, legend.plot=F)
legend("bottomleft", moose, fill=0)
Here I was just experimenting to even see if I could get the legend to be placed somewhere else, but each time I run this code, I get an error
Error in as.graphicsAnnot(legend) :
argument "legend" is missing, with no default
I would like to place the legend within the graph, but where it will not conflict with the data displaying. here is sample data:
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 32L, 33L,
33L, 34L, 34L, 34L), .Label = c("F07001", "F07002", "F07003",
"F07004", "F07005", "F07006", "F07008", "F07009", "F07010", "F07011",
"F07014", "F07015", "F07017", "F07018", "F07019", "F07020", "F07021",
"F07022", "F07023", "F07024", "F10001", "F10004", "F10008", "F10009",
"F10010", "F10012", "F10013", "F98015", "M07007", "M07012", "M07013",
"M07016", "M10007", "M10011", "M10015"), class = "factor"), Season = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("SUM", "WIN"
), class = "factor"), Time = structure(c(1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L), .Label = c("day", "night"), class = "factor"),
Repro = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("f", "fc", "m"), class = "factor"), Comp1 = c(-0.524557195,
-0.794214153, -0.408247216, -0.621285004, -0.238828585, 0.976634392,
-0.202405922, -0.633821539, -0.306163898, -0.302261589, 1.218779672
), ln1wr = c(0.833126490613386, 0.824526258616325, 0.990730077688989,
0.981816265754353, 0.933462450382474, 1.446048015519, 1.13253050687157,
1.1349442179155, 1.14965388471562, 1.14879830358128, 1.14055365645628
)), .Names = c("ID", "Season", "Time", "Repro", "Comp1",
"ln1wr"), row.names = c(1L, 2L, 3L, 4L, 5L, 220L, 221L, 222L,
223L, 224L, 225L), class = "data.frame")
I would suggest
par(bty="l",las=1)
scatterplot(Comp1~ln1wr|Season, moose,
xlab = "Risk", ylab = "Principal component 1",
labels= row.names(moose),
by.groups=TRUE, smooth=FALSE, boxplots=FALSE,
grid=FALSE,
legend.plot=FALSE)
legend("bottomright", title="Season",
legend=levels(moose$Season), bty="n",
pch=1:2, col=1:2)
As indicated in ?legend, bty controls the legend box -- "n" means "none.
I put the legend in the bottom right rather than in the bottom left because it seems to avoid your data better that way.
I used bty="l" to eliminate the top and right box edges (this means "box type L")
I used las=1 to get the y-axis tick labels horizontal -- you didn't ask for that but I strongly prefer it
grid=FALSE removes the internal grid lines
You have to unique your moose ID as you have more than one point for each moose.
legend("bottomleft",legend=unique(moose))
Then you have to associate a color and a point type to your legend (corresponding to your moose ID in your plot). I would also have a look at plot() instead of scatterplot().

Resources