R legend colors don't match pie chart colors -


library(rmysql) library(plotrix) con <- dbconnect(mysql(),                  user = 'root',                  password = 'password',                  host = 'localhost',                  dbname='testtable1') rs = dbsendquery(con, "                   select  `answer`                    `ams`                    `questiontext` =  'year of manufacture'                    ") mydata = fetch(rs,n=-1)  attach(mydata) count <- table(mydata)  percentlabels<- round(100*count/sum(count), 1) pielabels<- paste(percentlabels, "%", sep="")   pie(count, main="pie chart") legend('topright', legend=sapply(percentlabels, as.expression), fill=factor(count)) }) 

i've created pie chart of several dates. in legend, i'd include percent values.

the problem i'm having colors in legend don't match colors in pie chart. also, i'd percent values preceded percent symbol (%) if possible. here's how graph looks far:

enter image description here

i see 2 errors in code:

1) in legend statement, have percentlabels should pielabels.

2) should set vector of colors , call both in pie statement , in legend.

cols<-c("red", "green", "blue") pie(count, main="pie chart", col=cols) legend('topright', legend=sapply(pielabels, as.expression), fill=cols) 

i'm on ipad can't check it, should it.

see http://www.theanalysisfactor.com/r-tutorial-part-14/ tutorial.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -