labels for boxplot and barplot loop in r [duplicate] - r

I am trying to plot 16 boxplots, using a for loop. My problem is, that the 2nd title is plotted on the first plot, the 3rd title on the second plot and so forth.
Does anyone have a guess on, what I am doing wrong?
My code is the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"],title(main = "Sweden"))
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"],title(main = "Norway"))
boxplot(data$distance[data$countryname=="Denmark"]~data$alliance[data$countryname=="Denmark"],title(main = "Denmark"))
boxplot(data$distance[data$countryname=="Finland"]~data$alliance[data$countryname=="Finland"],title(main = "Finland"))
boxplot(data$distance[data$countryname=="Iceland"]~data$alliance[data$countryname=="Iceland"],title(main = "Iceland"))
boxplot(data$distance[data$countryname=="Belgium"]~data$alliance[data$countryname=="Belgium"],title(main = "Belgium"))
boxplot(data$distance[data$countryname=="Netherlands"]~data$alliance[data$countryname=="Netherlands"],title(main = "Netherlands"))
boxplot(data$distance[data$countryname=="Luxembourg"]~data$alliance[data$countryname=="Luxembourg"],title(main = "Luxembourg"))
boxplot(data$distance[data$countryname=="France"]~data$alliance[data$countryname=="France"],title(main = "France"))
boxplot(data$distance[data$countryname=="Italy"]~data$alliance[data$countryname=="Italy"],title(main = "Italy"))
boxplot(data$distance[data$countryname=="Spain"]~data$alliance[data$countryname=="Spain"],title(main = "Spain"))
boxplot(data$distance[data$countryname=="Portugal"]~data$alliance[data$countryname=="Portugal"],title(main = "Portugal"))
boxplot(data$distance[data$countryname=="Germany"]~data$alliance[data$countryname=="Germany"],title(main = "Germany"))
boxplot(data$distance[data$countryname=="Austria"]~data$alliance[data$countryname=="Austria"],title(main = "Austria"))
boxplot(data$distance[data$countryname=="Ireland"]~data$alliance[data$countryname=="Ireland"],title(main = "Ireland"))
boxplot(data$distance[data$countryname=="UK"]~data$alliance[data$countryname=="UK"],title(main = "UK"))

I think this could replace all your lines and fix your problem:
for (i in data$countryname)
boxplot(distance~alliance, subset(data, countryname==i), main=i)
But that's hard to verify without a reproducible example or some of your data.frame.

Based on the documentation, you should be assigning a title to your boxplots by making explicit calls to the function title(), rather than as a parameter in the call to boxplot(). The first two calls to generate your boxplots should look something like the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"])
title(main = "Sweden")
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"])
title(main = "Norway")

Related

Title is missing in 1st boxplot when using a for loop

I am trying to plot 16 boxplots, using a for loop. My problem is, that the 2nd title is plotted on the first plot, the 3rd title on the second plot and so forth.
Does anyone have a guess on, what I am doing wrong?
My code is the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"],title(main = "Sweden"))
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"],title(main = "Norway"))
boxplot(data$distance[data$countryname=="Denmark"]~data$alliance[data$countryname=="Denmark"],title(main = "Denmark"))
boxplot(data$distance[data$countryname=="Finland"]~data$alliance[data$countryname=="Finland"],title(main = "Finland"))
boxplot(data$distance[data$countryname=="Iceland"]~data$alliance[data$countryname=="Iceland"],title(main = "Iceland"))
boxplot(data$distance[data$countryname=="Belgium"]~data$alliance[data$countryname=="Belgium"],title(main = "Belgium"))
boxplot(data$distance[data$countryname=="Netherlands"]~data$alliance[data$countryname=="Netherlands"],title(main = "Netherlands"))
boxplot(data$distance[data$countryname=="Luxembourg"]~data$alliance[data$countryname=="Luxembourg"],title(main = "Luxembourg"))
boxplot(data$distance[data$countryname=="France"]~data$alliance[data$countryname=="France"],title(main = "France"))
boxplot(data$distance[data$countryname=="Italy"]~data$alliance[data$countryname=="Italy"],title(main = "Italy"))
boxplot(data$distance[data$countryname=="Spain"]~data$alliance[data$countryname=="Spain"],title(main = "Spain"))
boxplot(data$distance[data$countryname=="Portugal"]~data$alliance[data$countryname=="Portugal"],title(main = "Portugal"))
boxplot(data$distance[data$countryname=="Germany"]~data$alliance[data$countryname=="Germany"],title(main = "Germany"))
boxplot(data$distance[data$countryname=="Austria"]~data$alliance[data$countryname=="Austria"],title(main = "Austria"))
boxplot(data$distance[data$countryname=="Ireland"]~data$alliance[data$countryname=="Ireland"],title(main = "Ireland"))
boxplot(data$distance[data$countryname=="UK"]~data$alliance[data$countryname=="UK"],title(main = "UK"))
I think this could replace all your lines and fix your problem:
for (i in data$countryname)
boxplot(distance~alliance, subset(data, countryname==i), main=i)
But that's hard to verify without a reproducible example or some of your data.frame.
Based on the documentation, you should be assigning a title to your boxplots by making explicit calls to the function title(), rather than as a parameter in the call to boxplot(). The first two calls to generate your boxplots should look something like the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"])
title(main = "Sweden")
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"])
title(main = "Norway")

Colors in Rcharts

I am trying to generate bar plots / columns using rCharts(v 0.4.2). My problem is that I have an year's worth of data and I need to group on Months. So in Total I have 12 bars that I need to display. However, I have only 9 unique colors after which the colors start repeating. I read this documentation and tried inserting
colors <- c('#7cb5ec','#434348', '#90ed7d', '#f7a35c','#8085e9','#f15c80', '#e4d354','#2b908f','#f45b5b','#91e8e1')
into my code and then calling it as follows :
c <- hPlot(x = 'Confi', y = 'n', data = tablefinalC, type = 'bar', group = 'Month',title = "Inccode By confi",
subtitle = "Bar Graph")
c$plotOptions(series = list(stacking = "normal",colors=paste0('colors'))
c$chart(backgroundColor = NULL)
c$set(dom = 'chart5')
However, I still get the same repetitive colors. So can someone please confirm how I can increase the amount of colors? Thanks in advance
You can create empty chart and then add series
Example
library(rCharts)
df=data.frame(x=1:10,y=-10:-1,z=letters[1:10],stringsAsFactors = F)
colors1=c( '#7cb5ec','#434348', '#90ed7d')
df$col=rep(colors1,round(nrow(df)/length(colors1),0)+1)[1:nrow(df)]
# Create new chart
a <- rCharts:::Highcharts$new()
# Set options
a$chart(type = "bar")
for(i in unique(df$z)){
a$series(name=i,stacking = "normal" ,color=df$col[df$z==i], data= rCharts::toJSONArray2(df[df$z==i,], json=F, names=T))
}
a#plot
Result
Update( re-read question)
if you want to add more colors custominze colors1 and df$col
df=data.frame(x=1:20,y=-20:-1,z=letters[1:20],stringsAsFactors = F)
colors1=c( '#0048BA','#B0BF1A','#7CB9E8','#C9FFE5','#B284BE',
'#5D8AA8','#00308F','#72A0C1','#AF002A','#F0F8FF',
'#84DE02','#E32636','#C46210','#EFDECD','#E52B50',
'#AB274F','#F19CBB','#AB274F','#D3212D','#3B7A57',
'#FFBF00','#FF7E00','#FF033E','#9966CC','#A4C639',
'#F2F3F4','#CD9575','#665D1E','#915C83','#841B2D'
)
df$col=colors1[1:nrow(df)]
Give you

r - taking difference of two xyplots?

I have several xyplot objects that I have saved as .RDATA files. I am now interested in being able to look at their differences. I have tried things like
plot1-plot2
but this does not work (I get the "non-numeric argument to binary operator error).
I would also be able to do this if I knew how to extract the timeseries data stored within the lattice xyplot object, but I have looked everywhere and can't figure out how to do this either.
Any suggestions?
EDIT:
just to make it perfectly clear what I mean for MrFlick, by "taking the difference of two plots" I mean plotting the elementwise difference of the timeseries from each plot, assuming it exists (i.e. assuming that the plots have the same domain). Graphically,
I might want to take the following two plots, stored as xyplot objects:
and end up with something that looks like this:
-Paul
Here is a little function I wrote to plot the difference of two xyplots:
getDifferencePlot = function(plot1,plot2){
data1 = plot1$panel.args
data2 = plot2$panel.args
len1 = length(data1)
len2 = length(data2)
if (len1!=len2)
stop("plots do not have the same number of panels -- cannot take difference")
if (len1>1){
plotData = data.table(matrix(0,0,4))
setNames(plotData,c("x","y1","y2","segment"))
for (i in 1:len1){
thing1 = data.table(cbind(data1[[i]]$x,data1[[i]]$y))
thing2 = data.table(cbind(data2[[i]]$x,data2[[i]]$y))
finalThing = merge(thing1, thing2,by = "V1")
segment = rep(i,nrow(finalThing))
finalThing = cbind(finalThing,segment)
setNames(finalThing,c("x","y1","y2","segment"))
plotData = rbind(plotData,finalThing)
}
}
if (len1==1){
plotData = data.table(matrix(0,0,3))
setNames(plotData,c("x","y1","y2"))
thing1 = data.table(cbind(data1[[i]]$x,data1[[i]]$y))
thing2 = data.table(cbind(data2[[i]]$x,data2[[i]]$y))
plotData = merge(thing1, thing2,by = "V1")
}
plotData$difference = plotData$y1-plotData$y2
if (len1==1)
diffPlot = xyplot(difference~x,plotData,type = "l",auto.key = T)
if (len1>1)
diffPlot = xyplot(difference~x|segment,plotData,type = "l",auto.key = T)
return(diffPlot)
}

Calculate equation from .csv file input and plot result over barplot

I coulnd't found any post with a related subject. I actually don't know if its posible.
So I have my. csv file:
Periodo;Teorico;Real;F1;F2;F3
20140101;50;20;7;7;16
20140108;55;29;11;5;5
20140115;52;21,4;8,6;10;12
20140122;66;32;9;8;17
I asign it to a data.frame:
df<-read.csv2('d:\\xxx\\test2.csv', header = T, sep = ";")
Then I do barplot function:
bp <- barplot(t(df[,-c(1:2)]),col=c("blue", "red", "green", "yellow"),legend=colnames(df[,-c(1:2)]),args.legend = list(x="topleft"))
axis(side = 1, at = bp, labels = df$Periodo)
title(main = "Teorico = Real + F1+F2+F3", font.main = 4)
Now I must calculate the following function: (efficiency function)
((Teorico-Real)/Teorico)*100
And represent the result of the function of each row on the top of each Periodo (week).
If you could help me with the code for the function and "replotting" parts or give some guidelines or posts related to this I would be really gratefull.
Thanks
You can try:
lbls <- round(((df$Teorico - df$Real) / df$Teorico)* 100)
mtext(lbls, at=bp)
(I just used round to make it look better.)

Issues with formatting header in R prior to using plot() function

I have a data set that I've successfully read into R. It's a simple data.frame with ONE ROW of data (I'm not sure how many columns, but its in the hundreds). It was read with column headers, but no row labels. So the data set looks something like this:
df=structure(list(X500000 = 0.0958904109589041, X1500000 = 0.10958904109589, X2500000 = 0.10958904109589, X3500000 = 0.164383561643836, X4500000 = 0.136986301369863, X5500000 = 0.205479452054795, X6500000 = 0.136986301369863, X7500000 = 0.0273972602739726, X8500000 = 0.0821917808219178, X9500000 = 0.178082191780822), .Names = c("X500000", "X1500000", "X2500000", "X3500000", "X4500000", "X5500000", "X6500000", "X7500000", "X8500000", "X9500000"), class = "data.frame", row.names = 79L)
Except that it is MUCH LARGER (I don't know if it matters, but it has around 300 columns going across). I'm trying to plot it so that the X##### labels are on the x axis, and the value of each data point is plotted on the y axis (say like a scatter plot on excel or even a line graph). Doing just plot(df) gives me an extremely bizarre graph that makes no sense to me (a bunch of boxes each with a dot right in the centre and no labels?).
I have a feeling it might work if I were to transform the data frame into a vector by removing the headings and then adding x-axis labels individually afterwards and doing a plot() on the vector, but if there is a way of avoiding that it would be great....
As explained in '?plot', 'x' and 'y' must be two vectors of numerics, of same size:
df=structure(list(X500000 = 0.0958904109589041, X1500000 = 0.10958904109589, X2500000 = 0.10958904109589, X3500000 = 0.164383561643836, X4500000 = 0.136986301369863, X5500000 = 0.205479452054795, X6500000 = 0.136986301369863, X7500000 = 0.0273972602739726, X8500000 = 0.0821917808219178, X9500000 = 0.178082191780822), .Names = c("X500000", "X1500000", "X2500000", "X3500000", "X4500000", "X5500000", "X6500000", "X7500000", "X8500000", "X9500000"), class = "data.frame", row.names = 79L)
plot(x=as.numeric(substr(names(df),2,nchar(names(df)))), as.numeric(df), xlab="This is xlab", ylab="This is y")

Resources