My goal is to pass separate values to change the colors used for fill aesthetics in different geoms.
For example:
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(fill = Species), color = 'black', shape = 21) +
scale_fill_manual(values = c('royal blue', 'red2', 'limegreen'))
In this plot, I would like to be able to use separate colors to fill the bars and points. Is this possible? I'm aware of using scale_fill_manual()
to set the colors to whatever values I want, but this will change the fills of both the bars and the points to the same colors.
Here is a semi-working example of what I am trying to do, however, the legend is off...
iris_j <- iris %>%
mutate(Species_bar = factor(paste0(Species, '_bar')))
color.groups <- c('royal blue', 'red2', 'limegreen', NA, 'royal blue', 'white')
names(color.groups) <- c(levels(iris_j$Species), levels(iris_j$Species_bar))
ggplot(iris_j, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species_bar), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(fill = Species), color = 'black', shape = 21) +
scale_fill_manual(values = color.groups)
This is one of the limitations of ggplot—an aesthetic can only be mapped to a single variable. Generally speaking, I find it a reasonable limitation, as it forestalls a lot of confusing and hard-to-read graphs. That said, with some creativity, it can be worked around, e.g. by coloring the points with the color aesthetic, and then overplotting to add a stroke:
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(color = Species)) + # add colored points
geom_point(color = 'black', shape = 21, show.legend = TRUE) + # add point strokes (including in legend)
scale_color_manual(values = c('royal blue', 'red2', 'limegreen')) + # define point colors
scale_fill_manual(values = c(NA, 'royal blue', 'white')) # define bar colors
To separate the legends, specify a different name for each. To add a stroke to the points in the legend, you'll need to effectively rebuild it in guide_legend. (According to the docs, supplying a named vector to show.legend should work, but in practice it fails.)
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(color = Species)) +
geom_point(color = 'black', shape = 21) +
scale_color_manual('points', values = c('royal blue', 'red2', 'limegreen'),
guide = guide_legend(override.aes = list(shape = 21, color = 'black',
fill = c('royal blue', 'red2', 'limegreen')))) +
scale_fill_manual('bars', values = c(NA, 'royal blue', 'white'))
Such an approach will not generalize to a plot where color is already being used otherwise.
Here are a few little things to try that you could build off of.
First up, if you don't need to use a filled shape, you can just map color to the species in geom_point, so you have a color scale and a fill scale. In this case, I changed the label for fill to mark it as being the means, to show how you can split these into two legends.
library(tidyverse)
light_colors <- c("#87CEEB", "#FFB6C1", "#FF8C69")
dark_colors <- c("#22A0D6", "#E33650", "#BF411B")
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), geom = "bar", fun.y = mean) +
geom_point(aes(color = Species)) +
scale_fill_manual(values = light_colors) +
scale_color_manual(values = dark_colors) +
labs(fill = "Mean by Species")
Second, if you do need a filled shape, let geom_point get a fill scale and hack the bars to have a color instead. One way to do that is by making what look like bars but are actually really big geom_segments. I changed the size in the legend to make the legend keys not ridiculously huge.
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(xend = Species, yend = 0, color = Species), geom = "segment", fun.y = mean, size = 30, lineend = "butt") +
geom_point(aes(fill = Species), color = "black", shape = 21) +
scale_fill_manual(values = light_colors) +
scale_color_manual(values = dark_colors, guide = guide_legend(override.aes = list(size = 4)))
Third way, make a data frame of averages and give it a variable to denote that it's got averages, then add a variable to the original data frame to denote that it's observations. Then you can map the interaction of type with species to get separate colors in one fill scale.
avgs <- iris %>%
group_by(Species) %>%
summarise(Sepal.Length = mean(Sepal.Length)) %>%
mutate(type = "Mean")
iris %>%
select(Species, Sepal.Length) %>%
mutate(type = "Observation") %>%
ggplot(aes(x = Species, y = Sepal.Length, fill = interaction(Species, type))) +
geom_col(data = avgs) +
geom_point(color = "black", shape = 21)
Not quite a perfect solution, but it may be a sufficient workaround
cols_1 <- c("red", "green", "blue")
cols_2 <- c("orange", "purple", "yellow")
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_point(aes(color = Species)) + # Using color instead of fill
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean, alpha = c(0.5, 0.05, 1)) +
scale_color_manual(values = cols_1) + # colors your points
scale_fill_manual(values = cols_2) # fills your Summary Bars
Adjust the colors, alpha, and other graphical parameters as you see fit.
Related
I'm wanting to prepare a simple plot with some points and horizontal lines with a legend. The code below generates the desired plot and a legend but the legend symbols are combinations of the shape and line, when I would just like a shape for the shape and a line for the line.
dat <- iris %>% select(Sepal.Length)
dat$Type <- "Sepal.Length"
ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, colour = Type), shape = 10, size = 2) +
geom_hline(aes(yintercept = 6, colour = "Some line"), linetype = "dashed")
Custom linetypes and shapes are assigned using scale_*_manual, like so:
dat %>%
ggplot() +
geom_point(aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), size = 2) +
geom_hline(aes(yintercept = 6, linetype = 'Some line')) +
scale_linetype_manual(values = c('Some line' = 'dashed')) +
scale_shape_manual(values = c('Sepal.Length' = 10))
i need the plan legend
How to add a legend manually for geom_line
ggplot(data = impact_end_Current_yr_m_actual, aes(x = month, y = gender_value)) +
geom_col(aes(fill = gender))+theme_classic()+
geom_line(data = impact_end_Current_yr_m_plan, aes(x=month, y= gender_value, group=1),color="#288D55",size=1.2)+
geom_point(data = impact_end_Current_yr_m_plan, aes(x=month, y=gender_value))+
theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom", axis.text.x = element_text(face = "bold", color = "black", size = 10, angle = 0, hjust = 1))+
labs(x="", y="End Beneficiaries (in Num)", fill="")+
scale_fill_manual(values=c("#284a8d", "#00B5CE","#0590eb","#2746c2"))+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE)
The neatest way to do it I think is to add colour = "[label]" into the aes() section of geom_line() then put the manual assigning of a colour into scale_colour_manual() here's an example from mtcars (apologies that it uses stat_summary instead of geom_line but does the same trick):
library(tidyverse)
mtcars %>%
ggplot(aes(gear, mpg, fill = factor(cyl))) +
stat_summary(geom = "bar", fun = mean, position = "dodge") +
stat_summary(geom = "line",
fun = mean,
size = 3,
aes(colour = "Overall mean", group = 1)) +
scale_fill_discrete("") +
scale_colour_manual("", values = "black")
Created on 2020-12-08 by the reprex package (v0.3.0)
The limitation here is that the colour and fill legends are necessarily separate. Removing labels (blank titles in both scale_ calls) doesn't them split them up by legend title.
In your code you would probably want then:
...
ggplot(data = impact_end_Current_yr_m_actual, aes(x = month, y = gender_value)) +
geom_col(aes(fill = gender))+
geom_line(data = impact_end_Current_yr_m_plan,
aes(x=month, y= gender_value, group=1, color="Plan"),
size=1.2)+
scale_color_manual(values = "#288D55") +
...
(but I cant test on your data so not sure if it works)
I have a dataset containing 1,000 values for a model, these values are all within the same range (y=40-70), so the points overlap a ton. I'm interested in using color to show the density of the points converging on a single value (y=56.72) which I have indicated with a horizontal dashed line on the plot below. How can I color these points to show this?
ggplot(data, aes(x=model, y=value))+
geom_point(size=1) +
geom_hline(yintercept=56.72,
linetype="dashed",
color = "black")
I think that you should opt for an histogram or density plot:
n <- 500
data <- data.frame(model= rep("model",n),value = rnorm(n,56.72,10))
ggplot(data, aes(x = value, y = after_stat(count))) +
geom_histogram(binwidth = 1)+
geom_density(size = 1)+
geom_vline(xintercept = 56.72, linetype = "dashed", color = "black")+
theme_bw()
Here is your plot with the same data:
ggplot(data, aes(x = model, y = value))+
geom_point(size = 1) +
geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")
If your model is iterative and do converge to the value, I suggest you plot as a function of the iteration to show the convergence. An other option, keeping a similar plot to your, is dodging the position of the points :
ggplot(data, aes(x = model, y = value))+
geom_point(position = position_dodge2(width = 0.2),
shape = 1,
size = 2,
stroke = 1,
alpha = 0.5) +
geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")
Here is a color density plot as you asked:
library(dplyr)
library(ggplot2)
data %>%
mutate(bin = cut(value, breaks = 10:120)) %>%
dplyr::group_by(bin) %>%
mutate(density = dplyr::n()) %>%
ggplot(aes(x = model, y = value, color = density))+
geom_point(size = 1) +
geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")+
scale_colour_viridis_c(option = "A")
I would suggest to use the alpha parameter within the geom_point. You should use a value close to 0.
ggplot(data, aes(x=model, y=value)) +
geom_point(size=1, alpha = .1) +
geom_hline(yintercept=56.72, linetype="dashed", color = "black")
By using R, is it possible to place 2 ggplot together (i.e., on the same plot) but with different bar of color gradient? My code, e.g.,
library(ggplot2)
ggplot(df1, aes(duration, slopes, col = color)) +
geom_point(size = 3) +
scale_color_gradient(low = "black", high = "red")
ggplot(df2, aes(duration, slopes, col = color)) +
geom_point(size = 3) +
scale_color_gradient(low = "blue", high = "green")
produces the following two pictures
I wish instead to be able to integrate them together in one plot with a bar for red and black and another bar for blue and green.
Yes you could if you use the ggnewscale package:
a <- sample(nrow(iris), 75)
df1 <- iris[a,]
df2 <- iris[-a,]
library(ggnewscale)
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
geom_point(data = df1, aes(colour = Petal.Length)) +
scale_colour_gradientn(colours = c("red", "black")) +
# Important: define a colour/fill scale before calling a new_scale_* function
new_scale_colour() +
geom_point(data = df2, aes(colour = Petal.Width)) +
scale_colour_gradientn(colours = c("blue", "white"))
Alternatives are the relayer package, or the scale_colour_multi/scale_listed from ggh4x (full disclaimer: I wrote ggh4x).
EDIT: Here are the alternatives:
library(ggh4x)
# ggh4x scale_colour_multi (for gradientn-like scales)
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
geom_point(data = df1, aes(length = Petal.Length)) +
geom_point(data = df2, aes(width = Petal.Width)) +
scale_colour_multi(colours = list(c("red", "black"), c("blue", "white")),
aesthetics = c("length", "width"))
# ggh4x scale_listed (for any non-position scale (in theory))
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
geom_point(data = df1, aes(length = Petal.Length)) +
geom_point(data = df2, aes(width = Petal.Width)) +
scale_listed(list(
scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length"),
scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width")
), replaces = c("colour", "colour"))
library(relayer)
# relayer
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
rename_geom_aes(geom_point(data = df1, aes(length = Petal.Length)),
new_aes = c("colour" = "length")) +
rename_geom_aes(geom_point(data = df2, aes(width = Petal.Width)),
new_aes = c("colour" = "width")) +
scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length",
guide = guide_colourbar(available_aes = "length")) +
scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width",
guide = guide_colourbar(available_aes = "width"))
All the alternatives give warnings about unknown aesthetics, but this doesn't matter for the resulting plots. It is just a line of code in ggplot's layer() function that produces this warning and you can't go around this without either re-coding every geom wrapper or, as ggnewscale does, renaming the old aesthetic instead of providing a new aesthetic. The plots all look near-identical, so I figured I wouldn't have to post them again.
How do I change fill pattern or color group by group? My current code only changes the outline; I want to change the fill pattern or fill color group (not outline) by sex but also keep my fill gradient which is by Eye.
colors <- c("Green", "Blue", "Hazel", "Brown")
data <- data.frame(HairEyeColor)
data$Eye <- as.numeric(factor(data$Eye, labels = 1:4))
data <- data[c(5,6,12,15,17,22,27,28), ]
ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) +
geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex)) +
scale_fill_continuous(low = "blue", high = "green")
Bar plot from ggplot2 package does not support fill pattern at the moment (and as far as i know it is not possible with other packages neither).
However there are few solution that are going to help spot the difference in sex and eye easily which you can consider:
1.Using different (lighter) fill colours,thicker bar boundaries and theme_bw():
ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) +
geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex), size=2) +
scale_fill_continuous(low = "white", high = "grey") + theme_bw()
Merging two columns: Sex and Eye to get the new factor column which is going to be used as a fill argument:
data$Sex_Eye <- paste(data$Sex, data$Eye, sep="_")
ggplot(data, aes(x = Hair, y = Freq, fill = Sex_Eye)) +
geom_bar(stat = "identity", position = position_dodge()) + theme_bw()
Using geom_jitter() instead of geom_bar() and setting up shape argument as Sex:
ggplot(data, aes(x = Hair, y = Freq, colour = Eye, shape = Sex)) +
geom_jitter(size=5) + scale_colour_continuous(low = "blue", high = "green") + theme_bw()