1

I want to plot xExp and yExp on the x-axis, with the Yes/Nos as a stacked fill, and the xDur, yDur as the y-axis. Is this possible? Exp = experience and Dur = duration.

This is what I've most recently tried:

ggplot(md1,aes(variable,value,fill=str_detect("Exp")))+
geom_bar(stat="identity")

I melted the dataframe and filtered it and now it looks as below. I have a lot more Exp and Dur values than this, would it make sense to just keep them as separate columns?

md1 <- md %>% 
filter(str_detect(variable, paste(c("Exp","Dur"),collapse = '|')))

Subject variable    value
1   xExp    Yes
2   xExp    No
3   xExp    Yes
4   xExp    Yes
5   xExp    No
1   xDur    2
2   xDur    NA
3   xDur    1
4   xDur    10
5   xDur    NA
1   yExp    No
2   yExp    No
3   yExp    Yes
4   yExp    Yes
5   yExp    No
1   yDur    NA
2   yDur    5
3   yDur    8
4   yDur    2
5   yDur    NA

Thank you for any help! I have been searching on here and couldn't find exactly what I needed, but perhaps I was searching for the wrong thing.

Grace
  • 55
  • 5
  • There are dozens of Exp and Dur variable so I melted it to make it in long format, but this combined values of different types. I'm trying to plot them such as here: https://stackoverflow.com/questions/22305023/how-to-get-a-barplot-with-several-variables-side-by-side-grouped-by-a-factor – Grace May 02 '18 at 14:42

1 Answers1

1
txt <- tibble::tribble(
  ~Subject,~variable,~value,
           1,   "xExp"   , "Yes",
            2,   "xExp"  ,  "No",
           3 ,  "xExp"   , "Yes",
           4 ,  "xExp"   , "Yes",
            5  , "xExp"  ,  "No",
             1,   "xDur" ,   "2",
            2 ,  "xDur"  ,  "NA",
             3,   "xDur" ,   "1",
            4 ,  "xDur"  ,  "10",
            5 ,  "xDur"  ,  "NA",
            1 ,  "yExp"  ,  "No",
            2 ,  "yExp"  ,  "No",
           3  , "yExp"   , "Yes",
           4  , "yExp"  ,  "Yes",
            5 ,  "yExp"  ,  "No",
            1 ,  "yDur"  ,  "NA",
             2 ,  "yDur"  ,  "5",
             3 ,  "yDur" ,   "8",
             4 ,  "yDur"  ,  "2",
            5  , "yDur"   , "NA"
  )

I wonder if this is what you are after

library(tidyr)
library(dplyr)
library(ggplot2)    

df <- txt %>% 
  mutate(variable=gsub("([[:lower:]])([[:upper:]][[:lower:]])", "\\1 \\2", variable)) %>% 
  separate(variable, into=c("xy", "what")) %>% 
  spread(what, value) %>% 
  readr::type_convert() 

df
#> # A tibble: 10 x 4
#>    Subject xy      Dur Exp  
#>  *   <dbl> <chr> <int> <chr>
#>  1       1 x         2 Yes  
#>  2       1 y        NA No   
#>  3       2 x        NA No   
#>  4       2 y         5 No   
#>  5       3 x         1 Yes  
#>  6       3 y         8 Yes  
#>  7       4 x        10 Yes  
#>  8       4 y         2 Yes  
#>  9       5 x        NA No   
#> 10       5 y        NA No   

So the graph would look like this

ggplot(df)+
  geom_col(aes(x=xy, y=Dur, fill=Exp))

enter image description here

dmi3kno
  • 2,943
  • 17
  • 31
  • Yes this is exactly what I meant! A problem is that not all the variable names are 1 letter (xExp, yExp) some may be AbExp or such. Is there a way to use sep to parse based on case? Thank you! – Grace May 02 '18 at 15:04
  • I corrected code to help insert space before capital letter and then separate on it. – dmi3kno May 02 '18 at 17:09