ToTheClassiest
ToTheClassiest

Reputation: 29

R - How to Plot Multiple Density Plots With ggvis

How would one go about plotting multiple density plots on the same set of axes? I understand how to plot multiple line graphs and scatter plots together, however the matter of having the density plots share a common x-axis is tripping me up. My data is currently set up as such:

name  x1   x2   x3
a     123  123  123
b     123  123  123
c     123  123  123

Thanks for the help!

EDIT: Here are some details I was missing which may help make my question clearer.

I have a data frame attr_gains which looks like the example above, and whose variable names are Str, Agi, and Int. So far, I have been able to get a density plot of the Str variable alone with this code:

  attr_gains %>%
  ggvis(x=~Str)%>%
  layer_densities(fill :="red", stroke := "red")

What I would like to do is overlay two more density plots, one for Agi and Int each, so that I have three density plots on the same set of axes.

Upvotes: 2

Views: 1706

Answers (1)

lnNoam
lnNoam

Reputation: 1055

Directly from the documentation:

PlantGrowth %>% 
  ggvis(~weight, fill = ~group) %>% 
  group_by(group) %>%
  layer_densities()

Link

Your Case:

set.seed(1000)
library('ggvis')
library('reshape2')

#############################################

df = data.frame(matrix(nrow = 3, ncol = 5))
colnames(df) <- c('names', 'x1', 'x2', 'x3', 'colors')

df['names'] <- c('a','b','c')    
df['x1'] <- runif(3, 100.0, 150.0)
df['x2'] <- runif(3, 100.0, 150.0)
df['x3'] <- runif(3, 100.0, 150.0)
df['colors'] <- c("blue","orange","green")

df <- melt(df)

#############################################

df %>% 
  ggvis( ~value, fill = ~colors ) %>% 
  group_by(names) %>%
  layer_densities()

Please see this SE page for information on controlling ggvis color(s).


Looks like this:

Density Plot

Upvotes: 3

Related Questions