【问题标题】:GGPlot's scale_color_manual shows errorGGPlot 的 scale_color_manual 显示错误
【发布时间】:2016-12-06 17:58:38
【问题描述】:

有两个问题有类似的问题,但它们不适用于此,所以请不要标记为重复。

我得到了一个普通的 ggplot,其中包含一组值和 3 条水平线,但是当我使用 scale_colour_manual 时它会返回

c 中的错误(`Total Tweets` = "#f04546", Mean = "#3591d1", `Standard Deviation` = "#62c76b") :未使用的参数(`Total Tweets` = "#f04546", 平均值 = "#3591d1", `标准偏差` = "#62c76b")

Data<-data.frame("Date"=as.Date(16200:16499),"Total"=rnorm(300,4500,50))
Mean<-mean(Data$Total)
SD1<-Mean-sd(Data$Total)
SD2<-Mean+sd(Data$Total)

TotalDay <- ggplot(data = Data, aes(x=Date, y=Total,colour=Legend)) + 
  geom_line(aes(y=Total, colour="Total Tweets")) 

TotalDay + ggtitle("Tweets per Day") +labs(x="Date",y="Tweets") +
  theme(plot.title = element_text(color="#666666", face="bold", size=18, hjust=0)) +
  theme(axis.title = element_text(color="#666666", face="bold", size=13)) +
  geom_hline(aes(yintercept =Mean,colour="Mean")) + 
  geom_hline(aes(yintercept =(SD1), 
                 colour="Standard Deviation"))+
  geom_hline(aes(yintercept =(SD2), 
                 colour="Standard Deviation"))

TotalDay + scale_color_manual(name="Legend",
                              values=c("Total Tweets"="#f04546","Mean"="#3591d1","Standard Deviation"="#62c76b"))

所以只有最后一行不起作用。 我使用包(我不知道这是否有区别):

library(stringr)
library(dplyr)
library(Ryacas)
library(quantmod)
library(data.table)
library(tm)
library(lubridate)
library(ggplot2)
library(extrafont)

【问题讨论】:

  • 我在运行您的代码时没有收到错误消息。 (我正在使用 ggplot2 2.2.0 和 R 3.3.2)
  • 我也没有收到错误。你是否不小心用不同的函数覆盖了c?如果你只运行c,你应该得到function (...) .Primitive("c")。如果你得到不同的东西,那么问题几乎肯定是你保存了一个不同的函数来代替c
  • 是的,我之前用c作为函数,谢谢!

标签: r ggplot2


【解决方案1】:

在您的第二步中,您实际上并没有将绘图结果重新分配回TotalDay,因此当您到达scale_color_manual 时,您没有可以实际构建图例的线条。

Data<-data.frame("Date"=as.Date(16200:16499),"Total"=rnorm(300,4500,50))
Mean<-mean(Data$Total)
SD1<-Mean-sd(Data$Total)
SD2<-Mean+sd(Data$Total)

TotalDay <- ggplot(data = Data, aes(x=Date, y=Total,colour=Legend)) + 
  geom_line(aes(y=Total, colour="Total Tweets")) 

TotalDay <- TotalDay + ggtitle("Tweets per Day") +labs(x="Date",y="Tweets") +
  theme(plot.title = element_text(color="#666666", face="bold", size=18, hjust=0)) +
  theme(axis.title = element_text(color="#666666", face="bold", size=13)) +
  geom_hline(aes(yintercept =Mean,colour="Mean")) + 
  geom_hline(aes(yintercept =(SD1), 
                 colour="Standard Deviation"))+
  geom_hline(aes(yintercept =(SD2), 
                 colour="Standard Deviation"))

TotalDay + scale_color_manual(name="Legend",
                              values=c("Total Tweets"="#f04546","Mean"="#3591d1","Standard Deviation"="#62c76b"))

【讨论】:

  • 我的主要问题是在 cmets 中(我之前使用 c 作为函数),但后来你的回答也帮助了我,谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多