Difficulty adjusting color inn ggplot2 using r - r

I am trying to match plots I make in R to plots I make in python using matplotlib.
The current code I use does not match the custom colors I want to use correctly. What can I change to get this to work correctly?
My main concern is matching the colour of the lines.
I am trying to use scale fill manual. This changes the colours but not in the way I want.
ggplot(data = reactor.summarised.ci, aes(x=standard_time, y=value, group =
group, colour=group)) +
geom_line(size = 0.25)+
geom_ribbon(aes(x = standard_time, ymin = lower.ci.od, ymax = upper.ci.od),
show.legend =FALSE, alpha =0.2, colour = NA)+
ylab("O.D.")+
xlab("Time (min)")+
xlim(0, 350)+
ggtitle('OD Over Time in in Bioreactor 1.02 before adjustment')+
theme(plot.title = element_text(hjust = 0.5))+
scale_fill_manual(values=c("#1f77b4", "#ff7f0e", "#2ca02c", '#d62728'))+
newtheme
newtheme is defined as follows:
newtheme <- theme_classic()+
theme(plot.title = element_text(hjust = 0.5))
I want to assign the following colours to the figure legend, ribbon and line.
(ReactorA = "#1f77b4",
ReactorB = "#ff7f0e",
ReactorC = "#2ca02c",
Reactor '#d62728')
current plot generated

You are mapping group to color not fill. Therefore you have to use scale_color_manual to adjust the color of the lines. Try this:
ggplot(data = reactor.summarised.ci, aes(x=standard_time, y=value, group =
group, colour=group)) +
geom_line(size = 0.25)+
geom_ribbon(aes(x = standard_time, ymin = lower.ci.od, ymax = upper.ci.od),
show.legend =FALSE, alpha =0.2, colour = NA)+
ylab("O.D.")+
xlab("Time (min)")+
xlim(0, 350)+
ggtitle('OD Over Time in in Bioreactor 1.02 before adjustment')+
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_manual(values=c("reactorA" = "#1f77b4", "reactorB" = "#ff7f0e", "reactorC" = "#2ca02c", "reactorD" = '#d62728'))+
newtheme

Related

how can i add color to my graph using R language

i want to add colors for my graph, i many ways to change the color but R still so confusing to me.
here is the code
ggplot(pie_chart_mutate, aes(x = "", y = per, fill = gender)) +
geom_col(color = "black") +
geom_text(aes(label = labels),
position = position_stack(vjust = 0.5)) +
guides(fill = guide_legend(title = "Gender")) +
coord_polar(theta = "y") +
scale_fill_brewer() +
theme_void()
i tried the scale_fill_manual but i don't really know how to apply it correctly.
here is the output of the ggplot
i want the colors to be in hex like #0f3443 and #34e89e

How to add a legend manually for line chart

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)

ggplot legend / label change with various guides

I am trying to change the labels of a legend on a ggplot where I have legends for 2 aes.
With scale_file_manual, it works, but for one of the two legends only. I guess I should use "guides" that I already used to remove the titles and also remove legend for a 3rd aes, but I do not manage to do it.
Do you have a solution?
Here is my code :
p <- ggplot(data, aes(x = Nb))
p + geom_ribbon(aes(ymin = Sandwich.min, ymax = Sandwich.max, fill = 'grey70',alpha=0.8)) +
geom_ribbon(aes(ymin = Assiette.min, ymax = Assiette.max, fill = '#6495ED80',alpha=0.8)) +
geom_line(aes(y = Pizza, col = '#FF7F24')) +
geom_line(aes(y = Sushis, col = '#228B22')) +
labs(title = "Business lunch cost by number of participants",
x = "Number of participants",
y = "Price (Euros)") +
scale_x_continuous(breaks=seq(1,20,1)) +
scale_y_continuous(breaks = seq(0,300,50)) +
theme_light() +
theme(plot.title = element_text(size = 12, hjust = 0.5)) +
guides(alpha = FALSE, colour = guide_legend(" "), fill = guide_legend(" ")) +
scale_fill_manual(
values=c('#6495ED80','grey70'),
labels=c("Assiettes","Sandwiches"))

Removing points from geom_bar legend ggplot r

This is my data.
Mod <- as.factor(c(rep("GLM",5),rep("MLP",5),rep("RF",5),rep("SVML",5),rep("SVMR",5)))
Manifold <- as.factor(rep(c("LLE","Iso","PCA","MDS","kPCA"),5))
ROC <- runif(25,0,1)
Sens <- runif(25,0,1)
Spec <- runif(25,0,1)
df <- data.frame("Mod"= Mod, "Manifold"= Manifold, "ROC" = ROC, "Sens" = sens, "Spec" = spec)
And I am making this graph
resul3 <- ggplot(df, aes(x = Mod, y = ROC, fill= Manifold)) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
ylab("ROC & Specificity") +
xlab("Classifiers") +
theme_bw() +
ggtitle("Classifiers' ROC per Feature Extraction Plasma") +
geom_point(aes(y=Spec), color = "black", position=position_dodge(.9)) +
scale_fill_manual(name = "Feature \nExtraction", values = c("#FFEFCA",
"#EDA16A" ,"#C83741", "#6C283D", "#62BF94"))
first graph
And what I want is another legend with tittle "Specificity" and a single black point. I dont want the point to be inside the Manifolds legend.
Something like this but without the points inside the manifold squares
Changing the geom_point line, adding a scale_color_manual and using the override as seen in #drmariod's answer will result in this plot:
ggplot(df, aes(x = Mod, y = ROC, fill= Manifold)) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
ylab("ROC & Specificity") +
xlab("Classifiers") +
theme_bw() +
ggtitle("Classifiers' ROC per Feature Extraction Plasma") +
geom_point(aes(y=Spec, color = "Specificity"), position=position_dodge(.9)) +
scale_fill_manual(name = "Feature \nExtraction", values = c("#FFEFCA",
"#EDA16A" ,"#C83741", "#6C283D", "#62BF94")) +
scale_color_manual(name = NULL, values = c("Specificity" = "black")) +
guides(fill = guide_legend(override.aes = list(shape = NA)))
You can overwrite the aesthetics for shape and set it to NA like this
ggplot(df, aes(x = Mod, y = ROC, fill= Manifold)) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
ylab("ROC & Specificity") +
xlab("Classifiers") +
theme_bw() +
ggtitle("Classifiers' ROC per Feature Extraction Plasma") +
geom_point(aes(y=Spec), color = "black", position=position_dodge(.9)) +
scale_fill_manual(name = "Feature \nExtraction", values = c("#FFEFCA",
"#EDA16A" ,"#C83741", "#6C283D", "#62BF94")) +
guides(fill = guide_legend(override.aes = list(shape = NA)))

Change the smoothness on a volcano plot

I have produced a volcano plot, however the underlying data has gaps, i.e. the histogram data looks like:
When I produce the volcano plot it looks a bit silly:
Is it possible to apply a smoother to the shaded area to iron out the ribbed feature; surely, it must already have a smoothness associated with it, otherwise the shadow would drop back to 0 each time?
Code:
ggplot(fly2[fly2$Region == "different",], aes(x = Probability)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
fill = "grey50", colour = "grey50",
geom = "ribbon", position = "identity") +
facet_grid(. ~ Algorithm) + xlim(0,0.3) +
coord_flip()
link to the dput file:
http://pastebin.com/ba95WEab
Use adjust in geom_density.
For example, when I use adjust = 1.6, this is what I get
ggplot(fly2[fly2$Region == "different",], aes(x = Probability)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
fill = "grey50", colour = "grey50",
geom = "ribbon", position = "identity",
adjust=1.6) +
facet_grid(. ~ Algorithm) + xlim(0,0.3) +
coord_flip()

Resources