ggplot2 use different palettes for different layers - r

I am currently trying to put a boxplot below a jitter, and want to color the boxes in a lighter shade.
So in opposite to this post https://stackoverflow.com/a/9236205/1842673 I just could add a '+' instead of the '-' in the line
cols_dk <- rgb2hsv(col2rgb(colour)) - c(0, 0, 0.2)
However, since as mentioned in the last answer on that page "proto" was discontinued, I can not seem to get this to work. Is there another (new) way to reach that point?
I also tried to just add
+scale_color_hue(l=80)
Directly after the layer definition, however, this then applies to the whole plot (also to the jitter layer.
In this request https://github.com/hadley/ggplot2/issues/723 a more general solution has been asked for, so if there is someone here, who could help with that, it would be a nice thing as well...
Any suggestions?

Related

Plot Order of plots and all drawn elements like line.new or label.new

It was my understanding that the plot order was the same as the occurrence in the code. So if I plotted a line on code line 23 it would be plotted before (and appear behind) a label that was defined from code line 98.
In a workaround to my last question, I am using a line set to 30 px and a transparency of 75 to create a fill. The problem is it is plotting over the top of labels that are defined much later in the code and because of their location should be plotting on top of the line, but they are not.
I have searched everywhere, pine manual vs 4, Kodafy, and here on Stack to find exactly what the output order of drawing, labels, line, and plots are.
If anyone knows the solution to my specific problem or where I can find the information resources to resolve it myself I would be very grateful.
Last Post referred to above: https://stackoverflow.com/questions/69236171/i-want-to-fill-between-two-extended-line-new-pine-script-is-not-having-it-√/69241461#69241461
Thanks, Michael
try adding this to you study() line:
explicit_plot_zorder = true
from the refman:
"explicit_plot_zorder (const bool) Specifies the order in which the indicator's plots, fills, and hlines are rendered. If true, the plots will be drawn based on the order in which they appear in the indicator's code, each newer plot being drawn above the previous ones. This only applies to plot*() functions, fill, and hline. Optional. The default is false."
Hope that can fix your problem
Cheers, and best of luck with your trading and coding

Extending ggplot functionality with ggproto

This solution addresses how to extend ggplot functionality using ggproto, specifically on a boxplot example. I wonder how this approach might be generalised.
For instance let's say I want to implement an algorithm to adjust the position of geom_point elements to reduce point overlapping (such as ggrepel does for labels). Grateful for a steer on how to approach this problem - i.e. how to access and edit the x/y values and point size parameters (for overlap identification). I'm assuming the solution is linked to ggproto, but if that's wrong I'd welcome advice.

rCharts - Manipulating Background Color and Suppressing Axes

These are probably separate questions but they both relate to controlling the look of an rCharts/Polycharts plots (to that end, my meta-question is: Where can I find a complete source for all the fields/methods for rCharts/Polycharts?)
My specific questions are:
1) How do I change the background color of the plot? Specifically, I need a color background instead of the default white.
2) How do I suppress the axes (and associated labels and ticks)?
Most of the SO posts with workarounds for the above are for nvd3.
The documentation found here:
https://media.readthedocs.org/pdf/rcharts/latest/rcharts.pdf
Was mostly just some basic examples, but nothing covering the above. I did find this thread on github from 2 years ago, noting that complete documentation was on the to-do list.
https://github.com/ramnathv/rCharts/issues/221
So maybe I'm just not finding it?
Thanks

Plot in R won't change color

This is probably going to sound like a really stupid question but my plot in R will not change color!
this is my line in R
plot(MasterModCfs$V3,MasterModCfs$V5,col="blue")
I have tried it with spaces and without, with different colors, reloading, everything I could think of. If it's important, MasterModCfs contains literally thousands of data values.
I did one of the examples just to check
cars <- c(1, 3, 6, 4, 9)
plot(cars)
plot(cars, col="blue")
and it's blue. So that works.
Why won't my plot change colors?
Googling the same problem, I came across this present page - but subsequently found the source of my own issue through trial and error so posting here in case it helps. My x axis values were set up as factors - when I re-converted them to a normal string using as.character(), I was able to re-apply my own colours finally.
Without knowing what your par() settings are and what the classes of your columns, it's impossible to say.
You might try adding 'pch=21, bg="red"' to your plot command and see if that changes anything. It might give you a hint.
I suggest using the aes command in ggplot2. You can try
ggplot(data = MasterModCfs) + geom_point (mapping =aes (x=V3, y=V5), color="blue")
That should work!
Using plot directly I am not sure how to do it. I try your simple example (with cars) but it also fails if you add another vector for the "y" axis (as you wish to do in the end). To be honest, I have no idea why it works using only "cars" and the color.

How to avoid overplotting (for points) using base-graph?

I am in my way of finishing the graphs for a paper and decided (after a discussion on stats.stackoverflow), in order to transmit as much information as possible, to create the following graph that present both in the foreground the means and in the background the raw data:
However, one problem remains and that is overplotting. For example, the marked point looks like it reflects one data point, but in fact 5 data points exists with the same value at that place.
Therefore, I would like to know if there is a way to deal with overplotting in base graph using points as the function.
It would be ideal if e.g., the respective points get darker, or thicker or,...
Manually doing it is not an option (too many graphs and points like this). Furthermore, ggplot2 is also not what I want to learn to deal with this single problem (one reason is that I tend to like dual-axes what is not supprted in ggplot2).
Update: I wrote a function which automatically creates the above graphs and avoids overplotting by adding vertical or horizontal jitter (or both): check it out!
This function is now available as raw.means.plot and raw.means.plot2 in the plotrix package (on CRAN).
Standard approach is to add some noise to the data before plotting. R has a function jitter() which does exactly that. You could use it to add the necessary noise to the coordinates in your plot. eg:
X <- rep(1:10,10)
Z <- as.factor(sample(letters[1:10],100,replace=T))
plot(jitter(as.numeric(Z),factor=0.2),X,xaxt="n")
axis(1,at=1:10,labels=levels(Z))
Besides jittering, another good approach is alpha blending which you can obtain (on the graphics devices supporing it) as the fourth color parameter. I provided an example for 'overplotting' of two histograms in this SO question.
One additional idea for the general problem of showing the number of points is using a rug plot (rug function), this places small tick marks along the margin that can show how many points contribute (still use jittering or alpha blending for ties). This allows the actual points to show their true rather than jittered values, but the rug can then indicate which parts of the plot have more values.
For the example plot direct jittering or alpha blending is probably best, but in some other cases the rug plot can be useful.
You may also use sunflowerplot, while it would be hard to implement it here. I would use alpha-blending, as Dirk suggested.

Resources