0

I have the below data:

library(rjson)
library(ggplot2)


l='[{"a": "abc", "date": "20190506","model": "honda", "features":"weather", "value": 10},
{"a": "abc", "date": "20190506","model": "honda", "features":"bad", "value": 14},
{"a": "abc", "date": "20190506","model": "honda", "features":"failure", "value": 20},
{"a": "abc", "date": "20190506","model": "honda", "features":"not", "value": 1},
{"a": "abc", "date": "20190506","model": "honda", "features":"search", "value": 24},
{"a": "abc", "date": "20190506","model": "honda", "features":"esrs", "value": 2},
{"a": "abc", "date": "20190506","model": "honda", "features":"issue", "value": 1},
{"a": "abc", "date": "20190506","model": "honda", "features":"errors", "value": 30},

{"a": "abc", "date": "20190510","model": "ford", "features":"ice", "value": 12},
{"a": "xyz", "date": "20190509", "model": "honda", "features":"summer", "value":18},
{"a": "xyz", "date": "20190507", "model": "ford", "features":"hot", "value":14},

{"a": "abc", "date": "20190506","model": "ford", "features":"search", "value": 20},
{"a": "abc", "date": "20190510","model": "honda", "features":"400", "value": 18},
{"a": "xyz", "date": "20190509", "model": "ford", "features":"fail", "value":24},
{"a": "xyz", "date": "20190507", "model": "honda", "features":"200", "value":15}]'

And when I use this data in the form of dataframe to plot bar graph between features and value using below code:

l = fromJSON(l)
df = data.frame(do.call(rbind, l))
ggplot(df, aes(y=features, x=value))

I get the below error:

Error: Discrete value supplied to continuous scale

What I am doing here wrong?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
user15051990
  • 1,835
  • 2
  • 28
  • 42
  • 1
    I did not run your code, but I suspect that all of your variables are factors. If this is true, you should convert the appropriate ones to numeric prior to calling `ggplot`. You can check with `sapply(dat, class)` where dat is the name of your data.frame. – lmo Jun 29 '19 at 16:11
  • It shows “list” for every variable. – user15051990 Jun 29 '19 at 16:22
  • I am new to R. Can you help in changing their class? – user15051990 Jun 29 '19 at 16:22
  • It looks like the `fromJSON` function has a simplify argument. Set this to TRUE. Then, if you are lucky, the returned object will be a list of character vectors. Then your code will result in the factor situation I mentioned above. If yes, use `dat$numVar <- as.numeric(dat$numVar)` replacing dat and numVar with appropriate names to convert each factor variable to a numeric. – lmo Jun 29 '19 at 16:34

1 Answers1

2

I had luck using jsonlite's fromJSON:

l = jsonlite::fromJSON(l) 
ggplot(l,aes(y=features, x=value)) + 
  geom_point()

enter image description here

Edit:

Here's a bar graph. Note that there are two "search" values, stacked here by default.

ggplot(l,aes(x=features, y=value)) + 
  geom_col(color = "white") + 
  coord_flip()

enter image description here

Or if you want them sorted, I like forcats::fct_reorder; but note that it's sorting by individual values, not total values; wasn't sure how you wanted to treat the two in "search":

ggplot(l,aes(x=forcats::fct_reorder(features, value), y=value)) + 
  geom_col(color= "white") + coord_flip()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53