Reputation: 109
I really need serious help here. I have been working with R the last month. I have been analyzing data, and plotting many visualizations using ggplot2. I have many .r
files which includes data transformations and reading .csv ..etc
My instructor wants me to show him my code or at least he want to execute and get all the figures I did. Can i run .r scripts (that includes codes to generate ggplot figures) on my instructor machine? - the good thing, the instructor machine has R too.
PS: The result can be as saving image to .JPEG to the instructor's machine.
Your help is much appreciated.
Thanks!
Upvotes: 2
Views: 1646
Reputation: 7832
First, you need to ensure that the R software where you will run your scripts, has all required packages installed. Second, make sure the data is somehow available. This should usually make all scripts runnable.
If you want to save ggplot-figures, use the ggsave
-function after each plot is generated. This will save the last plot into the working directory (unless else specified).
Depending on how you wrote your code, you may create an "executable" script that calls all your relevant r-scripts via source
command, so only one r-script needs to be compiled / sourced, and all others are automatically sourced from this "basefile".
An example of an "executable" script, which I usually create when working on new projects:
# --------------------------------
# load required libraries
# --------------------------------
library(sjmisc)
library(sjPlot)
# --------------------------------
# load data
# --------------------------------
david <- read_spss("Pat_Dateneingabe150511_MR_DL.sav")
# --------------------------------
# do all necessary recoding
# --------------------------------
source("recodes.R")
# --------------------------------
# create new variables / scales
# --------------------------------
source("Skalenbildung.R")
In this case, I have two large scripts recodes.R and Skalenbildung.R, which are sourced from this "base"-script. You could then add another script plots.R or whatever you may call it, and this script then contains all ggplot
-commands to create the figures (don't forget to add library(ggplot2)
). After each ggplot(...)
add a ggsave(...)
to save each plot.
Instead of using ggsave
, you can also easily print all plots to a PDF file:
# --------------------------------
# plot all figures and save them to PDF
# --------------------------------
pdf("all-plots.pdf", width = 9, height = 7)
source("ggplot-figures.R")
dev.off()
In the ggplot-figures.R, you have all your ggplot-code that creates the plots you need, and these are saved to a single PDF.
Upvotes: 2