-1

I am trying to subset my input Table by 4th column, there is so many different strings:

a   1  3  led  a.1.3.led 10 38

a   3  4  led  a.3.4.led 11 40

b   1  2  dad  b.3.4.dad 20 125

b   2  4  dad  b.2.4.dad 80 222

c   1  10 lik  c.1.5.lik 100 250

And I want to separate it by different string in fourth column and plot for it graphs. Could you help me to subset it please?

Thank you

Vonton
  • 2,872
  • 4
  • 20
  • 27
  • 1
    This should help: https://stackoverflow.com/questions/18165578/subset-and-ggplot2 – AntoniosK Jan 19 '18 at 13:04
  • Or if you prefer one plot per category: https://www3.nd.edu/~steve/computing_with_data/13_Facets/facets.html – AntoniosK Jan 19 '18 at 13:06
  • 1
    You can use the answers from the link @AntoniosK provided for col4. If you're itnerested in working with col5, you may need to use regular expressions instead e.g. `ggplot(dat[ col5 %in% grep("*\\.[a-zA-Z]{3}$", dat$col5, val = T), ]) + geom_point(...)`. – Gautam Jan 19 '18 at 13:13
  • 1
    `split(df, df[,4])` – Onyambu Jan 19 '18 at 14:54

1 Answers1

0

It's not totally clear to me what you want, but is it something like this,

df <- structure(list(V1 = structure(c(1L, 1L, 2L, 2L, 3L), .Label = c("a", 
"b", "c"), class = "factor"), V2 = c(1L, 3L, 1L, 2L, 1L), V3 = c(3L, 
4L, 2L, 4L, 10L), V4 = structure(c(2L, 2L, 1L, 1L, 3L), .Label = c("dad", 
"led", "lik"), class = "factor"), V5 = structure(c(1L, 2L, 4L, 
3L, 5L), .Label = c("a.1.3.led", "a.3.4.led", "b.2.4.dad", "b.3.4.dad", 
"c.1.5.lik"), class = "factor"), V6 = c(10L, 11L, 20L, 80L, 100L
), V7 = c(38L, 40L, 125L, 222L, 250L)), .Names = c("V1", "V2", 
"V3", "V4", "V5", "V6", "V7"), class = "data.frame", row.names = c(NA, 
-5L))
df
#>   V1 V2 V3  V4        V5  V6  V7
#> 1  a  1  3 led a.1.3.led  10  38
#> 2  a  3  4 led a.3.4.led  11  40
#> 3  b  1  2 dad b.3.4.dad  20 125
#> 4  b  2  4 dad b.2.4.dad  80 222
#> 5  c  1 10 lik c.1.5.lik 100 250
# install.packages(c("tidyverse"), dependencies = TRUE)
library(ggplot2)
ggplot(df, aes(x = V3, y = V6)) + geom_point() + facet_wrap(~V4)

facet_wrap ggplot by 4th col

or possibly

ggplot(subset(df, V4 %in% c("led" , "dad"))) +  
     geom_point(aes(x = V3, y = V6)) + 
     labs(title = "Only `led' , `dad' from the 4th column")

subset ggplot by 4th col

Eric Fail
  • 8,191
  • 8
  • 72
  • 128