ggplot legend not working with scale_colour_manual
You have to use show_guide=TRUE
in geom_vline
(defaults to FALSE
):
p <- data.frame(a = runif(10, 1, 2))
ggplot(data=p, aes(x=a)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(a), colour="mea"), show_guide=TRUE) +
geom_vline(aes(xintercept=median(a), colour="med"), show_guide=TRUE) +
scale_colour_manual(name="Statistic",
values=c("med"= "red", "mea"="green"))
scale_color_manual not working for assigning custermized color
so the fix is very easy, change scale_color_manual
to scale_fill_manual
If you want colors to be the color in your color
variable, just do this:
scale_fill_manual(
breaks = c("A", "B", "C"),
values = color)
Can't add legend to ggplot2 figure - scale_color_manual() does nothing
Move the color to your aes()
and it works as expected
ggplot(data = averages, aes(x = as.integer(Deciles))) +
scale_x_reverse(breaks = 10:1) +
geom_line(aes(y = P_pred, color = 'darkred'), size = 2) +
geom_line(aes(y = P_true, color = 'darkgreen'), size = 2) +
ylab('Probabilities') +
xlab('Deciles') +
scale_color_manual(values = c('darkred','darkgreen'))
ggplot, scale_colour_manual issues
I advise you to look at how ggplot and "grammar of graphics" works (here for example: https://ramnathv.github.io/pycon2014-r/visualize/ggplot2.html).
So first you need to reshape your data to meet the requirements of ggplot:
full <- full %>% pivot_longer(cols = ends_with(c("AS","GDPpC")),
names_to = c("country", ".value"),
names_sep="_") %>%
rename("year" = "X1")
The resulting tibble:
# A tibble: 50 x 4
year country AS GDPpC
<dbl> <chr> <dbl> <dbl>
1 2011 Mali 8.29 6.73
2 2011 Nigeria 9.32 7.82
3 2011 Senegal 7.54 7.22
4 2011 Afghanistan 9.94 6.38
5 2011 Iraq 9.43 8.71
6 2012 Mali 7.75 6.66
7 2012 Nigeria 8.56 7.91
8 2012 Senegal 7.70 7.18
9 2012 Afghanistan 9.90 6.46
10 2012 Iraq 9.30 8.83
# ... with 40 more rows
Then you can use the ggplot correctly:
ggplot(data = full, mapping = aes(x = GDPpC, y = AS, col = country))+
geom_point()+
scale_color_manual(values = c("Afghanistan"="darkgreen","Iraq"="red" ,"Mali"="green", "Nigeria"="purple","Senegal"="orange"))+
geom_smooth(method = "lm")+
labs (x = "Log - GDP per Capita", y = "Log - Asylum Applications - First Time", colour = "Legend") +
theme_classic()
scale_color_manual() not working
As per the commenter above, I need to map color
to a variable, the following works
ggplot(df, aes(x=cond, y=yval, color = cond)) + geom_point() +
scale_color_manual(values=c("red", "blue", "green"))
Scale_color_manual not working as expected
There is no reproducible data. I, therefore, created a simple data here. I also simplified the code of the OP. What is necessary here is scale_fill_manual
.
mydf <- data.frame(time = letters[1:3],
variable = LETTERS[1:3],
value = runif(3, 10, 15),
stringsAsFactors = FALSE)
ggplot(mydf, aes(x=time, y=value, fill=variable)) +
geom_bar(stat="identity") +
scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a"))
Manual legend (scale_colour_manual) missing in plot with several aesthetics
You were almost there. The color variable inside the aes needs to be mapped to an actual colour, and the colour outside aes is unncessary.
ggplot() +
geom_point(data=df, aes(colour='one', x=gp, y=y)) +
geom_point(data=ds, aes(colour='two', x=gp, y=mean))+
geom_errorbar(data=ds, aes(colour='three', x=gp, y=mean, ymin=mean-sd, ymax=mean+sd))+
scale_color_manual(values=c(one='red', two='green', three='blue'),
breaks=c("one","two","three"))
Related Topics
Count Unique Combinations of Values
Implementation of Skyline Query or Efficient Frontier
Draw Lines Between Different Elements in a Stacked Bar Plot
How to Add Se Error Bars to My Barplot in Ggplot2
Extracting Indices for Data Frame Rows That Have Max Value for Named Field
Ggplot Bar Plot Side by Side Using Two Variables
How Is J() Function Implemented in Data.Table
Cast String Directly to Idatetime
How to Merge Two Nodes into a Single Node Using Igraph
Include Text Control Characters in Plotmath Expressions
Creating New Shape Palettes in Ggplot2 and Other R Graphics
Using Lm in List Column to Predict New Values Using Purrr
Cannot Read File with "#" and Space Using Read.Table or Read.CSV in R
Plot a Jpg Image Using Base Graphics in R
Why Does Is.Vector() Return True for List
Tidyr::Pivot_Wider() Reorder Column Names Grouping by 'Name_From'