代码之家  ›  专栏  ›  技术社区  ›  user2814482

在散点图中正确使用颜色

  •  1
  • user2814482  · 技术社区  · 7 年前

    我想用散馅饼做6个不同的馅饼。有101个不同的类别组成了馅饼(不是所有的馅饼都有101个),所以我想能够区分颜色。

    这并没有给我足够的颜色(我只看馅饼就知道了)

    ggplot(wholebody_cutLH_wide_t) +
    #  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
    geom_scatterpie(aes(x=imX, y=imY,r=radius),
                data=wholebody_cutLH_wide_t,     cols=colnames(wholebody_cutLH_wide_t[1:101]),color=NA) +
    #scale_color_manual(values=sample(allcolors,101)) +
     scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
    scale_y_continuous(expand=c(0,0), lim=c(0,6)) +
    theme(legend.position="none",
        panel.background = element_rect(fill = "transparent") # bg of the panel
        , plot.background = element_rect(fill = "transparent") # bg of the plot
        , panel.grid.major = element_blank() # get rid of major grid
        , panel.grid.minor = element_blank(), # get rid of minor grid
        line = element_blank(),
        text = element_blank(),
        title = element_blank()
    )  
    

    enter image description here

    然后,如果我试图手动设置颜色如下,我会得到一个空白屏幕。 如果我尝试在散点图(color=sample(allcolors,101))中设置颜色,那么我会得到错误

    错误:美学必须为长度1或与数据(2864):颜色相同

    allcolors = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]
    ggplot(wholebody_cutLH_wide_t) +
    #  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
    geom_scatterpie(aes(x=imX, y=imY,r=radius),
                data=wholebody_cutLH_wide_t, cols=colnames(wholebody_cutLH_wide_t[1:101]),color=NA) +
    scale_color_manual(values=sample(allcolors,101)) +
    scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
    scale_y_continuous(expand=c(0,0), lim=c(0,6)) +
    theme(legend.position="none",
        panel.background = element_rect(fill = "transparent") # bg of the panel
        , plot.background = element_rect(fill = "transparent") # bg of the plot
        , panel.grid.major = element_blank() # get rid of major grid
        , panel.grid.minor = element_blank(), # get rid of minor grid
        line = element_blank(),
        text = element_blank(),
        title = element_blank()
     )  
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   user2814482    7 年前

    这是最终的工作代码。我不得不将scale\u color\u手动切换到scale\u fill\u手动。

    ggplot(wholebody_cutLH_wide_t) +
    annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
    geom_scatterpie(aes(x=imX, y=imY,r=radius),
                data=wholebody_cutLH_wide_t, cols=colnames(wholebody_cutLH_wide_t)[1:101],color=NA) +
    scale_fill_manual(values=sample(allcolors,101)) +
    scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
    scale_y_continuous(expand=c(0,0), lim=c(0,6)) +
    theme(legend.position="none",
        panel.background = element_rect(fill = "transparent") # bg of the panel
        , plot.background = element_rect(fill = "transparent") # bg of the plot
        , panel.grid.major = element_blank() # get rid of major grid
        , panel.grid.minor = element_blank(), # get rid of minor grid
        line = element_blank(),
        text = element_blank(),
        title = element_blank()
    )  
    

    这是情节

    enter image description here