【问题标题】:scale_fill_discrete does not change legend labels -ggplot Rscale_fill_discrete 不会更改图例标签-ggplot R
【发布时间】:2017-07-21 22:37:29
【问题描述】:

当我在 R 中使用 ggplot2 时,我无法更改我的图例标签。我绘制了一个折线图,其中包含 7 条线,对应于温度区 (x) 上的进料策略 (y) 的样本比例。到目前为止,我已经尝试使用 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"))

【问题讨论】:

  • 我不会尝试在 ggplot 调用中更正标签。首先考虑变量并应用标签/级别和顺序。

标签: r ggplot2 legend legend-properties


【解决方案1】:

使用示例数据集更新代码,该数据集根据馈送(颜色和线型)生成包含 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"))

这能解决您的问题吗?

【讨论】:

  • 嗨蒂芙尼,这是制作情节的代码:feedPLOT
  • 我不确定你所说的数据有第 1 到第 7 行(只有 7 行......?)。无论哪种方式,我都制作了一个示例数据集,我认为它与您拥有的类似,并且上面的更新代码应该会有更多用处。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多