scale_fill_discrete不会更改图例标签-ggplot R.

时间:2017-07-21 22:37:30

标签: r ggplot2 legend legend-properties

当我在R中使用ggplot2时,我无法更改我的图例标签。我绘制了一条线图,其中7行对应于带有进料策略(y)的样品在温度区域(x)上的比例。到目前为止,我尝试使用2种不同的方法来更改标签(目前为1-7),但两者都没有改变。这些是我尝试过的:

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

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

2 个答案:

答案 0 :(得分:0)

使用示例数据集更新代码,该数据集基于进料(颜色和线型)生成具有7条不同线的图,显示与温度成比例的响应:

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

Desired plot output

这会解决您的问题吗?

答案 1 :(得分:0)

您没有在ggplot()调用中使用fill参数,因此不能使用scale_fill_discrete()。您可能正在寻找scale_color_discrete(),scale_group_discrete()或scale_linetype_discrete(),具体取决于您要为其更改标签的图例。