Facetting in ggplot2

后端 未结 1 1289
情话喂你
情话喂你 2021-01-15 04:27

I have this dataset: https://dl.dropboxusercontent.com/u/73950/data.csv The dataset contains 3 variables.

Here\'s how I visualize the data right now:



        
1条回答
  •  粉色の甜心
    2021-01-15 05:03

    You can get this by putting the data in the format ggplot likes. In this case, a column that can be used to split the data into facets (called var below). To do that, I just repeated the data three times, choosing the appropriate x and y variables for each 2-way combo, and using the variable left out of each combination as the coloring variable.

    ## Rearrange the data by 2-way combinations, the coloring is the remaining column
    res <- do.call(rbind, combn(1:3, 2, function(ii)
        cbind(setNames(dat[,c(ii, setdiff(1:3, ii))], c("x", "y", "color")),
                       var=paste(ii, collapse=".")), simplify=F))
    
    ggplot(res, aes(x=x, y=y, color=color))+ geom_point(alpha = .2,size = 3) + 
      facet_wrap(~ var, scales="free") + sc
    

    0 讨论(0)
提交回复
热议问题