Search code examples
rdataframeggplot2rjson

Getting Discrete Value supplied to continuous scale while using ggplot


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?


Solution

  • 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