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.
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 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)+
I am working on a script for plotting volcano plots for a specific number of groups that the user specifies, i.e. if he wants to have five groups colored, the script modifies the data set accordingly with a new column called color. All the other observations that are there shouldn't be highlighted specifically, but be grey.
Is it possible to only specify the color of one group while letting ggplot2 choose all the colors for the other groups available?
I couldn't solve this using scale_color_manual, since it expects values for all groups!
This image demonstrates my problem, all other features except the groups, i.e. the "Features (all)" group should be grey instead of red, however I want ggplot to color all other groups automatically...
Specifying a custom color palette and sorting the color column solved my problem :)
I´m trying to create a bar chart, but I´m getting an error like:
Scale for 'fill' is already present. Adding another scale for 'fill',
which will replace the existing scale.
I identified the problem as trying to use two scale_fill functions. If I remove one of them (scale_fill_brewer or scale_fill_discrete), it works.
ggplot(df_postgres, aes(x=as.factor(language), y=total/1000, fill=type)) +
geom_bar(stat='identity', color = 'black')+
geom_text(mapping=aes(label=ifelse(type=='A', (paste(as.character(floor(total/1000)),'K') ), '')), vjust=-0.7,color='black')+
scale_x_discrete(name="Language")+
scale_y_continuous(name="Repos (thousands)")+
scale_fill_brewer(palette='Pastel1')+
scale_fill_discrete(name="Proportion",labels=c("Language","Others"))
My question is: how can I set the discrete options AND the nice palette option?
I´m a newbie in R, but can´t find a response to this specific error.
The ... argument to scale_fill_brewer will take arguments that will be passed to scale_fill_discrete.
So move your name and labels arguments to scale_fill_brewer, delete your scale_fill_discrete and everything should work.
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()