1

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’

  • 1
    You could improve your chances of finding help here by adding a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Adding a MRE and an example of the desired output (in code form, not tables and pictures) makes it much easier for others to find and test an answer to your question. That way you can help others to help you! P.S. Here is [a good overview on how to ask a good question](https://stackoverflow.com/help/how-to-ask) – dario Oct 14 '21 at 08:15

2 Answers2

2

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

12666727b9
  • 1,133
  • 1
  • 8
  • 22
1

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

bird
  • 2,938
  • 1
  • 6
  • 27