I am attempting to make a plots for each column of my dataframe and pass on the column name to be the title for each plot. There are a total of 72 columns that need their own individual plot. Facet_wrap is not an appropriate solution to this problem.
Running the top code gives me the individual plots with the incorrect name. This returns the first row value from i column. I want to return the column name. plot with first row value for name
Is there a way I can automatically pull column names into the title for each iteration?
Here is my subset of my data
Wisconsin_GR <- read.table(header=TRUE, text="
Species Adams Ashland Barron Bayfield Brown
Ash -.5889 4.1211 5.6036 26.8347 NA
Aspen -.5867 15.82 .4329 1.1622 NA
")
The code that returns a plot with the first row value of i column.
for( i in Wisconsin_GR[2:6]){
print(
gf_point(i~Species, data = test)%>%
gf_labs(title=i,
y = "Growth to Removal Ratio",
x = "")+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
scale_y_continuous(expand = c(0,0), limit = c(-5,100)))
}
The code below is altered to pull column names which works fine for the call names(Wisconsin_GR) but returns the following error when input into the code.
Error:
x Can't convert from to due to loss of precision.
Caused by error in stop_vctrs():
! Can't convert from to due to loss of precision.
for( i in Wisconsin_GR[2:6]){
print(
gf_point(i~Species, data = test)%>%
gf_labs(title=names(Wisconsin_GR[i]),
y = "Growth to Removal Ratio",
x = "")+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
scale_y_continuous(expand = c(0,0), limit = c(-5,100)))
}
Not sure how many rows of data you have, because this code is not fast, but works.
Note the if condition I added, that might be problematic but not too complicated to deal with.
And it's always better to state where your data is from (it's from
ggformula, but we don't need to check ourselves).