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

ggplot色阶渲染不正确

  •  0
  • JFD  · 技术社区  · 5 年前

    我正在努力使用r的ggplot来重新创建最初用python创建的图形。图形使用通过RGB颜色指定的色阶。当我使用相同的RGB颜色创建图形时,它们在我的Windows 10/Rstudio设置中无法正确渲染。下面我显示了正确渲染的图例和r脚本生成的渲染。看起来颜色里透着太多的黄色。

    我不知道从哪里开始尝试确定为什么指定的颜色没有渲染成与我的示例图像相同的颜色。您有什么建议来解决这个问题?

    enter image description here

    下面是我的可重复示例。

    library(urbnmapr)
    library(ggplot2)
    library(dplyr)
    library(ggthemes)
    
    # Set colors 
    red   <- c(0.67, 0.75, 0.84, 0.92, 1,    1,    0.8, 0.53, 0,   0,    0,   0)
    green <- c(0.25, 0.4,  0.56, 0.71, 0.86, 1,    1,   0.95, 0.9, 0.75, 0.6, 0.48)
    blue  <- c(0.11, 0.18, 0.25, 0.33, 0.4,  0.45, 0.4, 0.27, 0,   0,    0,   0)
    
    # Obtain county polygon data
    states_sf <- get_urbn_map(map = "states", sf = TRUE)
    counties_sf <- get_urbn_map(map = "counties", sf = TRUE)
    
    # Assign random values of data to each count  
    counties_sf$value = runif(length(counties_sf$county_fips), min=-3.0, max=3.0)
    
    # Remove AK and HI - lower 48 only 
    states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
    counties_sf <- counties_sf[!(counties_sf$state_abbv %in% c("HI","AK")),]
    
    # Plot county level data with a discrete legend 
    
    data_levels <- c(-3,-1.5, -0.8, -0.5, -0.25,-0.1,0.1,0.25,0.5,.8,1.5,3)
    level_colors <- rgb(red, green, blue)
    
    length(data_levels)
    length(level_colors)
    
    counties_sf %>%
      ggplot() +
      # Overlay State Outlines
      # Plot county data and fill with value
      geom_sf(mapping = aes(fill = value), color = NA) +
      geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
      # Remove grid lines from plot
      coord_sf(datum = NA) +   
      scale_fill_stepsn(breaks=data_levels, colors=level_colors, limits=c(-3,3), 
                        labels=scales::label_number(accuracy=0.1)) + 
      labs(title='This Data is Completely Random', 
           fill ='The Legend') + 
      theme_map() + 
      theme(legend.position = "bottom",
            legend.key.width=unit(1.5,"cm"),
            legend.box.background = element_rect(color="black", size=2),
            legend.title = element_text(face = "bold"),
            legend.spacing = unit(0.25,"cm"),
            legend.justification = "center",
            plot.title=element_text(hjust=0.5))  +
      guides(fill = guide_bins(title.position="top", title.hjust = 0.5,
                               keywidth=unit(20,'points'), axis=FALSE,
                               axis.linewidth=unit(3,'points')))
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   TarJae    5 年前

    这就是你要找的情节吗?使用 n.breaks=12

    更换

    scale_fill_stepsn(breaks=data_levels, colors=level_colors, limits=c(-3,3), labels=scales::label_number(accuracy=0.1)) +

    具有

    scale_fill_stepsn(n.breaks=12, colors=level_colors, limits=c(-3,3), labels=scales::label_number(accuracy=0.1)) +

    enter image description here

    当我运行你的代码时,我得到了这个图。 enter image description here

        2
  •  2
  •   arvi1000    5 年前

    看起来你可能没有使用所有级别?我没有安装 urbnmapr 包来运行您的示例,但当我运行这个更简单的示例时,颜色看起来很合适。

    # Set colors 
    red   <- c(0.67, 0.75, 0.84, 0.92, 1,    1,    0.8, 0.53, 0,   0,    0,   0)
    green <- c(0.25, 0.4,  0.56, 0.71, 0.86, 1,    1,   0.95, 0.9, 0.75, 0.6, 0.48)
    blue  <- c(0.11, 0.18, 0.25, 0.33, 0.4,  0.45, 0.4, 0.27, 0,   0,    0,   0)
    level_colors <- rgb(red, green, blue)
    
    library(ggplot2)
    ggplot(data.frame(x=level_colors, y=1),
           aes(x=level_colors, y=1, fill=level_colors)) +
      geom_col() +
      scale_fill_manual(values = level_colors)
    

    enter image description here

    请注意,如果数据中不存在所有级别,它们也不会全部显示出来。

    ggplot(data.frame(x=level_colors[1:5], y=1),
           aes(x=x, y=y, fill=factor(x))) +
      geom_col() +
      scale_fill_manual(values = level_colors)
    

    enter image description here

    但你可以强迫他们在传奇中表现出来 drop = FALSE 在磅秤调用中:

    dat <- data.frame(x=factor(level_colors, levels=level_colors), y=1)
    ggplot(dat[1:5,], aes(x=x, y=y, fill=x)) +
      geom_col() +
      scale_fill_manual(values = level_colors, drop=F)
    
    

    enter image description here