Changing the legend names: R
1
0
Entering edit mode
6.7 years ago
KVC_bioinfo ▴ 600

Hello all,

ggplot(dataset, aes( x = Norm, fill = Condition, y=log2(measure))) + geom_boxplot() +
    scale_fill_manual(values=c("brown1","darkolivegreen4","burlywood3")) + theme_classic() + 
    labs(title="Comparison",x="Norm", y = "counts") +
    scale_fill_continuous(labels = paste("condition1", "condition2", "condition3"))

I am trying to change the labels of the legend. Currently, it shows the labels that are present in my dataset. However, I want to change it to "condition1", "condition2", "condition3". Above is the code I am trying. It gives me following error.

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale. Error: Discrete value supplied to continuous scale In addition: Warning message: Removed 18 rows containing non-finite values (stat_boxplot).

Could someone help me here? TIA

R ggplot2 • 27k views
ADD COMMENT
0
Entering edit mode

Not strictly a bioinformatics question, IMO. However, there is some overlap, enough to not close the question.

ADD REPLY
0
Entering edit mode

See if this thread can help you: R: ggplot2 specify dataset for scale_fill_continuous

If the solution provided there doesn't help, maybe this quote can help:

It is difficult to help without a reproducible example

ADD REPLY
0
Entering edit mode

Another thing you might consider is making a copy of the 'Condition' column in your dataframe, converting it to Factor, then changing the 'levels' of the factor to "condition1", "condition2", etc.. This way, all the entries are still part of the same groupings, but those groups have the desired name for the plot.

ADD REPLY
2
Entering edit mode
6.6 years ago

You cannot use scale_fill_manual and scale_fill_continuous at the same time - they access the same information in the plot.

What you need is this:

ggplot(dataset, aes( x = Norm, fill = Condition, y=log2(measure))) + geom_boxplot() +
    scale_fill_manual(name="My new legend", values=c("brown1","darkolivegreen4","burlywood3", labels=c("condition1", "condition2", "condition3")) +
    theme_classic() + 
    labs(title="Comparison",x="Norm", y = "counts"))
ADD COMMENT

Login before adding your answer.

Traffic: 1936 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6