Graphing multiple variables in R - r

I am currently attempting to graph multiple columns in a matrix in R. So far, I have figured things out, but here is my problem- when I submit a matrix with 5 columns, I only get a graph with 4 lines. I've noticed that the missing line is always the line closest to the x-axis. I've been working on this for several hours now, and I have tried several different things. Any advice or help on how to get R to produce that 5th line (with a corresponding color filling the space between the x-axis and the line) would be greatly appreciated.
gender=cbind(matrix(malepop),matrix(femalepop))
plotmat(year,gender)
#a sample set
biggen=cbind(malepop,femalepop,malepop,femalepop)
#start of the function
plotmat2=function(years,m,colors){
n=m/1000000
#create a plot with the base line
plot(years,n[,1],type='l',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):2) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=clrs[i])
next
cord.xmat=c(min(years),years,max(years))
cord.ymat1=c(-1,n[,1]/1000000,-1)
polygon(cord.xmat,cord.ymat,col="purple")
}
}
#sample color set
clrs=c("red","blue","yellow","pink","purple", "cyan", "hotpink")
#run the function
plotmat2(year,biggen,clrs)
Thanks for any and all help you can provide!

It might be that you are unintentionally covering up your first line with the other colored sections, and that you may be skipping the creation of the polygon for n[,1].
From the way you tried to graph the columns in descending order, I am assuming you know that your columns are in ascending size order (the section that is pink in your example plot would be the final column in the matrix "biggen"). In case I am wrong about this, it might be a good idea to change your polygon shading using the density argument, which may help you see if you are covering up other sections by accident.
## plotmat2 function
plotmat2=function(years,m,colors){
n=m/1000000
#create a blank plot based on the baseline
plot(years,n[,1],type='n',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):1) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=colors[i], density=10)
}
}
P.S. If this doesn't help fix the problem, it might help if you provided a portion of your dataset. I am still learning about R and about StackOverflow, but that seems to be sensible advice that is given on a lot of the threads I have read on here. Good luck!

Related

geom_bspline across multiple plots combined into a single figure

I would like to create a ggplot2 layer that includes multiple geom_bspline(), or something similar, to point to regions on different plots after combining them into a single figure. A feature in the data seen in one plot appears in another plot after a transformation. However, it may not be clear to a non-expert they are due to the same phenomenon. The plots are to be combined into a single figure using ggarrange(), cowplot(), patchwork() or something similar.
I can get by using ggforce::geom_ellipse() on each plot but it's not as clean. Any suggestions?
Of course, after asking the question and staring at the figure in question, it came to me that I simply need to add a geom_bspline() to the combined figure. Tried that earlier but didn't give enough thought to the coordinates on the new layer. The coordinates of the spline are given in the range of 0 to 1 for both the x and y values on this new layer. Simple and obvious.

Change colors in r plot

I am currently trying to plot some data and don't manage to obtain a nice result. I have a set of 51 individuals with each a specific value (Pn) and split within 14 groups. The closest thing I end up with is this kind of plot. I obtain it thanks to the simple code bellow, starting by ordering my values for the Individuals :
Individuals <- factor(Individuals,levels=Individuals[order(Pn)])
dotchart(Pn,label=Individuals,color=Groups)
The issue is that I only have 9 colors on this plot (so I lost information somehow) and I can't manage to find a way to apply manually one color per group.
I've also try to use the ggplot2 package by reading it could give nice looking things. In that case I can't manage to order properly the Individuals (the previous sorting doesn't seem to have any effect here), plus I end up with only different type of blue for the group representation which is not an efficient way to represent the information given by my data set. The plot I get is accessible here and I used the following code:
ggplot(data=gps)+geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))
I apologize if this question seems redundant but I couldn't figure a solution on my own, even following some answer given to others...
Thank you in advance!
EDIT: Using the RColorBrewer as suggested bellow sorted out the issue with the colors when I use the ggplot2 package.
I believe you are looking for the scale_color_manual() function within ggplot2. You didn't provide a reproducible example, but try something along the lines of this:
ggplot(data=gps, mapping=aes(x=Individuals, y=Pn, color=Groups))+
geom_point() +
scale_color_manual(values = c('GROUP1' = 'color_value_1',
'GROUP2' = 'color_value_2',
'GROUP3' = 'color_value_3'))
Replace GROUPX with the values inside your Group column, and replace color_value_x with whatever colors you want to use.
A good resource for further learning about ggplot2 is chapter 3 of R For Data Science, which you can read here: http://r4ds.had.co.nz/data-visualisation.html
I can't be sure without looking at your data, but it looks like Groups may be a numeric value. Try this:
gps$Groups <- as.factor(gps$Groups)
library(RColorBrewer)
ggplot(data=gps)+
geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))+
scale_colour_brewer(palette = "Set1")

R - Adding series to multiple plots

I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')

R confusion heatmap

I have created my first confusion heatmap using the code I found here.
As I result I got a very nice plot with an "increasing" diagonal showing that the predicted and actual data are closely related.
Now, when I look up other confusion matrices, all of them show a "decreasing" diagonal and I'm wondering whether I should adapt my plot in that way (and if so: how?).
Any ideas on that?
By default, R displays a heatmap with the row names ordered from bottom to top, rather than top to bottom.
Here's how to change the ordering

Multiple Filled Contour plots on the

I think my query is partially answered by this:
Filled contour lines from a plot with color in R
I have added a response to that post, but being quite old, I thought I might re-post it as a new question. If you can help me out, that would be great!
I have a matrix (G) that I need to separate into 3 zones on a plot, divided by two contours (cutoff1 and cutoff2).
depth = seq(mindepth,maxdepth,part)
width = seq(minwidth,maxwidth,part)
G = matrix(0,length(width),length(depth))
# G filled in with values at each coordinate from depth/width.
filled.contour2(width,depth,G,levels=seq(0,cutoff1),col=colors()[374])
filled.contour2(width,depth,G,levels=seq(cutoff1,cutoff2),col=colors()[411])
filled.contour2(width,depth,G,levels=seq(cutoff2,max(G)),col=colors()[50])
This doesn't work because consecutive filled.contour2 operations overwrite the previous ones. I can't post images because I'm new to posting on this site, but the three separated zones are simple and the separating contours do not overlap.
It looks like something along the lines of:
plot(width,depth,G,cont=c(cutoff1), display="filled.contour2", col=colors()[411])
should work, but obviously this isn't the right way to call plot.
Any help would be hugely appreciated. Cheers

Resources