I have an issue choosing the custom colouring of my plot, I am trying to add my own values to the plot but I can's seem to understand where should I put the custom color vector:
col <- c("#004d8d", "#cc2701", "#e5b400")
This is the plot code right now:
p1 <- ggplot(data = densdf1, mapping = aes(x = x, y = y)) +
geom_area(data = densdf1[densdf1$CI,],
aes(fill = Electrode, color = Electrode),
outline.type = "full", alpha = 0.3, size = 1) +
geom_line(aes(color = Electrode), size = 1) +
scale_fill_discrete(breaks=c("Fz","Cz","Pz")) +
guides(colour = "none") +
geom_vline(xintercept = 0) +
lims(x = c(-3, 2), y = c(0, 2.25)) +
labs(title="INTERVAL 225-275ms", x="VALUES", y="DENSITY") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=12),
plot.title=element_text(size=14))
This is the plot with default colours and it looks perfectly fine, but I'd like to customise the colours.
As you can see there is a contour line and the area from the density that need to be coloured.
I tried changing the aesthetics but I think I am not understanding the logic. I tried also using scale_manual_fill(values = col) removing scale_fill_discrete(breaks=c("Fz","Cz","Pz")) but it only works with the area inside.
Can any ggplot2 expert give me a hint? Thanks!
in the aes(...) section you define scales, and the color must be adjusted for each scale. In your case it would be the "fill" and the "color" scale, hence the following two lines must be added to adjust both scales:
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
If you want to make sure, that a certain category gets a certain color, you have to specify the breaks, and the order the vector corresponds to the order of the color vector.
I have a plot with a discrete colorscheme (109 different levels) and a colorstep legend.
The legend then shows all ticks for each color. How can I only show a few of the labels and ticks in the legend rather than all?
Plot 1 shows the legend when the labels are still there
Plot 2 shows the legend without the labels. I want to show only certain labels in the legend, to make it actually understandable (10 different values rather than 100). How do I do that?
My code is below, I am clearly doing something wrong, I just do not know what it is.
tt <- ggplot() +
# first layer: all countries, no fill, no white outlines
geom_polygon(data = maps2,
aes(x = long, y = lat, group = group), fill="grey", show.legend = F, size = 0.1) +
# second layer: only countries with a fill
geom_polygon(data = test,
aes(x = long, y = lat, group = group, fill = as.factor(tally)), show.legend = T)+
scale_fill_viridis_d(option="D", na.value = NA)+
facet_grid(~IO)+
theme( strip.text.x = element_blank())
tt + guides(fill=guide_colorsteps(label = F, title=element_blank())
I made a plot using ggplot in R and wanted to have labels with the values that correspond with each of the points on the plot. I used the function geom_label_repel to get the labels on the plot, but it seems like it changed the legend to a letter instead of a dot, which I don't really like. Is there a way to change the shape of the symbol in the legend to a dot instead?
Here's the code for my plot:
ggplot(CI_bar_df_null, mapping = aes(x = model, y = R2, color = condition, group = condition)) +
geom_point() +
geom_line() +
geom_label_repel(
label = CI_bar_df_null$R2factor,
nudge_x = .3, nudge_y = 0) +
geom_errorbar(aes(ymin = CI_lower, ymax = CI_upper), width = .3) +
#ylim(-.06, .6)
ggtitle('Model R-Squared Values and Confidence Intervals') +
ylab('R-Squared Value') +
xlab('Model Type') +
scale_color_discrete(name = "Condition") +
scale_shape_discrete(shape = 17)
You can simply add the show.legend = F argument inside geom_label_repel. This will avoid creating a legend for geom_label_repel and leave intact the other two legends (geom_point and geom_line).
geom_label_repel(
label = mtcars$cyl,
nudge_x = .3,
nudge_y = 0,
show.legend = F)
I want to remove the color line from a fill legend for a ggplot. I usually use guide_legend(override.aes = ...) to modify legend aesthetics - works great for points, lines, alpha, etc., but it's not working for my color aesthetic. What am I doing wrong?
# generate data
set.seed(47)
data = data.frame(year = rep(2000:2004, 3),
value = runif(15),
group = rep(c("A", "B", "C"), each = 5))
# create the plot
p = ggplot(data, aes(x = year, y = value, fill = group)) +
geom_area(position = position_fill(), color = "white") +
scale_fill_grey()
# this should modify the fill legend to remove the colored line
# but the line is still there
p + guides(fill = guide_legend(override.aes = list(color = NA)))
This was one of the cases where colour needed to be spelt with u. Adding the u made override.aes work just fine. ggplot2 releases since 2016 have fixed this bug, and you can use either spelling.
p + guides(fill = guide_legend(override.aes = list(colour = NA)))
My question is very similar to this and this and also this question. I have a scatterplot (using geom_point) coloured by a factor, using a particular colour palette. I'm using stat_smooth to draw certain smoothing lines through the points, grouped by another factor. I'd like these lines to use a different colour palette.
Here is a Dropbox link to some example data. Just do a
currDT <- read.table("SO_data", sep = "|", header = TRUE, strip.white = TRUE)
I usually have my data in a data.table, so you might find it helpful change that as well. Oh and here is the colour scheme I'm using at the moment, you can use scale_colour_brewer to generate your own, I'm just including this for completeness.
my_col_scheme <- c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#7B8A7B",
"#0B29D6", "#f781bf", "#999999", "black")
Hopefully that is clear enough. Here is some example code:
icorr_elec <- ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
geom_point(aes(colour = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_colour_manual(values = my_col_scheme) +
stat_smooth(method = "lm", formula = y ~ x + 0, aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides"))
How can this be done in ggplot2? From the other questions I gathered I could specify each line manually, but I'd like to avoid that.
You can get separate color mappings for the lines and the points by using a filled point marker for the points and mapping that to the fill aesthetic, while keeping the lines mapped to the colour aesthetic. Filled point markers are those numbered 21 through 25 (see ?pch). Here's an example, adapting #RichardErickson's code:
ggplot(currDT, aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides")) +
geom_point(aes(fill = Anion, shape = Cation), size = 3, alpha = 0.4, colour="transparent") +
scale_colour_manual(values = c("blue", "red")) +
scale_fill_manual(values = my_col_scheme) +
scale_shape_manual(values=c(21,24)) +
guides(fill = guide_legend(override.aes = list(colour=my_col_scheme[1:8],
shape=15, size=3)),
shape = guide_legend(override.aes = list(shape=c(21,24), fill="black", size=3)),
colour = guide_legend(override.aes = list(linetype=c("dotdash", "F1"))),
linetype = FALSE)
Here's an explanation of what I've done:
In geom_point, change colour aesthestic to fill. Also, put colour="transparent" outside of aes. That will get rid of the border around the points. If you want a border, set it to whatever border color you prefer.
In scale_colour_manual, I've set the colors to blue and red, but you can, of course, set them to whatever you prefer.
Add scale_fill_manual to set the colors of the points using the fill aesthetic.
Add scale_shape_manual and set the values to 21 and 24 (filled circles and triangles, respectively).
All the stuff inside guides() is to modify the legend. I'm not sure why, but without these overrides, the legends for fill and shape are blank. Note that I've set fill="black" for the shape legend, but it's showing up as gray. I don't know why, but without fill="somecolor" the shape legend is blank. Finally, I overrode the colour legend in order to include the linetype in the colour legend, which allowed me to get rid of the redundant linetype legend. I'm not totally happy with the legend, but it was the best I could come up with without resorting to special-purpose grobs.
NOTE: I changed color=NA to color="transparent", as color=NA (in version 2 of ggplot2) causes the points to disappear completely, even if you use a point with separate border and fill colors. Thanks to #aosmith for pointing this out.
I don't think ggplot2 will let you change color twice and update the legend. I seem to recall reading that you cannot change scale_color_manual twice in a plot. I couldn't find that webpage, but this post touches on the topic. I also tried using the suggestions in this post, but that didn't work. Probably because we're trying to mix geoms (but that's just a guess).
I can get either the regression lines colored:
part1 <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"),
labels = c("Halides", "Non-Halides")) +
scale_color_manual(name = "", values = c("red", 'blue'),
labels = c("Halides", "Non-Halides"))
ggsave('part1.jpeg', part1)
Or the data points to plot:
part2 <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
geom_point(aes(color = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"),
labels =c("Halides", "Non-Halides")) +
scale_colour_manual(values = my_col_scheme)
ggsave('part2.jpeg', part2)
But not both:
both <-
ggplot(currDT,
aes(x = EFP, y = SAPT), na.rm = TRUE) +
stat_smooth(method = "lm", formula = y ~ x + 0,
aes(linetype = Halide, colour = Halide),
alpha = 0.8, size = 0.5, level = 0) +
scale_linetype_manual(name = "", values = c("dotdash", "F1"),
breaks = c("hal", "non-hal"), labels = c("Halides", "Non-Halides")) +
geom_point(aes(color = Anion, shape = Cation), size = 3, alpha = 0.4) +
scale_colour_manual(values = my_col_scheme)
ggsave('both.jpeg', both)
I think you're going to need to add each line manual. Hopefully somebody else knows how to answer this, but I don't think #hadley allows both colors to changed. Luckily, you're data.table so it should be easy to do :-)
Comment to anybody else trying to solve this Hopefully my partial answer helps you to answer this question. Also, my third figure shows how ggplot2 isn't getting the legend color correct as the OP wants it. As another tip, you might try playing around with the legend options.
For completeness sake - adding two scales for the same aesthetic is now easily possible with the ggnewscale package