Max Mustermann
Max Mustermann

Reputation: 77

Plot a dataset in r

I'm trying to plot the frequency of different hair colors in women with the HairEyeColor dataset. Example: enter image description here

library(plotly)
freq <- HairEyeColor[,,2]
plot_ly(x = freq, type = 'bar')

I got the error-message: more elements in the method signature (2) than in the generic signature (1) for function ‘asJSON’

Upvotes: 1

Views: 231

Answers (2)

12666727b9
12666727b9

Reputation: 1127

You could try this way

library(plotly)
freq <- HairEyeColor[,,2]
freq <- as.data.frame(freq)
attach(freq)
plot_ly(freq, x = ~ Hair, y = ~ Freq, type = 'bar')

I got the following graph. Don't knwo whether that was the one you were looking for

enter image description here

Upvotes: 2

bird
bird

Reputation: 3294

You could do:

library(plotly)
library(tidyverse)

HairEyeColor[,,2] %>% 
        as_tibble() %>% 
        group_by(Hair) %>% 
        summarise(freq = sum(n)) %>% 
        plot_ly(x = ~Hair, y = ~freq, type = "bar")

enter image description here

Upvotes: 1

Related Questions