简体   繁体   中英

scale_fill_discrete does not change legend labels -ggplot R

I am having trouble changing my legend labels when I use ggplot2 in R. I have plotted a line graph with 7 lines corresponding to proportion of specimens with feeding strategy(y) over temperature zones (x). So far I have tried using 2 different methods to change the labels (which are currently 1-7) but neither changes anything. These are what I have tried:

plot + scale_fill_discrete("Feeding Type",breaks=c("1", "2", "3", "4", "5", "6", "7"),
   labels=c("Fungivorous", "Herbivorous", "Saprophagous", "Predacious", "Xylophagous", "Parasitoid", "Algivorous"))

and

plot + scale_shape_manual("Feeding Type",breaks=c("1", "2", "3", "4", "5", "6", "7"),
   labels=c("Fungivorous", "Herbivorous", "Saprophagous", "Predacious", "Xylophagous", "Parasitoid", "Algivorous"))

Updated code with an example dataset which generates a plot with 7 different lines based on feeding (color and line type) showing response of proportion to temperature:

feedings <- c("Fungivorous", "Herbivorous", "Saprophagous", "Predacious",
        "Xylophagous", "Parasitoid", "Algivorous")

tempzone <- c("-5 to 0", "0 to 5","5 to 10", "10 to 15")
tempzone <- factor(tempzone, levels=c("-5 to 0", "0 to 5","5 to 10", "10 to 15"))

proportion <- c(0.05,0.1,0.15,0.3)

F <- data.frame(Temperature=tempzone, Proportion=proportion, Feeding="Fungivorous")
H <- data.frame(Temperature=tempzone, Proportion=proportion+.1, Feeding="Herbivorous")
S <- data.frame(Temperature=tempzone, Proportion=proportion+.2, Feeding="Saprophagous")
P <- data.frame(Temperature=tempzone, Proportion=proportion+.3, Feeding="Predacious")
X <- data.frame(Temperature=tempzone, Proportion=proportion+.4, Feeding="Xylophagous")
Pa <- data.frame(Temperature=tempzone, Proportion=proportion+.5, Feeding="Parasitoid")
A <- data.frame(Temperature=tempzone, Proportion=proportion+.6, Feeding="Algivorous")

data <- rbind.data.frame(F,H,S,P,X,Pa,A)

feedingPLOT <- ggplot(data=data, aes(x=Temperature, y=Proportion, group=Feeding, linetype=Feeding, color=Feeding))+
        geom_line() + 
        scale_color_manual(values=c("Fungivorous"="blue","Herbivorous"="red","Saprophagous"="green",
                        "Predacious"="orange","Xylophagous"="black","Parasitoid"="purple","Algivorous"="yellow"))

所需的绘图输出

Does this resolve your issue?

You did not use the fill parameter in your ggplot() call, so you cannot use scale_fill_discrete(). You are probably looking for scale_color_discrete(), scale_group_discrete(), or scale_linetype_discrete() depending on which legend you want to change the labels for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM