How can add space in this plot ? I am a beginner in R - r

Here is the code for plotting 10 different graphs
lab=unique(train_train$PdDistrict)
lab=as.character(lab)
par(mfrow=c(5,2))
for(i in 1:length(lab))
{
a=plot(table(train_train[train_train$PdDistrict==lab[i],1]),las=3,main=lab[i])
}
The resultant graph is shown in this image link
How can i space those graphs so that its readable ?

My new code that I modified
lab=unique(train_train$PdDistrict)
lab=as.character(lab)
par(mfrow=c(1,2),mar=c(9,4,1,0))
for(i in 1:length(lab))
{
a=plot(table(train_train[train_train$PdDistrict==lab[i],1]),las=2,main=lab[i])
}
Now it looks much better.

Related

sunburst.R total frequency count is incorrect

I am plotting a sunburst donut and I cannot figure out why the total is incorrect.
library(sunburstR)
reports <- data.frame(
sequence = c("SVP-VP-Dir-end","SVP-VP-Dir-end","SVP-VP-Dir-end","SVP-VP-Dir-end","SVP-No VP-Dir-end","SVP-No VP-Dir-end","SVP-No VP-Dir-end"),
freq = as.numeric(c("167","60","51","32","5","1","1")))
sunburst(reports, count = TRUE)
It is supposed to be 100% 317 of 317 . Anyone know how to fix this? There is not much documentation on this great package.
Also, I would like it to have a default value in the center of donut.
If there is another way to create an interactive donut using R, please let me know.
Thanks you in advance.
It looks like the default function generating the message in the center of the donut rounds the total value to the nearest ten.
But you can customize this function using the explanation argument of sunburst. Oddly, the customized function (in javascript) must be provided as a string.
Try the following function:
custom.message = "function (d) {
root = d;
while (root.parent) {
root = root.parent
}
p = (100*d.value/root.value).toPrecision(3);
msg = p+' %<br/>'+d.value+' of '+root.value;
return msg;
}"
Now:
sunburst(reports, explanation = custom.message )
will generate the donut displaying exact total values. The count argument is no longer needed, as it is used by the default explanation function.
The value returned by custom.message is html code. As you can see, I've just inserted a line break (<br/>). You can modify the msg return value to further customize the look and feel.

Small issue with R plot - lines() stop before end value

I am in the process of making a nice graph but I encountered an issue with the extremities of the lines I drew. I do not know why R does not plot the lines all the way until maxxx (10 here). You can see on the graph from the code below what I am talking about (I cannot put picture as my reputation is too low), the extremities of the lines stop before 10:
maxxx<-10
plot(0,type="n",axes=FALSE,xlim=c(0,10),ylim=c(0,10),ylab="",xlab="")
mtext("TROLOLOL",side=3,cex=3)
axis(1,pos=0,at=c(0,10),labels=FALSE)
mtext("R1 ",side=1,line=0,cex=3)
axis(2,pos=0,at=c(0,10),labels=FALSE)
mtext("R2",side=2,line=0,cex=3)
Given_growth_rates<-c(0,0.3,0.5,1,2,5);
K1<-7
g1<-5.5
m<-0.2
R1_isoclines<-numeric(length(Given_growth_rates))
for (i in 1:length(Given_growth_rates)){
R1_isoclines[i]<-((Given_growth_rates[i]+m)*K1)/(g1-Given_growth_rates[i]-m)
}
R1_isoclines
K2<-10
g2<-7
R2_isoclines<-numeric(length(Given_growth_rates))
for (i in 1:length(Given_growth_rates)){
R2_isoclines[i]<-((Given_growth_rates[i]+m)*K2)/(g2-Given_growth_rates[i]-m)
}
R2_isoclines
for (i in 1:length(R1_isoclines)){
lines(rep(R1_isoclines[i],times=length(R2_isoclines[i]:maxxx)), R2_isoclines[i]:maxxx, col=i+1, type="l")
lines(R1_isoclines[i]:maxxx,rep(R2_isoclines[i],times=length(R1_isoclines[i]:maxxx)),col=i+1,type="l")
}
Try to replace the last four lines of your code by:
for (i in 1:length(R1_isoclines)){
lines(rep(R1_isoclines[i],times=2), c(R2_isoclines[i],maxxx), col=i+1, type="l")
lines(c(R1_isoclines[i], maxxx), rep(R2_isoclines[i],times=2),col=i+1,type="l")
}
Maybe that does what you expect.

Google charts tools Stacked chart

I'm new to Google Charts tools and I was wondering what I'am doing wrong. I want to make the BarChart (or ColumnChart) stacked by adding 'isStacked':true but then the chart gives me wrong data.
You can try it yourself on the Google playground with this code
(Just add 'isStacked':true to the options to see the wrong results)
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable({"cols":[{"id":"","label":"Date","type":"string"},
{"id":"","label":"Complaints","type":"number"},
{"id":"","label":"Compliments","type":"number"},
{"id":"","label":"Questions","type":"number"},
{"id":"","label":"Suggestions","type":"number"}],
"rows":[{"c":[{"v":"12\/2011"},{"v":30},{"v":0},{"v":0},{"v":0}]},
{"c":[{"v":"1\/2012"},{"v":93},{"v":"5"},{"v":0},{"v":0}]},
{"c":[{"v":"2\/2012"},{"v":82},{"v":"5"},{"v":0},{"v":0}]},
{"c":[{"v":"3\/2012"},{"v":72},{"v":"10"},{"v":0},{"v":0}]},
{"c":[{"v":"4\/2012"},{"v":68},{"v":"8"},{"v":0},{"v":0}]},
{"c":[{"v":"5\/2012"},{"v":59},{"v":"7"},{"v":0},{"v":0}]},
{"c":[{"v":"6\/2012"},{"v":30},{"v":"3"},{"v":"3"},{"v":0}]},
{"c":[{"v":"7\/2012"},{"v":37},{"v":"3"},{"v":"4"},{"v":"3"}]},
{"c":[{"v":"8\/2012"},{"v":31},{"v":"2"},{"v":"5"},{"v":0}]},
{"c":[{"v":"9\/2012"},{"v":47},{"v":"2"},{"v":"1"},{"v":"1"}]},
{"c":[{"v":"10\/2012"},{"v":67},{"v":0},{"v":"5"},{"v":"1"}]},
{"c":[{"v":"11\/2012"},{"v":38},{"v":"1"},{"v":"4"},{"v":0}]},
{"c":[{"v":"12\/2012"},{"v":14},{"v":"1"},{"v":"1"},{"v":"1"}]}
]});
// Create and draw the visualization.
new google.visualization.BarChart(document.getElementById('visualization')).
draw(data,
{title:"Yearly Coffee Consumption by Country",
width:1000, height:400,
vAxis: {title: "Year"},
hAxis: {title: "Cups"}}
);
}
I hope somebody can help me...
Thanks!
I just had the very same problem. The solution is, numeric values should NOT be between quotation marks, otherwise "3"+"4" becomes 34 instead of 7.
Just remove the " marks if there's a numeric value there.
cheers, greg
UPDATE: if you use 'f:' values as well, you will need the quotation marks again, otherwise mouseover might not work on the chart.
{"c":[{"v":"12\/2012"},{"v":14, "f":"14"},{"v":1, "f":"1"},{"v":1, "f":"1"},{"v":1, "f":"1"}]}

keeping X-axis length fixed in Qwt Plot

I'm making an app for realtime data plotting from serial port using Qt & Qwt.I'm plotting data on QwtPlot object using QwtPlotCurve's setData(QVector<double>&,QVector<double> method.Since I'm plotting large amounts of data,the x axis keeps on shrinking making the graph ugly after certain time.So I'm resetting the QVector<double> after plotting 500 points.It got better but I't still keeps shrinking till 500 points.I don't wan't that. I need the x-axis to be static.Any solution??
Here's part of code for reference.
d = new QVector<double>();
t = new QVector<double>();
curve = new QwtPlotCurve("My Plot");
curve->setPen(QPen(Qt::red));
curve->attach(ui->qwtPlot);
Plotting:
void MainWindow::plot(double val)
{
if(d->size() < 500)
{
d->push_back(val);
t->push_back(d->size());
}
else
{
d->clear();
t->clear();
d->push_back(val);
t->push_back(d->size());
}
curve->setData(*t,*d);
ui->qwtPlot->replot();
}
The plot() method gets triggered every time new data is available at the serial port.
Disable autoscaling, see QwtPlot::setAxisScale()

pasting Jpeg output in directory in R

I have function which performs scatter plot and I want to paste the results(Jpeg images) in D:/output but instead it is pasting in D:/.I want my results to be pasted on D:/output.
Please do help me.
setwd("D:/output")
IDs <- colnames(raw.expression)
for (i in 1:(dim(raw.expression)[2]-1))
{ for( j in i:(dim(raw.expression)[2]) )
{ if (i != j)
{ jpeg(file=paste("/",IDs[i],"gegen",IDs[j],".jpg",sep=""))
correlation <- round(cor(raw.expression[,i],raw.expression[,j]),2)
maximum <- max(log2(raw.expression[,i]))
minimum <- min(log2(raw.expression[,i]))
plot(log2(raw.expression[,i]),log2(raw.expression[,j])
,xlab=IDs[i],ylab=IDs[j],p‌​ch='.'
,text (maximum-2,minimum+0.5
,labels=paste("R = ",correlation,sep=""),pos=4,offset=0))
dev.off()
}
}
}
In the line
jpeg(file=paste("/",IDs[i],"gegen",IDs[j],".jpg",sep=""))
you prepend the filename with a "/" which would indicate that this is an absolute path, starting at the top of the file structure. I'm guessing on windows, this would be the top of the current drive letter, so it is going into D: rather than the current working directory D:/output.

Resources