R, ggvis graphing from two data frames that both need to be grouped by -
i'm creating stacked bar graph multiple horizontal lines running through it. done in shiny app. user picks option , depending on is, there either 2 or 3 horizontal lines.
here minimal reproducible example:
df1 <- data.frame(a=as.factor(rep(1:10,2)), b=sample(1:5,20, replace=t), c=c(rep("apple",10), rep("banana",10)) ) df1 <- df1[order(df1$a, df1$c),] df2 <- data.frame(a=as.factor(rep(1:10,2)), i=c(rep(3,10),rep(4,10)), j=c(rep("red",10), rep("green",10)) ) > df1 b c 1 1 5 apple 11 1 2 banana 2 2 3 apple 12 2 3 banana 3 3 1 apple 13 3 2 banana 4 4 3 apple 14 4 1 banana 5 5 4 apple 15 5 3 banana 6 6 4 apple 16 6 2 banana 7 7 3 apple 17 7 4 banana 8 8 5 apple 18 8 1 banana 9 9 5 apple 19 9 2 banana 10 10 1 apple 20 10 3 banana > df2 j 1 1 3 red 2 2 3 red 3 3 3 red 4 4 3 red 5 5 3 red 6 6 3 red 7 7 3 red 8 8 3 red 9 9 3 red 10 10 3 red 11 11 3 red 12 1 4 green 13 2 4 green 14 3 4 green 15 4 4 green 16 5 4 green 17 6 4 green 18 7 4 green 19 8 4 green 20 9 4 green 21 10 4 green 22 11 4 green ggvis(data=df1, x=~a, y=~b) %>% group_by(c) %>% layer_bars(fill=~c) %>% layer_paths(data=df2, x=~a, y=~i, strokewidth:=2)
which gives me following graph (it'll different each time because of sample() ).
but don't want inverse z in middle. want 2 parallel lines grouped df2$j. i'm not sure how go 2 data frames in ggvis.
the reason have df2 in long form because user choose option create more 2 horizontal lines. don't want use if , else control that. in actual code, df1 , df2 both reactives.
thank in advance.
you can give layer_paths
dataset grouped on y variable horizontal lines drawn separately each group.
to this, can use data = group_by(df2, i)
instead of data = df2
.
and code , plot like:
ggvis(data=df1, x=~a, y=~b) %>% group_by(c) %>% layer_bars(fill=~c) %>% layer_paths(data = group_by(df2, i), x = ~a, y = ~i, strokewidth:=2)
Comments
Post a Comment