phiiipsm m@iii@g oii cp@@ei1@stormweb@@et
2018-Sep-05 02:08 UTC
[R] Multi-word column names in a data frame
I am having trouble working with column names in a data frame. My column names are multi-word text strings and I like it that way. I want to loop through the columns, plotting graphs for each one, and I want to use the column names in the chart labels and in the file names when I save the charts. Here is a simple reproducible example that does not work. library(dplyr) `RefDate` <- as.Date(c("2010-11-1","2010-12-01","2011-01-01")) `Number of vegetables` <- c(14,23,45) `Number of people` <- c(20,30,40) MyData <- data.frame(RefDate,`Number of vegetables`,`Number of people`,check.names=FALSE) MyVars <- c("Number of vegetables","Number of people") for (A in MyVars) { g2 <- ggplot(MyData,aes(RefDate,eval(parse(text=A)))) + geom_line() + labs(title = paste(A," adjusted",sep="")) g2 ggsave(paste(A,".jpg",sep=""),g2,height=5,width=8,dpi=300) } Philip
Hi Philip, This may work: library(dplyr) `RefDate` <- as.Date(c("2010-11-1","2010-12-01","2011-01-01")) `Number of vegetables` <- c(14,23,45) `Number of people` <- c(20,30,40) MyData <- data.frame(RefDate,`Number_of_vegetables`, `Number_of_people`,check.names=FALSE) MyVars <- c("Number of vegetables","Number of people") My_Vars <- c("Number_of_vegetables","Number_of_people") nnames<-length(MyVars) for (i in 1:nnames) { g2 <- ggplot(MyData,aes(RefDate,eval(parse(text=My_Vars[i])))) + geom_line() + labs(title = paste(MyVars[i]," adjusted",sep="")) g2 ggsave(paste(A,".jpg",sep=""),g2,height=5,width=8,dpi=300) } Jim On Wed, Sep 5, 2018 at 1:22 PM <philipsm at cpanel1.stormweb.net> wrote:> > I am having trouble working with column names in a data frame. My > column names are multi-word text strings and I like it that way. I > want to loop through the columns, plotting graphs for each one, and I > want to use the column names in the chart labels and in the file names > when I save the charts. Here is a simple reproducible example that > does not work. > > library(dplyr) > `RefDate` <- as.Date(c("2010-11-1","2010-12-01","2011-01-01")) > `Number of vegetables` <- c(14,23,45) > `Number of people` <- c(20,30,40) > MyData <- data.frame(RefDate,`Number of vegetables`,`Number of > people`,check.names=FALSE) > MyVars <- c("Number of vegetables","Number of people") > for (A in MyVars) { > g2 <- ggplot(MyData,aes(RefDate,eval(parse(text=A)))) + geom_line() + > labs(title = paste(A," adjusted",sep="")) > g2 > ggsave(paste(A,".jpg",sep=""),g2,height=5,width=8,dpi=300) > } > > Philip > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
a) missing ggplot2 library b) cannot word wrap in the middle of a string in R without introducing newlines c) aes is not recommended for working with string variables as names... use aes_string d) Because aes_string will parse the string, you need to add the backticks e) paste0() is a shorter version of paste( sep="" ) ############################## library(dplyr) #> #> Attaching package: 'dplyr' #> The following objects are masked from 'package:stats': #> #> filter, lag #> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union library(ggplot2) `RefDate` <- as.Date(c("2010-11-1","2010-12-01","2011-01-01")) `Number of vegetables` <- c(14,23,45) `Number of people` <- c(20,30,40) MyData <- data.frame( RefDate , `Number of vegetables` , `Number of people` , check.names = FALSE ) MyVars <- c( "Number of vegetables", "Number of people" ) for ( A in MyVars ) { g2 <- ggplot( MyData , aes_string( x = RefDate , y = paste0( "`", A, "`" ) ) ) + geom_line() + labs( title = paste0( A, " adjusted" ) ) g2 ggsave( paste0( A, ".jpg" ) , g2 , height=5 , width=8 , dpi=300 ) } #' Created on 2018-09-05 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0). ###################################33 On Tue, 4 Sep 2018, philipsm at cpanel1.stormweb.net wrote:> I am having trouble working with column names in a data frame. My column > names are multi-word text strings and I like it that way. I want to loop > through the columns, plotting graphs for each one, and I want to use the > column names in the chart labels and in the file names when I save the > charts. Here is a simple reproducible example that does not work. > > library(dplyr) > `RefDate` <- as.Date(c("2010-11-1","2010-12-01","2011-01-01")) > `Number of vegetables` <- c(14,23,45) > `Number of people` <- c(20,30,40) > MyData <- data.frame(RefDate,`Number of vegetables`,`Number of > people`,check.names=FALSE) > MyVars <- c("Number of vegetables","Number of people") > for (A in MyVars) { > g2 <- ggplot(MyData,aes(RefDate,eval(parse(text=A)))) + geom_line() + > labs(title = paste(A," adjusted",sep="")) > g2 > ggsave(paste(A,".jpg",sep=""),g2,height=5,width=8,dpi=300) > } > > Philip > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k