代码之家  ›  专栏  ›  技术社区  ›  Jack Armstrong

几何点的着色与标号

  •  1
  • Jack Armstrong  · 技术社区  · 7 年前

    enter image description here

    然而,我有一点困难的着色点,然后着色的标签,而不失去原来的点的颜色。我不知道我应该问多个问题还是全部问题。我一起计算,因为他们涉及到同一个图表,但如果这是不允许的,请让我知道,我可以编辑之前,它下来。这简直不公平。我在下面贴了一个可复制的例子,上面有一些评论,让它更简单。

    df2$plotter 我用它来创建数据的子集,然后绘制第二层。颜色向量, cdf,

    1. 着色点

    如果是一个级别,我会使用 scale_color_manual fill/color color vector 也就是说,它会根据向量中的值来着色。但是它没有使用我指定的颜色。取而代之的是标签,点 D to O 作为一个 murky green 而不是 grey 如十六进制代码所示: #A9A9A9 并将颜色作为传说的一部分。我更喜欢下面的地图。我不知道如何创建一个这样的颜色向量,即每个单元格的值被用作实际颜色,这个向量还需要对标签进行着色。第二,当我尝试传递第二个层次的向量时 aesthetics 在里面 geom_point Error: Aesthetics must be either lenght 1 or the same as the data plotter

    Alice (二者) Alice and Alice2 #b79f00

    Bob #00ba38

    Charlie #00bfc4

    Peter is #619立方英尺`

    Quin #F8766D

    Roger is #f564e3型`

    D到O #A9A9型

    1. 标签和着色

    geom_text. 然后我称之为相同的数据和美学。我的问题是上面提到的部分颜色,但现在当我给他们上色,我失去了我的记忆 color 但要保持 fill 我的观点。注意下面。我不知道为什么我的 在路上迷路了或者怎么修。我试着先画课文,然后再画要点,但这没有改变什么,我也猜不到。简言之,每个标签的颜色应与其点相同。

    enter image description here

    可复制数据:

    k<-18  
    ct<-12 
    x_vector<-seq(1,k,1)
    radius<-rep(5,k,1)
    name<-c('Alice','Bob','Charlie','D','E','F','G','H','I','J','K','L','M','N','O','Peter','Quin','Roger')
    df<-data.frame(x_vector,radius,name)
    
    name2<-c('Alice2','Bob2','Charlie2','D','E','F','G','H','I','J','K','L','M','N','O','Peter2','Quin2','Roger2')
    plotter<-c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)
    radius2<-rep(7,k,1)
    df2<-data.frame(x_vector,radius2,name2,plotter)
    
    color1<-c('#F8766D'
              ,'#F564E3'
              ,'#B79F00'
              )
    other_color<-c(rep('#A9A9A9',ct))
    color2<-c('#00BFC4'
              ,'#619CFF'
              ,'#00BA38'
              )
    cdf<-c(color1,other_color,color2)  #color palette
    df$label_radius<-df$radius+0.5  ##used to adjust the labels by a radius of 0.5
    p<-ggplot()+
      ## Level1
      geom_point(data=df,aes(x=x_vector,y=radius,color=cdf,fill=cdf),size=3,shape=21)+
      geom_text(data=df,aes(x=x_vector,y=label_radius,label=name,color=name))+
    
      ## Level2
      geom_point(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,color=name2,fill=name2),size=3,shape=21)+
      geom_text(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,label=name2,color=name2))+
    
      ## transform into polar coordinates
      coord_polar(theta='x',start=0,direction=-1,clip='on')+
    
      ## sets up the scale to display from 0 to 7
      scale_y_continuous(limits=c(0,7))+
    
      ## Used to 'push' the points so all 'k' show up.
      expand_limits(x=0)
    p
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   psychOle    7 年前

    Bob1 Bob2 作为 df$name df$name=Bob 创造,比如说, df$nr . Bob 事件。

    scale_color_manual scale_fill_manual link ).

        library(ggplot2)
        k<-18  
        ct<-12 
        x_vector<-seq(1,k,1)
        radius<-rep(5,k,1)
        name<-c('Alice','Bob','Charlie','D','E','F','G','H','I','J','K','L','M','N','O','Peter','Quin','Roger')
        df<-data.frame(x_vector,radius,name)
    
        name2<-c('Alice2','Bob2','Charlie','D','E','F','G','H','I','J','K','L','M','N','O','Peter2','Quin2','Roger2')
        plotter<-c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)
        radius2<-rep(7,k,1)
        df2<-data.frame(x_vector,radius2,name2,plotter)
    
        color1<-c(rep('#F8766D',2), # Alice and Alice2
                  rep('#F564E3',2), # Bob and Bob2
                  rep('#B79F00',1)  # Charlie
        )
        other_color<-c(rep('#A9A9A9',12))
        color2<-c(rep('#00BFC4',2),
                  rep('#619CFF',2),
                  rep('#00BA38',2)
        )
        cdf<-c(color1,other_color,color2) #color palette
        df$label_radius<-df$radius+0.5  ##used to adjust the labels by a radius of 0.5
        p<-ggplot()+
          ## Level1
          geom_point(data=df,aes(x=x_vector,y=radius,color=name,fill=name),size=3,shape=21)+
          scale_color_manual(values=cdf)+
          scale_fill_manual(values=cdf)+
          geom_text(data=df,aes(x=x_vector,y=label_radius,label=name,color=name))+
    
          ## Level2
          geom_point(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,color=name2,fill=name2),size=3,shape=21)+
          geom_text(data=df2[(df2$plotter>0),], aes(x=x_vector,y=radius2,label=name2,color=name2))+
    
          ## transform into polar coordinates
           coord_polar(theta='x',start=0,direction=-1)+ #,clip='on')+ # <-- the clip property does not work for me, probably due to my ggplot version 
    
          ## sets up the scale to display from 0 to 7
          scale_y_continuous(limits=c(0,7))+
    
          ## Used to 'push' the points so all 'k' show up.
          expand_limits(x=0)
        p
    

    enter image description here

    推荐文章