Error in file(file, "r") : cannot open the connection - r

I have been trying to run this code and nothing is working but it did work a while ago. I do not know what I am doing wrong. I also combined a data set and this is the code I put(I do not know which is the issue)
The error I keep getting is on the line of "fit_CFA_groups", and it states
Error in file(file, "r") : cannot open the connection
3.
file(file, "r")
2.
scan(file = file, what = "", sep = ";", strip.white = TRUE, comment.char = "#")
1.
cfa(three_factor_groups, data = df3, group = "country")
This is the code I have put in
library(haven) #packages
library(dplyr)
library(vctrs)
library(haven)
NewIrishdata <- read_sav("NewIrishdata.sav")
df3 <- bind_rows(Englishdark, NewIrishdata)
df3
library(haven)
require(semTools)
library(semTools)
library(psych)
library(readr)
require(sem)
library(sem)
three_factor_groups <-
'General =~ Machiavellianism1 + Machiavellianism2 + Machiavellianism3 + Machiavellianism4 + Machiavellianism5 + Machiavellianism6 + Machiavellianism7 + Machiavellianism8 + Machiavellianism9
+ Narcissism1 + Narcissism2 + Narcissism3 + Narcissism4 + Narcissism5 + Narcissism6 + Narcissism7 + Narcissism8 + Narcissism9 + Psychopathy1 + Psychopathy2 + Psychopathy3 + Psychopathy4 + Psychopathy5 + Psychopathy6 + Psychopathy7 + Psychopathy8 + Psychopathy9'
fit_CFA_groups<-cfa(three_factor_groups, data= df3, group="country")
summary(fit_CFA_groups)
require(semPlot)
library(semPlot)
require(qgraph)
semPaths(fit_CFA_groups, layout='circle',style = "lisrel",what="std", combineGroups = F)

Related

(R) Error: Aesthetics must be either length 1 or the same as the data (6): x and y

I am working with the R programming language. I am trying to follow the instructions from this tutorial over here (https://www.nagraj.net/notes/gifs-in-r/) for animating 'gifs" in R.
Using the built in "mtcars" dataset, I made the two following graphs:
library(ggplot2)
#graph 1
LINES <- data.frame(ind = 1, Startx=c(1,1,1,3,5,6),#horiz lines then vert lines
Starty=c(15,20,35,7,7,7),
Endx=c(3,5,6,3,5,6),
Endy=c(15,20,35,15,20,35))
a = ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()+
geom_segment(aes(x=Startx,y=Starty,xend=Endx,yend=Endy),data=LINES)+
coord_cartesian(ylim=c(9,36),xlim = c(1.5,6.2))
#graph 2
LINES_1 <- data.frame(ind = 2, Startx=c(2,2,2,4,6,7),#horiz lines then vert lines
Starty=c(16,21,36,8,8,8),
Endx=c(4,6,7,4,6,7),
Endy=c(16,21,36,16,21,36))
b = ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()+
geom_segment(aes(x=Startx,y=Starty,xend=Endx,yend=Endy),data=LINES_1)+
coord_cartesian(ylim=c(9,36),xlim = c(1.5,6.2))
Now, I am trying to follow the instructions from the tutorial and turn these two graphs into an "animation gif":
#create joint dataset
Lines_frame = rbind(LINES, LINES_1)
library(magick)
## create a directory to which the images will be written
dir_out <- file.path(tempdir(), "tx-sales")
dir.create(dir_out, recursive = TRUE)
for (y in Lines_frame$ind) {
p <-
Lines_frame %>%
filter(ind == y) %>%
ggplot(aes(mtcars$wt,mtcars$mpg))
theme_minimal() +
labs(x = "wt", y = "mpg")
fp <- file.path(dir_out, paste0(y, ".png"))
ggsave(plot = p,
filename = fp,
device = "png")
}
## list file names and read in
imgs <- list.files(dir_out, full.names = TRUE)
img_list <- lapply(imgs, image_read)
## join the images together
img_joined <- image_join(img_list)
## animate at 2 frames per second
img_animated <- image_animate(img_joined, fps = 2)
## view animated image
img_animated
## save to disk
image_write(image = img_animated,
path = "tx-sales.gif")
But this returns the following error:
Saving 3.55 x 4.2 in image
Error: Aesthetics must be either length 1 or the same as the data (6): x and y
Run `rlang::last_error()` to see where the error occurred.
Can someone please show me what I am doing wrong? Is there an easier way to do this?
Thanks
library(gganimate)
# add an id row so that gganimate knows which line in the first
# frame matches the ones in the 2nd
Lines_frame = rbind(LINES, LINES_1) %>%
group_by(ind) %>% mutate(id = row_number()) %>% ungroup()
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() +
geom_segment(aes(x=Startx,y=Starty,xend=Endx,yend=Endy, group = id),
data=Lines_frame)+
coord_cartesian(ylim=c(9,36),xlim = c(1.5,6.2)) +
transition_reveal(ind)
There are a few transition options. For instance, you could alternate back and forth between the two states with transition_states and make the movement tween with a bounce, because why not:
# wrapping in `animate()` to access its parameters, like frames per second (fps)
animate(
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()+
geom_segment(aes(x=Startx,y=Starty,xend=Endx,yend=Endy),
data=Lines_frame)+
coord_cartesian(ylim=c(9,36),xlim = c(1.5,6.2)) +
transition_states(ind, state_length = 0.2) +
ease_aes('bounce-in'),
fps = 20, nframes = 200, height = 400, width = 600)

Tricky Figure: Plot values of two variable on same x-axis in R or excel

I am trying to make a figure using this excel Data herelike this , but I am unable to put two variables on x-axis with different values.
library(xlsx2)
test <- read.xlsx2("E:/Plot/oz.xlsx",1, header=TRUE)
test$Ozone = as.numeric(as.character(test$Ozone))
test$Altitude = as.numeric(as.character(test$Altitude))
test$Pressure = as.numeric(as.character(test$Pressure))
round(test$Altitude) # i want to round the values of Altitude
library(ggplot2)
ylim.prim <- c(0, 34) # in this example, precipitation
ylim.sec <- c(1010, 10)
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
ggplot(test, aes(x = Ozone, y = Altitude,color = 'Ozone Partial Pressure (mPa)'))+
geom_path(aes(x = Ozone))+ scale_y_continuous(name="Altitude (km)",sec.axis=sec_axis(~(.- a)/b, name = 'Pressure (hPa)'))+
scale_x_continuous(name="Temperature (km)",sec.axis=sec_axis(~(.), name = 'Temperature (C)'))+
theme_test() + theme(legend.position = c(0.01, 0.14),legend.justification = c(0, -4))
but I get a plot like this .
I would appreciate any help at this stage. Thank you
If you want two labels per break, you can seperate them with \n. You can do this manually, or if you know the transformation with a function provided to the labels argument of the scale. From the example figure I glanced that temperature is 4x - 100 wherein x is the ozon label. However, temperature and ozone don't appear to be colinear from your data.
library(ggplot2)
# Downloaded from google sheets as tsv
file <- file.choose()
df <- read.table(file, sep = "\t", header = TRUE)
# Per example figure
ozone2temp <- function(x){x * 4 - 100}
# Simplified for brevity
ggplot(df) +
geom_path(aes(Ozone, Altitude, colour = "Ozone")) +
scale_x_continuous(
labels = function(x) {
paste(x, ozone2temp(x), sep = "\n")
},
name = "Ozone\nTemp"
)
Created on 2021-08-10 by the reprex package (v1.0.0)
EDIT:
If you want to also plot altitude with temperature as the x-variable, you need the reverse transformation as well:
library(ggplot2)
# Downloaded from google sheets as tsv
file <- file.choose()
df <- read.table(file, sep = "\t", header = TRUE)
# Per example figure
ozone2temp <- function(x){x * 4 - 100}
temp2ozone <- function(x){(x + 100) / 4}
ggplot(df, aes(y = Altitude)) +
geom_path(aes(Ozone, colour = "Ozone")) +
geom_path(aes(temp2ozone(Temperature),
colour = "Temperature")) +
scale_x_continuous(
labels = function(x) {
paste(x, ozone2temp(x), sep = "\n")
},
name = "Ozone\nTemp"
)
Created on 2021-08-10 by the reprex package (v1.0.0)

How to use a variable in for loop as input to a function

As I learn R, I am trying to use it in my work. I needed to plot 142 plots and do some stats. Therefore I decided to write the following function:
Melting <- function(df){
i <- as.character(substitute(df))
df.m<-melt(df)
library(grid, lib.loc = "C:/Program Files/R/R-3.6.2/library")
name_plot = paste(i,".png",sep = "")
png(name_plot, width = 10, height = 9, units = 'in', res = 700)
print(ggplot(data = df.m, aes(x=Tissuetype, y=value)) + geom_boxplot(aes(fill=variable)) + stat_compare_means(aes(group = variable), label = "p.signif") + theme(axis.text.x = element_text(angle = 90)))
dev.off()
}
The function runs fine and takes one of the dataframe present in my environment as an input. For eg: Melting(liver)
Having tested that the function successfully plot the graph, I decided to put it in a for loop as I did in bash scripting. However, it throws the following error:
for(i in names){Melting(i)}
Error in FUN(X[[i]], ...) : object 'variable' not found
The names vector was created as follows
files = list.files(pattern = "*.csv")
names <-tools::file_path_sans_ext(files)
I have a hunch that the value of I in for loop is getting substituted as string eg: Melting("liver") rather than an argument Melting(liver). But I don't know how to get over it. I read several posts but couldn't understand. Please help
All thanks to #Valerifor for helping me out and #Ronak too. The final code is as follows for those who might face same problem
#Reading multiple file list
files = list.files(pattern = "*.csv")
#first making a list of all data frames we wish to have
l <- as.list(tools::file_path_sans_ext(files))
#Loading all files as dataframes together
for(i in l){
filepath <- file.path("E:/shantanu_new_tanta/tissue/facet_data/",paste(i,".csv",sep=""))
assign(i, read.table(filepath,header = FALSE, col.names = c("Tissuetype", "E", "P"), sep = "\t"))
}
#Defining a function that plots boxplot with significance values
Melting <- function(df, name_png){
df.m<-melt(df)
library(grid, lib.loc = "C:/Program Files/R/R-3.6.2/library")
name_plot = paste(name_png,".png",sep = "")
png(name_plot, width = 10, height = 9, units = 'in', res = 700)
print(ggplot(data = df.m, aes(x=Tissuetype, y=value)) + geom_boxplot(aes(fill=variable)) + stat_compare_means(aes(group = variable), label = "p.signif") + theme(axis.text.x = element_text(angle = 90)))
dev.off()
}
#Running function on all files in a single go
for(i in 1:3){Melting(get(l[[i]]), l[[i]])}

How do I export multiple plots as png files from R?

I have an R script that creates multiple trend plots from tabular data. I need to export each plot as a png file. I have searched and tried (to no avail) using png(). It seems like this should be a relatively easy fix. My code is below. Could someone please offer some suggestions as to how I might solve this dilemma?
library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(magrittr)
library(stringi)
library(lubridate)
library(stats)
#load in datafiles
c_data <- read_csv ("C:/Projects/A_AX_tech_memo/data_analysis/AAX_data_2017_dtcts.csv")
C_data_out <-
c_data %>%
group_by(METHOD_NAME, STD_CON_LONG_NAME, SAMP_SITE_NAME, FILTERED_FLAG) %>%
ungroup() %>%
select(METHOD_NAME, STD_CON_LONG_NAME, SAMP_SITE_NAME, SAMP_DATE, STD_VALUE_RPTD, STD_ANAL_UNITS_RPTD, FILTERED_FLAG, STD_REPORTING_LIMIT, STD_REQUIRED_DETECTION_LIMIT, LAB_QUALIFIER) %>%
rename(Method = METHOD_NAME, Constit = STD_CON_LONG_NAME, Well = SAMP_SITE_NAME, Date = SAMP_DATE, Value = STD_VALUE_RPTD, Unit = STD_ANAL_UNITS_RPTD, Filtered = FILTERED_FLAG, MDL = STD_REPORTING_LIMIT, RDL = STD_REQUIRED_DETECTION_LIMIT, Flag =LAB_QUALIFIER) %>%
mutate(Date = mdy(Date))
dfs <- split(C_data_out, with(C_data_out, interaction(Well, Constit, Filtered)), drop = TRUE)
plotz <- lapply(dfs, function(x){
ggplot(data = x, aes(Date, Value)) +
geom_point(data = x, aes(color = Flag), size = 3) +
ggtitle(paste(x$Well, x$Constit, x$Filtered, sep =".")) +
ylab("ug/L or Pci/L") +
geom_smooth(method = "lm", se = FALSE, rm.na = TRUE) +
geom_hline(aes(yintercept=x$MDL, linetype="MDL"), color ="dark green", lwd=1, lty=2) +
geom_hline(aes(yintercept=x$RDL, linetype="RDL"), color ="purple", lwd=1, lty=2)
})
Here is an example of what I have tried with png:
names <- lapply(dfs, function(x){
ggtitle(paste(x$Well, x$Constit, x$Filtered, sep ="."))
})
plotz <- lapply(dfs, function(x){
mypath <- file.path("C:","plots", paste(names[i], ".png", sep = ""))
png(file=mypath)
ggplot(data = x, aes(Date, Value)) +
geom_point(data = x, aes(color = Flag), size = 3) +
ggtitle(paste(x$Well, x$Constit, x$Filtered, sep =".")) +
ylab("ug/L or Pci/L") +
geom_smooth(method = "lm", se = FALSE, rm.na = TRUE) +
geom_hline(aes(yintercept=x$MDL, linetype="MDL"), color ="dark green", lwd=1, lty=2) +
geom_hline(aes(yintercept=x$RDL, linetype="RDL"), color ="purple", lwd=1, lty=2)
dev.off()
})
This gives me this error:
Error in paste(names[i], ".png", sep = "") : object 'i' not found
Any and all help would be much appreciated.
This snippet works using the map and walk functions from purrr.
mtcars_split <-
mtcars %>%
split(.$cyl)
paths <-
paste0(names(mtcars_split),".png")
plots <-
mtcars_split %>%
map(~ ggplot(data=.,mapping = aes(y=mpg,x=wt)) + geom_point())
pwalk(list(filename=paths,plot=plots),ggsave)

R - Passing data frame to function

R beginner here. I'm trying to write a function on my own which has a data frame as an argument and then reorders the data frame and then uses ggplot. I've been struggling with trying to get the function to work and somehow I can't seem to find the answer I'm looking for.
The first code I had was this,
pareto_plot <- function(pareto_data, title, x_label, y_label, filename){
pareto_calc = pareto_data[order(-pareto_data[2]),]
colnames(pareto_calc) = c("sku", "volume")
pareto_calc$sku_perc = 1/length(pareto_calc$sku)
pareto_calc$sku_cum = cumsum(pareto_calc$sku_perc)
pareto_calc$vol_perc = pareto_calc$volume/sum(pareto_calc$volume)
pareto_calc$vol_cum = cumsum(pareto_calc$vol_perc)
ggplot(pareto_calc, aes(x=pareto_data$sku_cum, y=pareto_data$vol_cum)) + geom_line(col="blue") +
geom_line(y=0.8, col="red") +geom_line(x=0.2, col="red") +
ggtitle(title) + ylab(y_label) + xlab(x_label)
ggsave(paste(filename,".png", sep=""))
}
When I used the above code, I got an error,
Error in eval(expr, envir, enclos) : object 'pareto_calc' not found
I then changed the code to make use of data as i saw that a lot of examples online made use of it as an argument. My modified code was now,
pareto_plot <- function(data, title, x_label, y_label, filename){
pareto_data = data
pareto_data[order(-pareto_data[2]),]
colnames(pareto_data) = c("sku", "volume")
pareto_data$sku_perc = 1/length(pareto_data$sku)
pareto_data$sku_cum = cumsum(pareto_data$sku_perc)
pareto_data$vol_perc = pareto_data$volume/sum(pareto_data$volume)
pareto_data$vol_cum = cumsum(pareto_data$vol_perc)
ggplot(pareto_data, aes(x=pareto_data$sku_cum, y=pareto_data$vol_cum)) + geom_line(col="blue") +
geom_line(y=0.8, col="red") +geom_line(x=0.2, col="red") +
ggtitle(title) + ylab(y_label) + xlab(x_label)
ggsave(paste(filename,".png", sep=""))
}
With this code, I now get the error,
Error in exists(name, envir = env, mode = mode) :
argument "env" is missing, with no default
Any help will be greatly appreciated. Thanks in advance! :)
When you make a function, it is often easiest to write the code first, without making it a function, until you are sure it works. Then wrap it as a function.
set.seed(33)
df <- data.frame(V1 = runif(10),
V2 = rnorm(10))
pareto_plot <- function(data, title, x_label, y_label, filename){
pareto_data <- data[order(-data[2]),] #you forgot to assign it
names(pareto_data) <- c("sku", "volume")
pareto_data$sku_perc <- 1/length(pareto_data$sku)
pareto_data$sku_cum <- cumsum(pareto_data$sku_perc)
pareto_data$vol_perc <- pareto_data$volume/sum(pareto_data$volume)
pareto_data$vol_cum <- cumsum(pareto_data$vol_perc)
ggplot(pareto_data, aes(x=sku_cum, y=vol_cum)) + geom_line(color="blue") +
geom_line(y=0.8, col="red") +geom_line(x=0.2, col="red") +
ggtitle(title) + ylab(y_label) + xlab(x_label)
ggsave(paste(filename,".png", sep=""))
}

Resources