Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing points from geom_bar legend ggplot r

Tags:

r

legend

ggplot2

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

like image 235
Ricardo Avatar asked Oct 30 '25 01:10

Ricardo


1 Answers

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)))

enter image description here

like image 134
LAP Avatar answered Nov 01 '25 15:11

LAP