Multiple Filled Contour plots on the - r

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

Related

Scatterplot looks strange

my scatterplot that shows the relationship between my principal component and one of my questionnaire items looks strange.
The scatterplot between two of my principal components looks great (see second image).
The item is measured on a Likert scale from 1 to 5 (1 = strongly disagree, 5 = strongly agree). The histogram of this item also has many gaps between the bars, which is strange to me too.
Please let me know which specific output you still need to figure this out.
Thanks
That is because the principal components are both metric and the Likert scale is ordinal, so the values can only be integers from 1-5. In this regard the Scatterplot does not look strange but it might be the wrong type of plot. If you anyway want to use a scatterplot you can use jitter(var, 0.3) on the ordinal variable when creating the plot. 0.3 is just a suggestion, it's best if you just try what works. As you didn't provide a reproducible example I wasn't able to try it out.
For reproducibility, it is best if you provide a piece of the database you are using to make these scatterplots. It would also be great if you include the axes titles inside the graph.
To be honest, I dont see anything wrong with the first figure. Variable y (the questionare item) is a discrete variable between 1 and 5. It make sense that one result from the PC1 is match with more than one of the values of the item questionare.

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.

Graphing multiple variables in 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!

In Stata, how do I modify axes of dot chart?

I'm trying to create a dot chart in Stata, splitting it into two categories
Running a chunk of code:
sysuse nlsw88, clear
drop if race == 3
graph dot (mean) wage, over(occ) by(race)
Creates such output:
So far so good but I'd like to remove labels of Y axis from the right graph to give the data some more space.
The only way I've been able to do that was to manually edit graph and hide the axis label object:
Is there a way to do it programmatically? I do know I could use one more over() but in some graphs of mine that is already taken.
I believe the solution is buried in help bystyle and help by_option. However, I can't get it to work with your example (I'm on Stata 12). But the description is clear. For example:
A bystyle determines the overall look of the combined graphs,
including
whether the individual graphs have their own axes and labels or if instead the axes and labels are shared across graphs arrayed in the
same row and/or in the same column;
...
There are options that let you control each of the above attributes --
see [G-3] by_option --
And also
iyaxes and ixaxes (and noiyaxes and noixaxes) specify whether the y axes and x axes are
to be displayed with each graph. The default
with most styles and
schemes is to place y axes on the leftmost graph of each row and to place x axes on
the bottommost graph of each column. The y and
x axes include the
default ticks and labels but exclude the axes titles.
If for some reason that doesn't work out, something like
sysuse nlsw88, clear
drop if race == 3
graph dot (mean) wage, over(occ) by(race)
gr_edit .plotregion1.grpaxis[2].draw_view.setstyle, style(no)
does (but I don't really like the approach). You can mess with at least the axis number [#] to do a bit of customization. I guess recording changes in the graphical editor and then recycling the corresponding code, may be one way out of difficult situations.

Labeling plot maximum peaks in GNUPlot

I have some data that I'm plotting with GNUPlot. I have three different data sets for different energies. What I need to do is label the maximas on the plot. For example, I need something like (20, 4.5) for the red plot. The values do not need to be above the maximas, as they only need to be distinguishable to which is what. Is there any easy way to do this in GNUPlot? I haven't been able to find anything online.
Thanks in advanced. Below is an example plot that I'm trying to work with. It wouldn't let me post images so I'm posting the link below.
http://i.imgur.com/xA3q52I.png
I think this example can help
http://www.gnuplot.info/demo/stats.html

Resources