ggplot(df, aes(x, y, colour = as.factor(z))) +
geom_point() +
geom_line(data=df,aes(y=predict(lmer(mymodel),df),newdata=df)
x and z are my explanatory variable with z being a factor. I then fit a linear mixed-effect model to my data, df.
How do I change the colour of as.factor ggplot in R? It's giving me RBG but I would like different colours. Your advise is appreciated. Thanks.
I think you are looking for scale_colour_manual of scale_colour_brewer. In the first you can specify the colours manually and in the second you can choose one of the Colorbrewer palettes.
You can also define the colors as the post above suggests and reference the colors by name in the following table r colors for ggplot
scale_color_manual(name="Sex", values=c("dark grey", "black"), labels=c("Males", "Females"))
where the name on the legend will be Sex and the labels will be male and female. Also, in the aes(x=year, y = number, color=sex)
You can look for a good combination of colours in here http://colorbrewer2.org/
You will get the code for the corresponding colour you need (for instance "#2ca25f" as a kind of green or "#000000" as black) and you can add it in your code as following:
ggplot(df, aes(x, y, colour = as.factor(z))) +
geom_point() +
geom_line(data=df,aes(y=predict(lmer(mymodel),df),newdata=df) +
scale_color_manual(values = c("#000000", "#2ca25f", ...)
depending on the number of your z factors.
P.S. you can also write common colour names as "black", "white", etc. instead of the #code.
Related
I want to plot 3 regression lines, one for each value of tau, and each one with its colour, as specified in the dataset. The internet says you provide the color variable at the general plot aesthetics (http://ggplot.yhathq.com/docs/geom_line.html), but that does not seem to work. Any body would konw what to do?
Thanks in advance,
here is the dataframe
https://www.dropbox.com/s/fqmayg6asf5e3eu/tautest.csv?dl=0
here is the code
ddl=read.csv("tautest.csv")
ggplot(ddl, aes(x = predictor, y = value, color=col)) +
geom_line(aes(group = tau),size = 0.8)
ggplot2 gives some colors, but I want the colors in my dataframe, why would ggplot2 give other colors?
ggplot 'maps' aesthetics to scales, meaning that it tries to fit a variable onto a scale. The default scale for categorical variables is scale_colour_discrete(), which matches a colour to every unique entry. Since ggplot doesn't know that the values in your col column are colour names, you have to let ggplot know that these colours are to be interpreted literally. You can do that with scale_colour_identity().
ggplot(ddl, aes(x = predictor, y = value, color = col)) +
geom_line(aes(group = tau),size = 0.8) +
scale_colour_identity()
I am trying to have ggplot2 show one line of a histogram as a different color than the rest. In this I have been successful; however, ggplot is using the default colors when a different set are specified. I am sure there is an error in my code, but I am unable to determine where it is. The data and code are below:
create data
library(ggplot2)
set.seed(71185)
dist.x <- as.data.frame(round(runif(100000, min= 1.275, max= 1.725), digits=2))
colnames(dist.x) <- 'sim_con'
start histogram
ggplot(dist.x, aes(x = sim_con)) +
geom_histogram(colour = "black", aes(fill = ifelse(dist.x$sim_con==1.55, "darkgreen", "firebrick")), binwidth = .01) +
theme(legend.position="none")
Which results in the following image:
I do not want to use the default colors, but instead want to use 'darkgreen' and 'firebrick'. Where is the error in the code? Thanks for any help you can provide.
You're so close!
In your code above, ggplot is interpreting your fill as variables in your data set - factor darkgreen and factor firebrick - and doesn't have any way of knowing that those labels are colors, not, say, names of animal species.
If you add scale_fill_identity() to the end of your plot, as below, it will interpret those strings as colors (the identity), not as features of the data.
One benefit of this approach vs #marat's excellent answer above: if you have a complex plot (say, using geom_segment(), with a starting value and an ending value for each observation) and you want to apply two fill scales on your data (one scale for the start value and a different scale for the end value) you can do the conditional logic in the data processing step, then use scale_fill_identity() to color each observation accordingly.
ggplot(
data=dist.x,
aes(
x = sim_con,
fill = ifelse(dist.x$sim_con==1.55, "darkgreen", "firebrick")
)
) +
geom_histogram(
colour = "black",
binwidth = .01
) +
theme(legend.position="none") +
scale_fill_identity()
I don't think you can explicitly set colors in aes; you need to do it in scale_fill_manual, as in the example below:
ggplot(dist.x, aes(x = sim_con)) +
geom_histogram(colour = "black", binwidth = .01,aes(fill=(sim_con==1.55))) +
scale_fill_manual(values=c('TRUE'='darkgreen','FALSE'='firebrick')) +
theme(legend.position="none")
I am creating a very simple plot that groups data and uses the grouping variable to determine linestyle and colour. I then override those using 'scale_linetype_manaul' and 'scale_colour_manual'. So far so good, but when I try to modify legend labels or its title, the legend splits into two parts: one for linetype and one for colour. I just want one legend, but with the custom labels and title.
Following this question, I made sure to name both scale objects the same, but that doesn't appear to help.
Minimal example:
X <- data.frame(TPP=factor(c(1,5,10,1,5,10,1,5,10)),
value=c(-0.035819, 0.003356, 0.066091, -0.028039, 0.004333, 0.060292, -0.023115, 0.005661, 0.058821),
horizon=c(1,1,1,2,2,2,3,3,3))
ggplot(X, aes(x=horizon, y=value, group=TPP, col=TPP, linetype=TPP))+
geom_line(size=1)+
scale_linetype_manual(name="X", values = c("solid","dashed", "dotted")) +
scale_color_manual(name="X", values = c("black", "red", "blue"), labels=c("Low", "5","High"))
This yields the following figure with two legends. How can I recombine those legends again, with custom labels and a title?
This might help:
ggplot(X, aes(x=horizon, y=value, group=TPP, col=TPP, linetype=TPP))+geom_line(size=1)+
scale_linetype_manual(name="X", values = c("solid","dashed", "dotted"),labels=c("Low", "5","High")) +
scale_color_manual(name ="X", values = c("black", "red", "blue"),labels=c("Low", "5","High"))
If the labels defined in scale_color_manual and in scale_linetype_manual are different, or if they are specified in only one of them, you will obtain two different legends.
I am trying to have ggplot2 show one line of a histogram as a different color than the rest. In this I have been successful; however, ggplot is using the default colors when a different set are specified. I am sure there is an error in my code, but I am unable to determine where it is. The data and code are below:
create data
library(ggplot2)
set.seed(71185)
dist.x <- as.data.frame(round(runif(100000, min= 1.275, max= 1.725), digits=2))
colnames(dist.x) <- 'sim_con'
start histogram
ggplot(dist.x, aes(x = sim_con)) +
geom_histogram(colour = "black", aes(fill = ifelse(dist.x$sim_con==1.55, "darkgreen", "firebrick")), binwidth = .01) +
theme(legend.position="none")
Which results in the following image:
I do not want to use the default colors, but instead want to use 'darkgreen' and 'firebrick'. Where is the error in the code? Thanks for any help you can provide.
You're so close!
In your code above, ggplot is interpreting your fill as variables in your data set - factor darkgreen and factor firebrick - and doesn't have any way of knowing that those labels are colors, not, say, names of animal species.
If you add scale_fill_identity() to the end of your plot, as below, it will interpret those strings as colors (the identity), not as features of the data.
One benefit of this approach vs #marat's excellent answer above: if you have a complex plot (say, using geom_segment(), with a starting value and an ending value for each observation) and you want to apply two fill scales on your data (one scale for the start value and a different scale for the end value) you can do the conditional logic in the data processing step, then use scale_fill_identity() to color each observation accordingly.
ggplot(
data=dist.x,
aes(
x = sim_con,
fill = ifelse(dist.x$sim_con==1.55, "darkgreen", "firebrick")
)
) +
geom_histogram(
colour = "black",
binwidth = .01
) +
theme(legend.position="none") +
scale_fill_identity()
I don't think you can explicitly set colors in aes; you need to do it in scale_fill_manual, as in the example below:
ggplot(dist.x, aes(x = sim_con)) +
geom_histogram(colour = "black", binwidth = .01,aes(fill=(sim_con==1.55))) +
scale_fill_manual(values=c('TRUE'='darkgreen','FALSE'='firebrick')) +
theme(legend.position="none")
I've got a line graph where the linetype is set by one variable (SampleType, in this example) and the color is set by another (Sample). For these data, I'd like it if the legend combined both of those variables into one legend entry rather than having one entry for the color and one for the linetype. Here are my example data:
EIC data
Here is the code and the plot I've come up with so far:
EIC <- read.csv("EIC data.csv")
ggplot(EIC, aes(x = Time, y = Counts, color = Sample, linetype = SampleType)) +
geom_line()
What I'd really like is for the legend to just show "Sample" and then the appropriate colors AND linetypes for each of those samples, so it would be solid for the standards and dashed for the clinical samples and each sample would be a different color. I love ggplot2, so I'd prefer to continue to use ggplot2 to do this. I've tried adding scale_linetype_manual like this to the code:
scale_linetype_manual(values = c(rep("solid", 3), rep("dashed", 2)))
but that's not changing the legend or the graph. I've also tried making a new column with "solid" or "dashed" in each row depending on whether the sample is a clinical sample or a standard and then using scale_linetype_identity(), but while that does work for the graph, it's not changing the legend since I'm still mapping color to one variable and linetype to a second.
I'm using R version 3.0.2 and ggplot2_1.0.0.
Thanks in advance for any ideas!
Use variable Sample for both - color= and linetype= and then with scale_linetype_manual() get the desired linetypes.
ggplot(EIC, aes(x = Time, y = Counts, color = Sample, linetype = Sample)) +
geom_line()+scale_linetype_manual(values = c(rep("solid", 3), rep("dashed", 2)))