【问题标题】:Scale_colour_manual error "Insufficient values in manual scale"Scale_colour_manual 错误“手动刻度中的值不足”
【发布时间】:2018-09-10 07:35:42
【问题描述】:

我有一个类似这个例子的数据集:

percentile  average group
1   11.65   ID1
2   12.84   ID1
3   13.61   ID1
4   14.02   ID1
5   14.25   ID1
6   14.45   ID1
1   11.66   ID2
2   12.84   ID2
3   13.58   ID2
4   14.01   ID2
5   14.28   ID2
6   14.46   ID2

在完整的数据集中,我有 8 IDs100 rows 每个 IDs。我正在尝试使用此命令创建density plot in R

library(ggplot2)
data <- read.table("data.csv", sep=",", header = T )
data$percentile <- factor(data$percentile, levels = data$percentile)
pdf("Ala_all.pdf",width=20,height=10)
d = ggplot(data=data, aes(x=percentile, y=average, group=group, shape=group, colour=group)) +
     geom_line(size=2) + coord_cartesian(ylim = c(0, 17))
d + scale_color_manual(values=c("#bbbbbb", "#ff6362", "#b1b1ff", "#282828", "#b30100", "#0100b1", "#2927ff", "#000000") )
dev.off()

但是,它给出了这个error

> d + scale_color_manual(values=c("#bbbbbb", "#ff6362") )Error: Insufficient values in manual scale. 12 needed but only 2 provided.
In addition: Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

【问题讨论】:

  • 请添加一个更具描述性的标题
  • 错误很明显。您的颜色映射(我假设“组”列)有 800 个级别,但您只提供 8 种颜色。要调试此错误,您需要提供可重现的示例,我们无权访问您的数据。
  • @Jack Brookes:我更新了问题
  • @Jack Brookes:实际上我想为这两个组制作密度策略:ID1 和 ID2 在同一个情节中。
  • 请附上一个可重复的例子stackoverflow.com/questions/5963269/…

标签: r ggplot2


【解决方案1】:

错误在于data$percentile &lt;- factor(data$percentile, levels = data$percentile)。您正在设置具有多个级别的因素。你可能想要unique()

library(ggplot2)

mydata <- read.table(header = TRUE, text = "
percentile  average group
1   11.65   ID1
2   12.84   ID1
3   13.61   ID1
4   14.02   ID1
5   14.25   ID1
6   14.45   ID1
1   13.66   ID2
2   14.84   ID2
3   15.58   ID2
4   16.01   ID2
5   16.28   ID2
6   16.46   ID2")



mydata$percentile <- factor(mydata$percentile, levels = unique(mydata$percentile))

d <-
  ggplot(mydata,
         aes(
           x = percentile,
           y = average,
           group = group,
           shape = group,
           colour = group
         )) +
  geom_line(size = 2) +
  coord_cartesian(ylim = c(0, 17))


d + scale_color_manual(values = c("#bbbbbb", "#ff6362") )

【讨论】:

    猜你喜欢
    • 2013-09-30
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2020-07-27
    • 2022-09-23
    • 2018-09-02
    相关资源
    最近更新 更多