0

I'm working on this df

Col0 <- c("AA", "BB", "CC", "DD","EE","FF")
    Col1 <- c(2,2,2,6,1,1)
    Col2 <- c(2,2,2,1,3,4)
    Col3 <- c(2,2,3,4,6,6)
    Col4 <- c(2,2,3,1,2,1)
    Col5 <- c(2,1,1,1,1,4)
    Col6 <- c(2,4,2,5,4,4)
    Col7 <- c(2,4,2,5,4,4)
    Col8 <- c(2,2,3,4,5,4)
    Col9 <- c(1,3,3,2,2,2)
    df<-data.frame(Col0,Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9)

And using facet I created a graph

library(ggplot2)
library(tidyr)

pl<-df %>%
  gather(Hi, Val, -Col0) %>%
  ggplot(aes(Hi, Val, group = Col0, col = Col0)) + facet_grid(Col0 ~ .)

pl<- pl + geom_line() +theme(
  axis.text.x = element_text(angle = 90, hjust = 1))+ theme( panel.border = element_rect(colour = "black", fill=NA, size=1),legend.direction ="vertical",legend.position = "right")+guides(fill=guide_legend(ncol=1))+scale_y_continuous(labels=comma) +theme(legend.text = element_text(size=6))  

print(pl)

The question is is possible instead of using Col0 pass to the functions the value:

value<- names(df[1])

Because i"m working on a lot of df and i'd like to generalize the function

mtoto
  • 23,919
  • 4
  • 58
  • 71
juse
  • 51
  • 1
  • 7
  • 1
    In `gather` or `ggplot`? Check out `aes_string` for ggplot2 and `gather_` in tidyr. – aosmith Jun 16 '16 at 16:21
  • I need it in both of them, I'd need to use `value` everytime appears Col0 – juse Jun 16 '16 at 16:24
  • @aosmith I'm new with R, if you could provide me a solved example you'll really save my life – juse Jun 16 '16 at 16:38
  • @juse check these links out http://stats.stackexchange.com/questions/177129/ggplot-and-loops & http://chem.sites.mtu.edu/mazzoleni/index.php/2016/02/20/looping-functions-with-ggplot2/ – Tung Jun 16 '16 at 16:50

1 Answers1

1

To work with strings in a function, you will need the standard evaluation versions of ggplot2 and tidyr functions. In tidyr, these end with an underscore. In ggplot2 you will want aes_string (or possibly aes_).

The standard evaluation portion of tidyr would look something like this:

df %>% gather_("Hi", "Val", select_vars_(names(.), 
                                         names(.), 
                                         exclude = "Col0"))

Everything in gather_ is a string. The complicated part of the code using select_vars_ is because you want to exclude the columns. See here.

For the plot, you can simply change out aes for aes_string and use strings for variable names. The trickier part is how to use a string in facet_grid, which can be done using formula as shown here.

The change to that part of the code would look like:

ggplot(aes_string("Hi", "Val", group = "Col0", col = "Col0")) + 
    facet_grid(as.formula(paste("Col0", "~.")))

All that's left is putting this into a function.

plotfun = function(data, column) {
    data %>%
        gather_("Hi", "Val", select_vars_(names(.), 
                                          names(.), 
                                          exclude = column)) %>%
        ggplot(aes_string("Hi", "Val", group = column, col = column)) + 
        facet_grid(as.formula(paste(column, "~."))) + 
        geom_line() +
        theme(axis.text.x = element_text(angle = 90, hjust = 1),
              panel.border = element_rect(colour = "black", fill=NA, size=1),
              legend.direction ="vertical",
              legend.position = "right",
              legend.text = element_text(size=6)) + 
        guides(fill=guide_legend(ncol=1)) +
        scale_y_continuous(labels=comma)
}

plotfun(df, "Col0")

enter image description here

Community
  • 1
  • 1
aosmith
  • 34,856
  • 9
  • 84
  • 118