Change colors to the 'fill' in aes? - r

I have this:
colr=c(a='black',b='red',c='brown')
Basically, i have used the fill with the categories from a column in the df in the aes. Thus it will show through the categories the plot. The problem is when i try to put the colr vector in the fill to change colors as it says it encounters the problem
Error: Aesthetics must be either length 1 or the same as the data (5): fill
Obviously the incorrect way of typing it makes it think that the colors refer to the brandscolumn while it should refer to the g_classes in the fill.
ggplot(df,aes(brands,fill=g_classes))+geom_bar(stat='count',fill=colr)
So, how to pass the colors in the colr vector to the fill (g_classes) in aes?

You have an option to use the scale_fill_manual command. It has to be the same length as you have categories, however. In this case it seems that you are attempting to specify three colours for five categories, and this is most likely why your code fails.
It is hard to reproduce your problem given the limited examples you give, but try omitting the fill argument in the geom_bar command, and changing the value for the stat argument to "identity"
Change your colour vector to
colr=c("black","red","brown")
and add additional ggplot line
scale_fill_manual(values=colr)+

Related

how to attribute colors to factor levels in ggplot2

usually I use +scale_color/fill_continuous(values=c("blue","red"))
but here I need to do several plots where some levels does not change, so I wondered if there where a way to do something like :
+scale_color/fill_continuous(values=c("A":"blue","B":"red"))
The idea is to save to a level a specific color that will never change.
You might want to assign this before you plot the data.
E.g., make a column in your dataframe with the name of the color (or color value), and use dplyr::case_when to conditionally assign the color based on some condition.

r ggplot - conditionally set aes element to NULL

I am building a wrapper function for ggplot to enable standard charts to be created with a single line of code.
One part of this is that in some cases the user will wish to set 'group' and 'color' aes elements, however in other cases they will not.
Is there a simple way of only having a 'group' or 'colour' aes element if the user passes one to the function, and to leave it blank otherwise?
I have tried simply setting these to to NULL, however this does not work, returning the following error.
Error: Aesthetics must be either length 1 or the same as the data (4): x, y, group, colour
Instead of NULL, try setting the aesthetic to 1.

keep colour palette constant between plots

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

How do I use ggplot2 to manually assign hexadecimal colors to each data point in a plot or bar graph?

I have a data set, and one of the variables is a factored array with hexadecimal characters (e.g. '#00FF00'). One of the things I wanted to try doing is creating a bar plot with all of the different colors combined.
I tried using
cg<-ggplot(my.data,aes(x=factor(1),fill=as.character(my.color)))
followed by
cg+geom_bar()
but the only colors plotted seem to be ones from the default scale. I've tried omitting the as.character() part of the code, but it doesn't make a difference. I also have the same issue when making 2d plots with geom_point().
If I try something like
plot(my.data$var1,my.data$var2,col=as.character(my.color))
the colors are plotted the way I wanted them, although the graph doesn't look as nice as the ones in ggplot2.
Is there something obvious I'm missing, or is this beyond the scope of ggplot2?
You should add scale_fill_identity() to use color names as actual colors.
ggplot(my.data,aes(x=factor(1),fill=my.color)) +
geom_bar()+
scale_fill_identity()

ggplot legend key color and items

Using the following data frame:
sdf<-data.frame(hours=gl(n=3,k=1,length=9,labels=c(0,2,4)),
count=c(4500,1500,2600,4000,800,200,1500,50,20),
machine=gl(n=3,k=3,length=9,labels=c("A","B","C")))
The following graph can be produced using either of these scripts:
ggplot(data=sdf,aes(x=hours,y=count,group=machine,fill=machine))+
geom_area(data=sdf[sdf$machine=="A",])+
geom_area(data=sdf[sdf$machine=="B",])+
geom_area(data=sdf[sdf$machine=="C",])
ggplot(data=sdf,aes(x=hours,y=count,group=machine,fill=machine))+
geom_area(position="dodge")
However, when the fill color is changed, the item in the legend disappears.
ggplot(data=sdf,aes(x=hours,y=count,group=machine,fill=machine))+
geom_area(data=sdf[sdf$machine=="A",])+
geom_area(data=sdf[sdf$machine=="B",],fill="darkorchid")+
geom_area(data=sdf[sdf$machine=="C",])
Ideally, the legend should show the color change.
Question: What script can create items in a legend as well as offer color controls for those items?
You can adjust the values assigned to any aesthetic using scale_X_manual(values=(whatever)). Here you want scale_fill_manual.
ggplot(data=sdf,aes(x=hours,y=count,group=machine,fill=machine))+
geom_area(position="dodge") +
scale_fill_manual(values=c("red", "darkorchid", "green"))
Note that, as a rule, you want to let ggplot group the data for you, as you have done in your second ggplot call (This is what the group argument does). Supplying each 'slice' of data separately, as you have done in your first example, pretty much defeats the purpose of ggplot2, and should be avoided.

Resources