If I have the following figure, where the rect has a colormapper fill_color, is there some way to find out what exactly the colors are?
p = figure(toolbar_location=None,
x_range = hm_data['Models'],
y_range=[metric],
plot_width=400, plot_height=75)]
p.rect(x = 'Models' , y = 'Metrics',
width=1, height=1,
fill_color={'field': 'Values', 'transform': mapper},
line_color= None,
source = source)
I assumed I could call something like p.rect.fill_color, but I haven't been able to find anything. I want to be able to use the list of colors mapped to the values on a different chart with the same data.
In general if you want to investigate objects in bokeh you can use .properties_with_values()
Do you mean the palette of colors that the color mapper will use when it color maps? That's available as the .palette attribute in the mapper:
mapper.palette # these colors will be used to colormap
Or, do you mean what colors each individual rect will be assigned to? If so, that information is not available in Python. The colormapping happens in the brower, when the plot is displayed. Python never knows them.
If you need to know the colors of individual glyphs in Python, you will have to set them yourself in Python (e.g. by making a column in your CDS containing all the colors you want).
Related
I have trouble for plotting my raster and some points as sf object over that. I use aes(color=...) to assign color to different types of my points. But I want to change the colours to specific ones like red and blue and yellow. also I can't use scale_fill_manual because my raster layer's colours which is in first layer, shouldn't be changed. Is there any solution for this problem?
thanks in advance
geom_raster(data=dem2,aes(x=x,y=y,fill=elevation))+
geom_sf(data=mypoint,aes(shape=Type,colour=ranges))
The fill parameter inside aes wich you can manipulate manually using scale_fill_manual works perfectly with polygons or raster data. But, when you need to color points
or simple lines, the color parameter inside aes is prefered. Said that, if you want to assing different colors to your points, use scale_color_manual, this shouldnt interfer in your raster color.
package "ggnewscale" will work in this problem. It allows to use different scale for color in one plot.
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 am graphing a network and trying to color the vertices using non-overlapping attributes. I want my network diagram to be colored according to different attributes. In this example, if the first three letters of ID 2 are equal to U 50 or U 51, I want this to show up as red. I have 5 attributes I want this graph coded by and any observations that don't fall into one of the categories should be coded in a default color. In this way I will be able to see the intensity of these attributes and better communicate this to other people. So far, I have been unable to get the code to work using a variety of different coding methods. First I tried to create a new variable that assigned the correct attribute to each observation before converting it into an i graph object.
anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U50" | substr(anon.nd$ID2,1,3)=="U51"]<-"O"
anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U54" | substr(anon.nd$ID2,1,3)=="U55"]<-"P"
anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
anon.nd$vertexcolor[anon.nd$Country=="L12"]<-"UK"
anon.nd$vertexcolor[anon.nd$ID2=="U769"]<-"OBL"`
I then specified the colors I wanted to assign to each each attribute. I used the get vertex attribute code and filled in the appropriate colors.
anon.nd1<-graph.data.frame(anon.nd)
vertex_colors=get.vertex.attribute(anon.nd1,"vertexcolor")
colors=c('azure3', 'firebrick1', 'orange1', 'darkblue', 'darkolivegreen', 'gold')
vertex_colors[vertex_colors==0]=colors[1]
vertex_colors[vertex_colors==1]=colors[2]
vertex_colors[vertex_colors==2]=colors[3]
vertex_colors[vertex_colors==3]=colors[4]
vertex_colors[vertex_colors==4]=colors[5]
vertex_colors[vertex_colors==5]=colors[6]
I tried this same method using just:
vertex_colors<-vertex_colors+1
Then to plot, I changed my edge color to black, specified my layout, and change the size of my edges and vertices.
E(anon.nd1)$color="black"
nd.layout<-layout.fruchterman.reingold(anon.nd1)
plot(anon.nd1, layout=nd.layout, vertex.color=vertex_colors, vertex.size=2, edge.arrow.size=.01, vertex.label=NA)
Using this method, no color shows up on the vertices, not even the default color. Using a different method where I set the vertex attribute, I do a little better. The default color shows up, but the colors I want do not.
anon.nd2<-graph.data.frame(anon.nd)
V(anon.nd2)$colors<-"azure3"
V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U50" | substr(anon.nd2$ID2,1,3)=="U51"]<-"firebrick1"
V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U54" | substr(anon.nd2$ID2,1,3)=="U55"]<-"orange1"
V(anon.nd2)$colors[anon.nd2$Country=="L12"]<-"darkblue"
V(anon.nd2)$colors[anon.nd2$INT.type=="K1"]<-"darkolivegreen"
V(anon.nd2)$colors[anon.nd2$ID2=="U769"]<-"gold"
E(anon.nd2)$color<-"black"
nd.layout<-layout.fruchterman.reingold(anon.nd2)
windows(width=20, height=16)
plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")
I think the problem might be that I am trying to code vertex color using multiple (non-overlapping) edge attributes. But I don't know how to convert and edge attribute into a vertex attribute. I also don't know if there is some other, unidentified problem with my code.
Here is the link to my data is copied below as well as a link to my full code file which has one or two other methods I tried using to solve this problem. Any help would be much appreciated!
Data
And here is an R file with my code, which is also above: R-file
I think you are messing up your vertex_color vector, have a look at it with head().
anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
vertex_colors[vertex_colors==0]=colors[1]
You first assign a string and then compare with numbers, so non of them should be true.
plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")
This contains a typo and returns an error for me since "vertex_colors" isn't a colour name.
Last but not least, does
plot(anon.nd2, vertex.color=colors)
or
plot(anon.nd2, vertex.color=1:8)
result in a colourful plots? If yes, the vertex_colors vector is your problem, if not something else is.
I need to compare two maps of the same quantities, I would like to keep the colour palette constant in the two graphs, for easing comprehension, but it looks like I don't get how to do so.
Should I set limits (e.g. the minimum between all the plots assigned to low and the highest level to high?)
Is there an easy way to do so?
I am new to this, so sorry if the solution is banal, I went through a lot of blog posts but looks like I am not finding anything.
My code:
fin<-get_map("Helsinki",zoom=12)
ggmap(fin, legend="bottom")+
geom_polygon(data=a,aes(x=a$long,y=a$lat, id=id, fill=Test_statistics), alpha=0.1, colour="white")
To give you an idea, this is an image
and this is another
it is not clear at all!
Images still need a bit of "prettyfying" it is just to give an idea
Basically what I would like is in this question, but for discrete (factor) values
I can't reproduce your plots because you've not given us the data, but setting limits in a scale_colour_gradient should work. See:
http://docs.ggplot2.org/0.9.3.1/scale_gradient.html
under "Tweak scale limits" (second example) where Hadley says:
Setting the limits manually is also useful when producing
multiple plots that need to be comparable
For example (and I'm using points here for simplicity - you probably have to use scale_fill_gradient to set the fill colour for polygons - I don't have the time to build some polygons):
> set.seed(310366); d=data.frame(x=runif(20),y=runif(20),
z1=rnorm(20), z2=rnorm(20)+5)
note that z1 has a range of about -1 to 1, and z2 has a range of 4 to 7. This helps us see the effect.
> ggplot(d,aes(x=x,y=y,col=z1))+geom_point(size=8) +
scale_colour_gradient(limit=range(c(d$z1,d$z2))
> ggplot(d,aes(x=x,y=y,col=z2))+geom_point(size=8) +
scale_colour_gradient(limit=range(c(d$z1,d$z2)))
produces two plots with the same limits on the palette legend, but the first one has very dark points because the values are all low (-1 to 1) and the second one has mostly light colours because the values are all high (4 to 7).
Both sets of points have been coloured using the same mapping of value to colour because of the limit argument in the scale_colour_gradient function. You are mapping the fill attribute so I think you need scale_fill_gradient.
I didnt get your problem exctly, but try adding this to all your plots. Then the colour code should be uniform.
+scale_colour_brewer(pallette="Set1")
You can add any of the pallette's shown here with examples
http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#color-charts
Theming for ggplot2 makes it quite easy to relegate the need for multiple or repetitive + opt()... lines. However, I would like to know if there is a way to define defaults for geoms and scale colors. Instead of having to write ...+ scale_fill_manual() for each plot, I'd like to be able to set it and forget it. Similarly, I'd like to be able to set geom options so I don't have to retype (or forget to retype) things like geom_text(...,size=3,color="white")
Update:
For scales, it seems at some point that there was a method:
set_default_scale("colour", "discrete", "grey")
But this function doesn't seem to exist in the most recent version of ggplot2.
There is another method for this now. You can essentially overwrite any aesthetics scale, for example:
scale_colour_discrete <- function(...) scale_colour_brewer(..., palette="Set2")
scale_fill_discrete <- function(...) scale_fill_brewer(... , palette="Set2")
Now, your aesthetics will be coloured or filled following that behaviour.'
As per: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/w0Tl0T_U9dI
With respect to defaults to geoms, you can use update_geom_defaults, for example:
update_geom_defaults("line", list(size = 2))
I can't think of anything useful for the geoms, but for the scales, one option would be to use the fact that components of ggplots are all simply R objects that can be saved, stored and reassigned like any other.
So you could perhaps create your own collection of "default" versions of many scales, like:
sfmDefault <- scale_fill_manual(...)
scmDefault <- scale_colour_manual(...)
etc. with your desired default values. Put them in your .RProfile or wherever and use them as needed.
Changing the default palletes, can also be achieved by setting options, eg.:
options(ggplot2.continuous.colour="viridis")
options(ggplot2.continuous.fill="viridis")
If you have defined a custom scale, say scale_color_custom, you would need to change the options as follows:
options(ggplot2.continuous.colour=scale_color_custom)
Notice that you are feeding the options a raw function and not a string. The string "viridis" is a reserved special input, but using a function is more generic.