【问题标题】:Error: colours encodes as numbers must be positive错误:颜色编码为数字必须为正
【发布时间】:2021-10-25 15:44:15
【问题描述】:

我正在尝试重新创建此图,但我遇到了一个问题,即 ggplot 不喜欢数据框中的负数,从错误消息的外观来看? Error: colours encodes as numbers must be positive。有谁知道它的问题是什么?这些是非常大的数据框,但我没想到这会是个问题?

## Load packages
library(tidyverse)
require(data.table)

## Read in data frames
m1<-fread("m1.csv", header = F)
m2<-fread("m2.csv", header = F)
L<-fread("l.csv", header = F)
LP<-fread("LP.csv", header = F)

## Get rate by taking m1 from m2

rate<-m1[1,]-m2[1,] ### subtract p1 rate from p2

## Transpose the data frame

t_rate <- transpose(rate) 

## Create row ID's to merge data frames
L$row_num <- seq.int(nrow(L))  
t_rate$row_num <- seq.int(nrow(t_rate)) 

all<-merge(L, t_rate, by = "row_num") ## merge the dataframes based on their ID

## Get rid of ID now we don't need it

all$row_num=NULL

## Plot the graph
ggplot(all,x=all$V1.x,y=all$V2,col=all$V1.y)+
geom_point(data=all,x=all$V1.x,y=all$V2,col=all$V1.y,size=0.1)+
geom_point(data=LP,x=LP$V1,y=LP$V2,size=1)

### Data (all)

structure(list(V1.x = c(163.75, 164.25, 164.75, 165.25, 165.75, 
166.25), V2 = c(-75.25, -75.25, -75.25, -75.25, -75.25, -75.25
), V1.y = c(1.55995, 1.56093, 1.56237, 1.56545, 1.56764, 1.56827
)), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L), .internal.selfref = <pointer: 0x7f9bd4811ae0>)

## Data (LP)

structure(list(V1 = c(169.7, 147.93, 150.01, 146.71, 147.31, 
-63.26), V2 = c(-46.47, -42.344, -36.59, -38.64, -43.3, 44.739
)), row.names = c(NA, -6L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7f9bd4811ae0>)

【问题讨论】:

    标签: r ggplot2 geospatial spatial


    【解决方案1】:

    问题在于您没有根据美学进行映射,而是将向量传递给参数。这样做时,您必须将颜色名称或代码或正数传递给 color 参数。

    但要解决您的问题,您可以像这样简单地映射美学:

    library(ggplot2)
    
    ggplot(all, aes(x = V1.x, y = V2)) +
      geom_point(aes(color = V1.y), size = 0.1) +
      geom_point(data = LP, aes(x = V1, y = V2), size = 1)
    

    【讨论】:

    • 啊,我明白了,非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多