I make a plot like this:
plot(
layer(x=sort(randn(1000),1), y=sort(randn(1000),1), Geom.point),
layer(x=[-4,4], y=[-4,4], Geom.line(), Theme(default_color=color("black"))))
As you can see, the white circle around the points makes the high density parts of the plot almost white.
I would like to change the outer circle color of the points to black (or blue) to better show that the points are really there.
From the Gadfly documentation it seems like the highlight_color argument of Theme() might do that, but it takes a function as argument.
I don't understand how that is supposed to work. Any ideas?
The argument name turns out to be discrete_highlight_color...
It should be a function that modifies the colour used for the plot,
typically by making it lighter (a "tint") or darker (a "shade").
In our case, we can just ignore the current colour and return black.
using Color
using Gadfly
plot(
layer(
x = sort(randn(1000),1),
y = sort(randn(1000),1),
Geom.point,
# Theme(highlight_width=0.0mm) # To remove the border
Theme( discrete_highlight_color = u -> LCHab(0,0,0) )
),
layer(
x = [-4,4],
y = [-4,4],
Geom.line(),
Theme(default_color=color("black"))
)
)
To find the correct argument, I first typed
code_lowered( Theme, () )
which gives the list of arguments,
and then
less( Gadfly.default_discrete_highlight_color )
which shows how the default value is defined.
For those like me trying to solve this problem more recently, I discovered that the best way to get rid of that pesky white ring is through the theme setting highlight_width=0pt
for example
plot(x=rand(10),y=rand(10),Theme(highlight_width=0pt))
I had some additional themes in the below image
Related
I would like to change the default color scheme in ggplot2. That is, I would like to define a color scheme (say: viridis) at the one point in the script so that all subsequent ggplot diagrams will use this color scheme without having to call + scale_color_viridis() each time.
I've seen this SO post featuring update_geom_defaults(geom, new), but I could not find a way to explain this function to use a scheme such as viridis.
I have also tried to update the ggplot color, similar to this post, but, as #baptise pointed out, this approach does not really work.
In short:
define new color scheme, eg., viridis
call ggplot subsequently without adding + scale_color_viridis() but still this ggplot diagram uses the viridis color scheme.
It looks like
options(ggplot2.continuous.colour="viridis")
will do what you want (i.e. ggplot will look for a colour scale called
scale_colour_whatever
where whatever is the argument passed to ggplot2.continuous.colour—viridis in the above example).
library(ggplot2)
opts <- options(ggplot2.continuous.colour="viridis")
dd <- data.frame(x=1:20,y=1:20,z=1:20)
ggplot(dd,aes(x,y,colour=z))+geom_point(size=5)
options(oldopts) ## reset previous option settings
For discrete scales, the answer to this question (redefine the scale_colour_discrete function with your chosen defaults) seems to work well:
scale_colour_discrete <- function(...) {
scale_colour_brewer(..., palette="Set1")
}
I'm building a static map using the sf package and I want my map to have both a specific color and transparency. I can manage to get both independently but not together.
library(sf)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)
I can make the map all red simply:
plot(nc["SID74"], col="red")
produces:
If I want to add transperency, I have to change the col argument and pass it a sf.colors call:
plot(nc["SID74"], col=sf.colors(n=1, alpha=0.3))
produces:
I have the transparency but it's the default blue and I can't find the option to change it to red in ?sf.colors. I want to set the color and also have the transparency.
Any idea how?
If you have a particular color you want to use, there's no need to use the sf.colors function. That chooses a color palette for you. The sf.colors function is not the only way to set colors for your plots, it just exposes the default colors that sf uses if you want use them elsewhere.
If you want to adjust the transparency of an exiting color, the scales package has a helper function to do that.
plot(nc["SID74"], col=scales::alpha("red", .3))
You can see that scales::alpha("red", .3) returns
[1] "#FF00004D"
so it's basically a rgb-hex value with an extra byte for transparency. You can also use the rgb() function to construct similar objects.
How do one plot the absolute value of the $|\Gamma(z)|$ with Maple?
As in the WIKI figure:
http://de.wikipedia.org/wiki/Gammafunktion#mediaviewer/File:Gamma_abs_3D.png
plot3d(
abs(GAMMA(x+I*y)), x= -4.6..5.1, y= -5.1..5.1,
view= [DEFAULT$2, 0..6],
labels= [Re(z), Im(z), ``], title = abs(GAMMA(z))
);
The command plots:-complexplot3d is intended as a convenient way of obtaining such plots (using z instead of the plot3d command with x+I*y, and putting in the labels automatically).
P := plots:-complexplot3d( abs(GAMMA(z)), z=-4-4*I..4+4*I,
view=[-4..4,-4..4,0..6], orientation=[-120,75] ):
For some reason, the surface stored in structure P gets some blue color that overrides the shading scheme.
P;
If we remove that COLOR substructure then the underlying shading scheme (which we could change in the original call, using the shading option) is revealed.
subsindets(P,specfunc(anything,COLOR),u->NULL);
I will submit a bug report about that heavy-handed blue coloring.
I met a problem when shading some areas by using function polygon in R. if these areas have common parts, the last shaded one will cover the previous one. But now, I do want do see this phenomenon and want to keep the original boundaries. what should I do?
see the following image as an example:
the codes for drawing this image are:
plot(variance_five[51:60,2],type="n",ylim=c(0,0.1))
for(i in 1:6)
{
polygon(c(1:10,10:1),c(variance_five[(10*i-9):(10*i),1],variance_five[(10*i): (10*i-9),3]),lty=2,col=i)
lines(variance_five[(10*i-9):(10*i),2])
}
we can see that the last shaded area(purple) covers some blue and light blue parts, what shall I do keep all the boundaries of all areas and the colour in the common areas are the sum effect of these colours? Thank you!
You want to use transparency. The function alpha in the scales package will add transparency to any color. I'd recommend setting up a vector of colors before the loop and making them transparent, e.g.
my_colors <- rainbow(6)
require(scales)
my_colors <- alpha(my_colors, alpha = 0.5)
And then inside the loop replacing col = i with col = my_colors[i].
You can, of course, adjust the alpha level (0 is completely transparent, 1 completely opaque) as necessary. The scales package also provides a function show_col which I find useful in picking colors. Try
show_col(rainbow(6))
show_col(heat.colors(6))
show_col(cm.colors(6))
I should note that rainbow et al. take alpha as an argument, so you don't need the alpha function, but it's more general to use the alpha function which will work on other R colors if you've got favorites. (I like dodgerblue2 and firebrick4, personally.)
I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps