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

有帮助吗?

解决方案

'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...)

其他提示

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()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top