0

I'm using the following code to create a pie chart with ggplot2, which contains two pie charts next to one another: one for each value of "MotT". Each pie chart need to how the proportions for each "Model". Here is my code:

library(ggplot2)
library(sqldf)


df <- data.frame("MorT" = c(1,2,1,2), "Model" = c(1,1,2,2),
             "Values" = c(length(outOfTime1withIns[,1]), 
                          length(outOfMem1withIns[,1]),
                          length(outOfTime1noIns[,1]),
                          length(outOfMem1noIns[,1])))



df=sqldf("select Values,
     CASE WHEN MorT==1 THEN 'Insuficient Time'
          WHEN MorT==2 THEN 'Insuficient Memory'
     END MorT,
     CASE WHEN Model==1 THEN '1) FSM1 with Insertion Dominance' 
          WHEN Model==2 THEN '2) FSM1 without Insertion Dominance' 
     END Model from df")

p = ggplot(data=df, 
       aes(x=factor(1),
           y=Summary,
           fill = factor(Model)
       )
)

I get the following error after I try to run "df=sqldf("select..."

Error in sqliteExecStatement(con, statement, bind.data) : 
RS-DBI driver: (error in statement: near "Values": syntax error)

And of course p is empty. I get

Error: No layers in plot

If I try to call it.

Any help will be very much appreciated!Thanks

Tania
  • 285
  • 1
  • 4
  • 13

2 Answers2

3

'Values' is a keyword in SQL so you can't use it as a variable name. Change it to 'value' or something else in the data frame, that should sort the SQL error.

It looks like you're following the example on http://www.r-chart.com/2010/07/pie-charts-in-ggplot2.html.

Firstly, you have y = Summary in your ggplot, that needs to be updated to 'value' for your code.

Next, there seemed to be an issue with the data you're using (I don't have outOfMem1noIns so i made test data), but you should make sure the values for each MorT sum up to 1.

Then the code as it is on the tutorial page should work (maybe with some warning messages...)

Ger
  • 754
  • 1
  • 9
  • 33
1

The SQL statement has a syntax error, as the error states. In addition, the ggplot2 error comes from the fact that you have not added a geometry, e.g. geom_point:

p = ggplot(data=df, 
       aes(x=factor(1),
           y=Summary,
           fill = factor(Model)
       )
) + geom_point()
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thanks for the answer, I realise there is an error in the syntax, however I don't know what it is. Adding + geom_point() results in error: Don't know how to automatically pick scale for object of type groupGenericFunction. Defaulting to continuous Error in data.frame(x = 1L, y = function (x, ..., na.rm = FALSE) : arguments imply differing number of rows: 1, 0, 4 – Tania Aug 15 '13 at 11:15
  • Next time, it is good idea to create a test dataset which reproduces your problem. – Paul Hiemstra Aug 15 '13 at 14:25